Tutorial
Since a tutorial was requested for setting FeedTools up with Rails, I thought I’d do a quick example. It’s really, really not hard. I packaged the whole thing up and put it on RubyForge, and maybe someday I’ll polish it up. Probably not until I have support for subscriptions in place. Not really worth it until then.
Anyhow, without further ado, I give you… your really short tutorial.
Ok, first off, you’re going to want to start loading the feed as early as possible. Ideally, you should start loading it within a thread at the beginning of the controller’s action, do whatever other activities you need to, then join on the thread. This is especially true if you’re loading multiple feeds at once. In the interests of simplicity though, we’re only going to load one feed, and since the feed is the only thing we care about in this action, we’ll just skip the threading bit.
So our ‘view’ action will simply look like this:1 2 3 4 |
def view @feed = FeedTools::Feed.open(@params['feed_url']) end |
We have a form as part of our layout that points to the view action and passes off a feed_url, so you can pick whatever feed you want to display.
In the view, it’s a simple matter of displaying the feed’s attributes and looping through the feed items and then displaying their attributes as well.
Take a look:1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<div class="contentBox">
<h1><a href="<%= h @feed.link %>">
<%= @feed.title %></a></h1>
<p><%= @feed.description %></p><%
for feed_item in @feed.items %>
<div class="feedItemBox">
<h2><a href="<%= h feed_item.link %>">
<%= feed_item.title %></a></h2>
<p><%= feed_item.description %></p>
</div><%
end
%>
</div>
|
The second part of the tutorial is even simpler. Just for fun I threw in some of the xml building capabilities. If you switch over to the ‘xml’ action, you can translate any feed to another variety of xml feed. Right now, RSS 1.0, 2.0, and Atom 0.3 are the only valid options, but eventually the xml generation should run the whole gamut of varieties.
Here’s the ‘xml’ action:1 2 3 4 5 |
def xml @feed_type = @params['id'] @feed = FeedTools::Feed.open(@params['feed_url']) end |
1 2 |
@feed.build_xml((@feed_type or "atom"), 0.0, xml) |
That’s all there is to it.
You’ll probably notice in the code that I disabled the cache because I wanted to have the thing be configuration-free, but feel free to point the database.yml file to your own database and turn caching back on.
The hardcore XHTML fans should also be happy to discover that I chose to deliver the content for this tutorial as XHTML 1.1 with the application/xhtml+xml content type. Thanks to Tidy, all of the nasty-looking html from the feed gets polished up, and everything works out perfectly.
Also, FeedTools 0.2.3 is out. It’s just a hotfix for a nasty bug that several people have pointed out, where get parameters were ignored.
Update: Martin Dittus wrote a really nice introduction to FeedTools, focussing on how to get it working in a non-Rails project.
Update: Apparently, threads aren’t so hot when a database-backed cache is involved because it opens up tons of extra connections to the database.