2011-04-14 18:35:41 -04:00
|
|
|
require "yaml"
|
2011-04-29 14:58:07 -04:00
|
|
|
require "httparty"
|
|
|
|
require "thor"
|
2011-04-14 18:35:41 -04:00
|
|
|
|
|
|
|
module Middleman::Features::Data
|
|
|
|
class << self
|
|
|
|
def registered(app)
|
2011-04-29 14:58:07 -04:00
|
|
|
app.extend ClassMethods
|
2011-04-15 12:57:45 -04:00
|
|
|
app.helpers Middleman::Features::Data::Helpers
|
2011-04-14 18:35:41 -04:00
|
|
|
end
|
2011-04-15 12:57:45 -04:00
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
|
|
|
module Helpers
|
|
|
|
def data
|
|
|
|
@@data ||= Middleman::Features::Data::DataObject.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DataObject
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2011-04-14 18:35:41 -04:00
|
|
|
end
|
|
|
|
|
2011-04-15 12:57:45 -04:00
|
|
|
def method_missing(path)
|
2011-04-29 14:58:07 -04:00
|
|
|
response = nil
|
|
|
|
|
|
|
|
@@remote_sources ||= {}
|
|
|
|
if @@remote_sources.has_key?(path.to_s)
|
|
|
|
response = HTTParty.get(@@remote_sources[path.to_s]).parsed_response
|
|
|
|
end
|
|
|
|
|
2011-04-15 12:57:45 -04:00
|
|
|
file_path = File.join(@app.class.root, "data", "#{path}.yml")
|
|
|
|
if File.exists? file_path
|
2011-04-29 14:58:07 -04:00
|
|
|
response = YAML.load_file(file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
if response
|
|
|
|
recursively_enhance(response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_source(name, json_url)
|
|
|
|
@@remote_sources ||= {}
|
|
|
|
@@remote_sources[name.to_s] = json_url
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def recursively_enhance(data)
|
|
|
|
if data.is_a? Hash
|
|
|
|
data = Thor::CoreExt::HashWithIndifferentAccess.new(data)
|
|
|
|
data.each do |key, val|
|
|
|
|
data[key] = recursively_enhance(val)
|
|
|
|
end
|
|
|
|
data
|
|
|
|
elsif data.is_a? Array
|
|
|
|
data.each_with_index do |val, i|
|
|
|
|
data[i] = recursively_enhance(val)
|
|
|
|
end
|
|
|
|
data
|
|
|
|
else
|
|
|
|
data
|
2011-04-15 12:57:45 -04:00
|
|
|
end
|
|
|
|
end
|
2011-04-14 18:35:41 -04:00
|
|
|
end
|
2011-04-15 12:57:45 -04:00
|
|
|
|
2011-04-29 14:58:07 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Makes HTTP json data available in the data object
|
|
|
|
#
|
|
|
|
# data_source :my_json, "http://my/file.json"
|
|
|
|
#
|
|
|
|
# Available in templates as:
|
|
|
|
#
|
|
|
|
# data.my_json
|
|
|
|
def data_source(name, url)
|
|
|
|
Middleman::Features::Data::DataObject.add_source(name, url)
|
|
|
|
end
|
|
|
|
end
|
2011-04-14 18:35:41 -04:00
|
|
|
end
|