Dir[File.dirname(__FILE__) + '/core_ext/*.rb'].each { |e| require e } %w(orthor/http_caching orthor/meta_data orthor/collections orthor/site orthor/object orthor/category orthor/feed orthor/page orthor/static_page).each { |r| require File.join(File.dirname(__FILE__), r) } require File.join(File.dirname(__FILE__), 'orthorings') require File.join(File.dirname(__FILE__), 'orthor_helper') require 'moneta' class Orthorings require 'net/http' class << self dsl_accessor :account_id attr_accessor :cache, :cache_expiry def content(id) get_content(id, "http://content.orthor.com/#{@account_id}/content_items/#{id}.html") end def query(id) get_content(id, "http://content.orthor.com/#{@account_id}/queries/#{id}.html") end def category(id) get_content(id, "http://content.orthor.com/#{@account_id}/categories/#{id}.html") end def feed(id) get_content(id, "http://content.orthor.com/#{@account_id}/feeds/#{id}.rss") end def caching(file, expiry = 300, options = {}) klass = file.to_s.split('_').collect { |e| e.capitalize }.join Moneta.autoload(klass.to_sym, "moneta/#{file}") @cache = Moneta.const_get(klass).new(options) @cache_expiry = expiry end def setup(&block) raise "Block required" unless block_given? @cache = false # default class_eval &block raise "No orthor account id given... Please add one to your Orthorings setup block" if Orthorings.account_id.nil? ActionView::Base.send(:include, OrthorHelper) if defined?(ActionView::Base) && !ActionView::Base.include?(OrthorHelper) end private def get_content(id, url) if @cache get_cached_content(id, url) else get_response(url) end end def get_cached_content(id, url) content = @cache[id] if content.empty? content = get_response(url) cache_content(id, content) end content end def cache_content(id, content) @cache.store(id, content, :expires_in => @cache_expiry) unless content.empty? end def get_response(uri) r = Net::HTTP.get_response(URI.parse(uri)) return (r.code.to_i == 200) ? r.body : "" rescue return "" end end end