require 'rss/0.9' require 'glue/string' require 'nitro/builder/xml' module Nitro # Build RSS represenations of ruby object collections. # Utilize duck typing to grab the attributes to render. # Add this mixin to you classes to support RSS rendering. # #-- # TODO: add more options here, 1.0/2.0 support and more. # use custom version to add headers like the following. # # #++ module RssBuilderMixin include XmlBuilderMixin # === Options # # [+:description+] # Description of the Feed # [+:base+] # Base url of the Feed. # [+:link+] # Link of the Feed. def build_rss_09(objects, options = {}) c = { :description => 'Syndication' }.update(options) c[:base] ||= c[:link] raise "Option ':base' cannot be infered!" unless c[:base] channel = RSS::Rss::Channel.new channel.description = c[:description] channel.link = c[:link] if c[:link] for obj in objects item = RSS::Rss::Channel::Item.new item.title = obj.title if obj.respond_to?(:title) # item.description = CGI.escape(Glue::StringUtils.head(obj.body, 256)) if obj.respond_to?(:body) item.link = "#{c[:base]}/#{obj.to_href}" if obj.respond_to?(:to_href) channel.items << item end rss = RSS::Rss.new '0.9' rss.channel = channel self << rss.to_s end alias_method :build_rss, :build_rss_09 =begin Experimental. def build_rss(objects, options = {}) # pi! :xml, :version => '1.0', :encoding => 'UTF-8' self << '' self << '' rss(:version => '2.0', 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:trackback' => 'http://madskills.com/public/xml/rss/module/trackback/') { channel { title 'koko' language 'en-us' ttl '40' description 'hello lamer' for obj in objects item { title(obj.title) description(obj.body) trackback :ping => 'http://www.joy.gr' } end } } end =end end # Abstract class for the RssBuilderMixin. class RssBuilder < String include RssBuilderMixin class << self def build_09(objects, options = {}) RssBuilder.new.build_rss_09(objects, options) end alias_method :build, :build_09 end end end # * George Moschovitis