The full sample code used for this post is available as a Ruby on Rails 3.1 project at https://github.com/rietta/SimpleBlogFeed.
Suppose you have built an amazing new website using Ruby on Rails 3. It has tons of features and some great content. However, one thing it is lacking is a solid blog section. You would like to avoid writing your own blog code in Rails since there are already tons of solid blog applications to choose from – Wordpress, Moveable Type, Typepad, Blogger.com, and more.
I personally like Blogger.com because: 1. Its run by Google and integrates nicely with Google Analytics and my Google Apps account 2. It supports being run as a subdomain of your website with a simple DNS entry (blog.example.com instead of just exampleblog.blogspot.com) 3. Supports RSS feeds for both the main blog and for labels 4. Posts are indexed quickly for Google Search results
Parsing a Blog RSS Feed in Ruby 1.9
Ruby supports loading RSS feeds natively without requiring any gem! To see if yourself, fire up IRB on your command line and run the following:
1 2 3 4
The RSS module (which is a native part of Ruby) will load the latest five blog posts from this blog into the blog_posts Array object. Each post in the array is an instance of RSS::Rss::Channel::Item. The object has quite a few methods and attributes, but the ones of primary interest to us are:
Integrating with a Rails 3 Website
In your controller:
1 2 3 4 5 6 7 8 9 10 11 12
In your view that includes the blog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Resulting in the following:
The result will be a bullet list of the latest posts (up to 5) that looks something like this:
Loading Feeds for Specific Labels
Blogger supports full site, comments-only, and label-specific feeds in Atom and RSS format as per Blogger Feed URLs.
An RSS feed for a label is referenced like this:
The label appears after the –/. It is case sensitive such that “Marketing” is not the same as “marketing”.
Please note that at the time of this writing the Google page had an error in that they were showing the a label feed as
Unfortunately, this does not work. Attempting to access that feed like that results in an “Unsupported alt type” error from Blogger’s web server.
The example code includes a full example of loading feeds from a couple of different labels.
Loading Feeds from Your Twitter
Twitter used to make it easy to get the RSS feed for your tweets. You can still access them through the following URL (be sure to replace the xxxxx with the Twitter username you want to use):
(see Here is How to Find Your Twitter RSS Feed and Your Twitter Favorites RSS Feed for a fuller explanation of the Twitter feed urls.)