From some fiddling I’ve found that there are two ways to generate RSS
using the rubyRSS libraries shipped with ruby 1.8.7.
Neither method seems ideal for generating RSS 2.0 (the RSS XML
produced seems to be missing tags/attributes that I would like in both
cases). Is there something that I’m missing and is there a preferred
method?
In particular, I would like to use the ‘guid’ attribute with rss/maker
but it seems to be unsupported? Surely there are some feed readers who
depend on this for determining if an item is unique? It does seem the
‘guid’ is a part of the RSS 2.0 standard.
However, if I were to use the rss/2.0 method, I could specify the
guid, but would lose the dc:date attribute and all of the xmlns
definitions at the top. Also with rss/2.0 I couldn’t work out how to
do a reverse sort of items (is this even needed though).
Any comments?
Method 1: rss/maker, simplest example:
#!/usr/bin/env ruby
require ‘rss/maker’
if FILE == $0
content = RSS::Maker.make(‘2.0’) do |m|
m.channel.title = “title”
m.channel.about = “about” # <<<=seems to not be used
m.channel.link = “www.example.com”
m.channel.description = “desc”
m.items.do_sort = true # <<<= sort items by date
2.times do |n|
i = m.items.new_item
i.title = "title"
i.link = "www.example.com/" + n.to_s
# i.guid = <<<=Not supported
i.description = "Main body"
i.date = Time.now
end
end
puts content.to_s
end
Outputs:
<?xml version="1.0" encoding="UTF-8"?>
title
www.example.com
desc
title
www.example.com/1
Main body
Fri, 26 Jun 2009 16:35:31 +0200
dc:date2009-06-26T16:35:31.598452+02:00</dc:date>
title
www.example.com/0
Main body
Fri, 26 Jun 2009 16:35:31 +0200
dc:date2009-06-26T16:35:31.592903+02:00</dc:date>
Method 2: rss/2.0, simplest example:
#!/usr/bin/env ruby
require ‘rss/2.0’
if FILE == $0
content = RSS::Rss.new(‘2.0’)
channel = RSS::Rss::Channel.new
channel.title = “title”
channel.link = “www.example.com”
channel.description = “desc”
#channel.item.do_sort = true "<<<=Cannot sort
2.times do |n|
i = RSS::Rss::Channel::Item.new
i.title = “title”
i.link = “www.example.com/” + n.to_s
i.guid = RSS::Rss::Channel::Item::Guid.new
i.guid.content = n.to_s
i.guid.isPermaLink = true
i.description = “Main body”
i.date = Time.now
channel.items << i
end
content.channel = channel
puts content.to_s
end
Outputs:
<?xml version="1.0"?> title www.example.com desc title www.example.com/0 Main body Fri, 26 Jun 2009 16:39:37 +0200 0 title www.example.com/1 Main body Fri, 26 Jun 2009 16:39:37 +0200 1Regards,
Jonathan G…