RSS Feeds + Ruby on Rails + Web App


The Feedtools library is a very comprehensive and powerful Ruby library for handling rss, atom, etc as well as caching. It makes creating, consuming and manipulating feeds a piece of cake.

It’s ideal for parsing RSS feeds in Ruby on Rails applications.

Assuming that you already have installed Ruby and Rubygem, simply run

gem install feedtools

to install the FeedTools gem

or download it from the FeedTools project page and put the untar/unzipped folder in the vendor/plugins directory of your app.

To do a very quick test, create a file called testfeed.rb and add the below code in it:

    require "RubyGems"
    require "feed_tools"

    feedurls = 'http://www.sphred.com/combined_feed'
    # If you want to fetch more than one feed then comment the above feedurls variable and   uncomment the below one.
    #feedurls = %w(http://feeds.feedburner.com/Sphred_top_10_feeds   http://feeds.feedburner.com/Sphred_site_only http://feeds.feedburner.com/Sphred_site_feature)
    my_feeds = FeedTools::build_merged_feed feedurls

    my_feeds.title = 'Sphred.com Feed'
    my_feeds.copyright = 'SPhred'
    my_feeds.author = 'Nasir '
    my_feeds.id = "http://www.sphred.com/combined_feed"

    File.open('./my_feeds.xml', 'w') do |file|
      file.puts my_feeds.build_xml()
    end

Running this file from IRB will create a my_feeds.xml file in your current directory with all the feed contents.

To show feeds on a website, you need to do a few things:
1) Put

require "feed_tools"

at the top of your controller
2) Create an action within this controller to show your feeds, lets call that action user_data

    def user_data
      @feed = FeedTools::Feed.open(params[:feed_url])
      # You can first test it with a static feed url like this
      #@feed = FeedTools::Feed.open('http://www.sphred.com/combined_feed')
    end

3) In the corresponding view add this code

  <div class="feeds">
    <h3>
      <a href="<%= h @feed.link %>">
      <%= @feed.title %></a>
    </h3>
    <p><%= @feed.description %></p>
    <% for feed in @feed.items %>
      <div class="feed_item">
        <h4>
          <a href="<%= h feed.link %>">
          <%= feed.title %></a>
        </h4>
        <p><%= feed.description %></p>
     </div>
   <% end %>
 </div>

This is all good for development environment or for small apps but once you go to production environment then you have to think of caching to avoid hitting the feed server every time before displaying feed contents.

Advertisements

18 thoughts on “RSS Feeds + Ruby on Rails + Web App

  1. James says:

    Thanks for the example code. My feed works great, but I am struggling to add images to it. Could you discuss the pros and cons of using enclosures VS images and show us how to take this to the next step?

  2. mahesh says:

    hi, nasir can you explain how can i upload my feed to another website. i.e have created my feed as an xml file. now how can i upload my xml files to that website

  3. Mike says:

    I’m relatively new to ruby and I dont really understand the code you’ve written, can you please write comments so I could better understand this?

    Thanks!

  4. Wael Shaban says:

    Hi,
    Thanks for this great work.
    I have a problem related to authentication. My RSS feed that I need to fetch requires authentication.
    I dunno how I can pass this authentication to FeedTools gem?!

    Can you help me please?

    thanks

  5. Rup says:

    how to control the number of feeds retrieved. I just want top 10 feeds from my feed url. How can I pass parameter to the feed reader to just get top 10 for me?
    Any ideas??

  6. nasir says:

    @Rup I don’t think you can control that. Why don’t you do your operations only with the top 10 feed that you want from all the feeds that are fetched?

  7. suryavir sood says:

    You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.link

    i have the follwoing error… any suggestions?

  8. Olya says:

    I get this error:

    undefined method `link’ for nil:NilClass

    Could you, please, help me figure this out?
    I did install feedtools.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s