Rietta
Rietta: Web Apps Where Security Matters
You are reading The Rietta Blog, a publication about the web since 2005.

Integrate Blog Content With Your Rails 3 Website With Pure Ruby Code and RSS

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
  require 'rss'
  blog_posts = RSS::Parser.parse(
      open('http://blog.rietta.com/feeds/posts/default?alt=rss').read, false
    ).items[0...5]

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:

  • :title
  • :description
  • :link
  • :guid
  • :pubDate

Integrating with a Rails 3 Website

In your controller:

1
2
3
4
5
6
7
8
9
10
11
12
# It is important to require the RSS module.  If you leave this off, the blog feed will not load.
require 'rss'
class PagesController < ApplicationController
  def index
     begin
       @latest_blog_posts = RSS::Parser.parse(open('http://blog.rietta.com/feeds/posts/default?alt=rss').read, false).items[05]
     rescue
       # Do nothing, just continue.  The view will skip the blog section if the feed is nil.
       @latest_blog_posts = nil
     end
  end
end

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
<h1>Simple Blog Feed</h1>
<% unless @latest_blog_posts.nil? %>
 The latest <%= pluralize(@latest_blog_posts.count, “post”) %> from the blog.
 <ul>
  <% @latest_blog_posts.each do |post| %>
   <% if nil != post && post.respond_to?(:pubDate) %>
    <li>
     <%= link_to post.title, post.link, :target => “_blank” %>
     (<%= time_ago_in_words(post.pubDate) %> ago via blog feed)
    </li>
   <% end %>
  <% end %>
 </ul>
<% else %>
 <p>
  <em>No blog posts to show.</em>
 </p>
<% end %>

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: http://blog.rietta.com/feeds/posts/default/-/Marketing?alt=rss

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 http://www.googleblog.blogspot.com/feeds/posts/default?alt=rss/-/privacy

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):

http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=xxxxx

(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.)

Further Reading

Comments

Frank Rietta
Excellent! Glad to be of service to you, Ed :–)
Ed Trollope
Thank you for this. I was trying to create a feed to a specific label and ran afoul of the incorrect information on Google's site (as you describe here, which is how I found you!). Your link works perfectly :)
avatar
Great post, thanks!
avatar
This comment has been removed by the author.
TiSer
This comment has been removed by the author.

About Frank Rietta

Frank Rietta's photo

Frank Rietta is specialized in working with startups, new Internet businesses, and in developing with the Ruby on Rails platform to build scalable businesses. He is a computer scientist with a Masters in Information Security from the College of Computing at the Georgia Institute of Technology. He teaches about security topics and is a contributor to the security chapter of the 7th edition of the "Fundamentals of Database Systems" textbook published by Addison-Wesley.