Generating an Atom feed using the rss gem
| updated
See Atom feeds for a summary of the Atom specification.
The rss gem is bundled with Ruby.
On Debian, it is included in the librubyX.Y package.
Usage example
The README has the following example for generating an Atom feed:
require 'rss'
rss = RSS::Maker.make('atom') do |maker|
maker.channel.author = 'matz'
maker.channel.updated = Time.now.to_s
maker.channel.about = 'http://www.ruby-lang.org/en/feeds/news.rss'
maker.channel.title = 'Example Feed'
maker.items.new_item do |item|
item.link = 'http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/'
item.title = 'Ruby 1.9.2-p136 is released'
item.updated = Time.now.to_s
end
end
puts rss
It generates the following output:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<author>
<name>matz</name>
</author>
<id>http://www.ruby-lang.org/en/feeds/news.rss</id>
<title>Example Feed</title>
<updated>2025-03-26T08:14:32+02:00</updated>
<entry>
<id>http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/</id>
<link href="http://www.ruby-lang.org/en/news/2010/12/25/ruby-1-9-2-p136-is-released/"/>
<title>Ruby 1.9.2-p136 is released</title>
<updated>2025-03-26T08:14:32+02:00</updated>
<dc:date>2025-03-26T08:14:32+02:00</dc:date>
</entry>
<dc:date>2025-03-26T08:14:32+02:00</dc:date>
</feed>
Observations
- The
/feed/idis set to the value ofchannel.about. - The
/feed/entry/idis set to the value ofitems[i].link. /feed/dc:dateand/feed/entry/dc:dateelements are generated for some reason, with the same value as the correspondingupdatedelement.
Decisions
For this blog …
It should be fine to use
Time.nowfor thechannel.updatedvalue, as long as I only run the script when the inputs have changed.But use
Time.now.utcinstead ofTime.now.to_s.What to put in
channel.about??Need to pick a fixed value for
channel.idin order to not confuse consumers.The
item.updatedfield should in theory be set to the date on which the page was last published, but it should be fine if it’s the date on which I updated it locally, even if I don’t publish the update straight away.To turn the date into a timestamp, set the time component to 12:00 UTC. There is no need to record the exact hour and minute of publication. In Ruby:
Time.new("#{meta['date']} 12:00:00", in: 'Z')