Sha256: c98685c54aa9296ac0fac7bddc3428cd2142ed00ee8b7633348d42603bfc7d02

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# * George Moschovitis  <gm@navel.gr>
# (c) 2005 Navel, all rights reserved.
# $Id: rss.rb 1 2005-04-11 11:04:30Z gmosx $

require 'rss/0.9'

require 'glue/string'

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.
#++

module RssBuilderMixin

	# === 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 = Glue::StringUtils.head(obj.body, 256) if obj.respond_to?(:body)
			item.link = "#{c[:base]}/#{obj.view_uri}" if obj.respond_to?(:view_uri)
			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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.16.0 lib/nitro/builders/rss.rb