Module: ResourceFeeder::Atom

Extended by:
Atom
Included in:
Atom
Defined in:
lib/resource_feeder/atom.rb

Instance Method Summary collapse

Instance Method Details

#atom_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/atom.rb', line 11

def atom_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][:id]    ||= "tag:#{request.host_with_port}:#{klass.name.pluralize}"
  options[:feed][:link]  ||= polymorphic_url(new_record)
  
  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(new_record) }

  xml.instruct!
  xml.feed "xml:lang" => "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do
    xml.title(options[:feed][:title])
    xml.id(options[:feed][:id])
    xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:feed][:link])
    xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:feed][:self]) if options[:feed][:self]
    xml.subtitle(options[:feed][:description]) if options[:feed][:description]

    for resource in resources
      published_at = call_or_read(options[:item][:pub_date], resource)

      xml.entry do
        xml.title(call_or_read(options[:item][:title], resource))
        xml.content(call_or_read(options[:item][:description], resource), :type => 'html')
        xml.id("tag:#{request.host_with_port},#{published_at.xmlschema}:#{call_or_read(options[:item][:guid] || options[:item][:link] || resource_link, resource)}")
        xml.published(published_at.xmlschema)
        xml.updated((resource.respond_to?(:updated_at) ? call_or_read(options[:item][:pub_date] || :updated_at, resource) : published_at).xmlschema)
        xml.link(:rel => 'alternate', :type => 'text/html', :href => call_or_read(options[:item][:link] || options[:item][:guid] || resource_link, resource))
      end
    end
  end
end

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


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

def render_atom_feed_for(resources, options = {})
  render :text => atom_feed_for(resources, options), :content_type => Mime::ATOM
end