module Crawlable class Feed class << self attr_accessor :options, :call_options def define!(*args, &block) self.options = args.extract_options!#.merge(:run => args.shift) instance_eval(&block) if block_given? end def parse!(path, options = {}) path ||= File.join(::Rails.root, 'config/initializers/feeds.rb') self.call_options = options.symbolize_keys eval(IO.read(path)) self.call_options = nil end def method_missing(meth, *args, &block) if block_given? if !self.call_options.blank? if call_options.has_key?(meth.to_sym) self.new(meth, *args, &block) else super(meth, *args, &block) end else self.new(meth, *args, &block) end else super(meth, *args, &block) end end end if defined?(::Rails) if ActionPack::VERSION::MAJOR == 3 include ::Rails.application.routes.url_helpers else require 'action_controller' include ActionController::UrlWriter end end attr_accessor :title, :url, :description, :master, :copyright, :updated_at def initialize(*args, &block) options = args.extract_options! name = args.shift options.each do |k, v| self.send(k, v) if self.respond_to?(k) end instance_eval(&block) end def copyright(string = nil) @copyright = string unless string.nil? @copyright end def title(string = nil) @title = string unless string.nil? @title end def description(string = nil) @description = string unless string.nil? @description end def url(string = nil) @url = string unless string.nil? @url end def author(string = nil) @author = string unless string.nil? @author end def master(string = nil) @master = string unless string.nil? @master end def entries @entries ||= [] end def entry(path, *args, &block) options = args.extract_options! result = options.dup result.merge!(:url => path) self.entries.push(result) end def to_rss builder = Nokogiri::XML::Builder.new do |xml| xml.rss "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do xml.channel do xml.title self.title xml.link self.url xml.description self.description self.entries.each do |entry| puts entry.inspect xml.item do xml.title entry[:title] xml.link entry[:url] xml.description entry[:description] xml.guid entry[:guid] || entry[:url] xml.author entry[:author] unless entry[:author].blank? entry[:categories].each do |category| xml.category category end unless entry[:categories].blank? xml.comments entry[:comments] unless entry[:comments].blank? xml.enclosure( :url => entry[:asset], :length => IO.size(entry[:asset]).to_s, :type => entry[:asset_type] ) unless entry[:asset].blank? xml.webMaster self.master unless self.master.blank? end end end end end builder.to_xml end def to_atom builder = Nokogiri::XML::Builder.new do |xml| xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do xml.title self.title xml.link "rel" => "self", "href" => url_for(:only_path => false, :controller => 'feeds', :action => 'atom') xml.link "rel" => "alternate", "href" => url_for(:only_path => false, :controller => 'posts') xml.id url_for(:only_path => false, :controller => 'posts') xml.updated self.updated_at.strftime "%Y-%m-%dT%H:%M:%SZ" if self.updated_at xml.author { xml.name self.author } self.entries.each do |entries| xml.entry do xml.title entries.title xml.link "rel" => "alternate", "href" => url_for(:only_path => false, :controller => 'posts', :action => 'show', :id => entries.id) xml.id url_for(:only_path => false, :controller => 'posts', :action => 'show', :id => entries.id) xml.updated entries.updated_at.strftime "%Y-%m-%dT%H:%M:%SZ" xml.author { xml.name entries.author.name } xml.summary "Post summary" xml.content "type" => "html" do #xml.text! render(:partial => "posts/post", :post => post) end end end end end builder.to_xml end end end