Module: ResourceFeeder::Rss

Extended by:
Rss
Included in:
Rss
Defined in:
lib/resource_feeder/rss.rb

Instance Method Summary collapse

Instance Method Details

#render_rss_feed_for(resources, options = {}) ⇒ Object



7
8
9
# File 'lib/resource_feeder/rss.rb', line 7

def render_rss_feed_for(resources, options = {})
  render :text => rss_feed_for(resources, options), :content_type => Mime::RSS
end

#rss_feed_for(resources, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/resource_feeder/rss.rb', line 11

def rss_feed_for(resources, options = {})
  xml = Builder::XmlMarkup.new(:indent => 2)

  options[:feed]       ||= {}
  options[:item]       ||= {}
  options[:url_writer] ||= self
  
  if options[:class] || resources.first
    klass      = options[:class] || resources.first.class
    new_record = klass.new
  else
    options[:feed] = { :title => "Empty", :link => "http://example.com" }
  end
  
  options[:feed][:title]    ||= klass.name.pluralize
  options[:feed][:link]     ||= polymorphic_url(new_record)
  options[:feed][:language] ||= "en-us"
  options[:feed][:ttl]      ||= "40"
  
  options[:item][:title]       ||= [ :title, :subject, :headline, :name ]
  options[:item][:description] ||= [ :description, :body, :content ]
  options[:item][:pub_date]    ||= [ :updated_at, :updated_on, :created_at, :created_on ]

  resource_link = lambda { |r| polymorphic_url(r) }

  xml.instruct!
  xml.rss(:version => "2.0") do
    xml.channel do
      xml.title(options[:feed][:title])
      xml.link(options[:feed][:link])
      xml.description(options[:feed][:description]) if options[:feed][:description]
      xml.language(options[:feed][:language])
      xml.ttl(options[:feed][:ttl])

      for resource in resources
        xml.item do
          xml.title(call_or_read(options[:item][:title], resource))
          xml.description(call_or_read(options[:item][:description], resource))
          xml.pubDate(call_or_read(options[:item][:pub_date], resource).to_s(:rfc822))
          xml.guid(call_or_read(options[:item][:guid] || options[:item][:link] || resource_link, resource))
          xml.link(call_or_read(options[:item][:link] || options[:item][:guid] || resource_link, resource))
        end
      end
    end
  end
end