RSS Feeds + Ruby on Rails + Web App

3 12 2007


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

    puts "Checkout Sphred.com while I fetct feeds for you"

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.


Sign up and be a part of SPhred.com and don’t forget to invite your friends ;o)



Actions

Information

12 responses

27 02 2008
anurag

hiii
where to create testfeed.rb?

27 02 2008
nasir

hi anurag,

you can create it anywhere on your system as long as you have feedtools gem, ruby,etc. installed on your system

14 03 2008
anurag

Thanks

18 03 2008
anas

hi nasir i want to know the work and exampl of will_paginate

18 03 2008
nasir

if you want to know how to use will_paginate then it is mentioned here http://nasir.wordpress.com/2007/10/31/pagination-in-ruby-on-rails-using-will_paginate-plugin/

Please let me know if something is not clear.

19 06 2008
James

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?

19 06 2008
mahesh

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

20 06 2008
nasir

Mahesh

Only if that website offers some kind of interface then only you can add your rss feed.

Thanks

18 07 2008
Mike

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!

30 07 2008
Wael Shaban

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

17 03 2009
asanga

Hi nasir,
Can u plz explain how to use unzipped plugin in the vendor/plugins directory in the app.

Thanks,

8 06 2009
yaseen

Hi nasir,

Thanks, how can i pull the from xml to my view?

Leave a comment