def atom_feed(params={})
require 'builder'
require 'time'
limit = params[:limit] || 5
relevant_articles = params[:articles] || articles || []
content_proc = params[:content_proc] || lambda { |a| a.compiled_content(:snapshot => :pre) }
excerpt_proc = params[:excerpt_proc] || lambda { |a| a[:excerpt] }
if @site.config[:base_url].nil?
raise RuntimeError.new('Cannot build Atom feed: site configuration has no base_url')
end
title = params[:title] || @item[:title] || @site.config[:title]
if title.nil?
raise RuntimeError.new('Cannot build Atom feed: no title in params, item or site config')
end
author_name = params[:author_name] || @item[:author_name] || @site.config[:author_name]
if author_name.nil?
raise RuntimeError.new('Cannot build Atom feed: no author_name in params, item or site config')
end
author_uri = params[:author_uri] || @item[:author_uri] || @site.config[:author_uri]
if author_uri.nil?
raise RuntimeError.new('Cannot build Atom feed: no author_uri in params, item or site config')
end
if relevant_articles.empty?
raise RuntimeError.new('Cannot build Atom feed: no articles')
end
if relevant_articles.any? { |a| a[:created_at].nil? }
raise RuntimeError.new('Cannot build Atom feed: one or more articles lack created_at')
end
sorted_relevant_articles = relevant_articles.sort_by do |a|
time = a[:created_at]
time.is_a?(String) ? Time.parse(time) : time
end.reverse.first(limit)
last_article = sorted_relevant_articles.first
buffer = ''
xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2)
xml.instruct!
xml.feed(:xmlns => 'http://www.w3.org/2005/Atom') do
root_url = @site.config[:base_url] + '/'
xml.id root_url
xml.title title
time = last_article[:created_at]
xml.updated((time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time)
xml.link(:rel => 'alternate', :href => root_url)
xml.link(:rel => 'self', :href => feed_url)
xml.author do
xml.name author_name
xml.uri author_uri
end
sorted_relevant_articles.each do |a|
url = url_for(a)
next if url.nil?
xml.entry do
xml.id atom_tag_for(a)
xml.title a[:title], :type => 'html'
create_time = a[:created_at]
update_time = a[:updated_at] || a[:created_at]
xml.published((create_time.is_a?(String) ? Time.parse(create_time) : create_time).to_iso8601_time)
xml.updated( (update_time.is_a?(String) ? Time.parse(update_time) : update_time).to_iso8601_time)
xml.link(:rel => 'alternate', :href => url)
summary = excerpt_proc.call(a)
xml.content content_proc.call(a), :type => 'html'
xml.summary summary, :type => 'html' unless summary.nil?
end
end
end
buffer
end