Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An example of how to integrate a blog with a Rails 3 site using the native RSS module.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 config
Octocat-spinner-32 db
Octocat-spinner-32 doc
Octocat-spinner-32 lib
Octocat-spinner-32 log
Octocat-spinner-32 public
Octocat-spinner-32 script
Octocat-spinner-32 test
Octocat-spinner-32 vendor
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 config.ru
README
Example Code for Integrate Blog Content with your Rails 3 Site with Pure Ruby Code and RSS
By Frank Rietta

See the original blog post at http://blog.rietta.com/2012/01/integrate-blog-content-with-your-rails.html.


-----------------------------------------
Overview
-----------------------------------------

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: 
- Its run by Google and integrates nicely with Google Analytics and my Google Apps account
- It supports being run as a subdomain of your website with a simple DNS entry (blog.example.com instead of just exampleblog.blogspot.com)
- Supports RSS feeds for both the main blog and for labels
- 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:
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:


# 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[0...5]
     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:


<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 %>

-----------------------------------------
LICENSE TERMS (BSD License)
-----------------------------------------

Copyright (c) 2012 Rietta Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Something went wrong with that request. Please try again.