Fetch Rss Feeds in Rails

Heya Guys,

Gossshh, I had lot of work, woooooohhh, but now I am over with that and thought to write a new post for all of you… I am very thankful to all of you that you are appreciating my articles and giving me such a good response, It is not only developing my confidence but also I want to bring more and more articles in rails, so that It will help you to know the magic behined RoR.

So let me start it. In this post of mine I will Fetch Rss Feeds from Techcrunch with open-uri, Interested to know, let’s see how it works…

Create an application, open your environment.rb file and add this line at the bottom:
RSS_FEED = ‘http://feedproxy.google.com/TechCrunch’

Now create a controller, in my case I have feeds_controller with fetch method, Open it and add below lines
class FeedsController < ApplicationController
require ‘rss/2.0’
require ‘open-uri’
def fetch
feed_url = “#{RSS_FEED}”
count = 50.to_i
output = “

Rss Reader


open(feed_url) do |http|
response = http.read
result = RSS::Parser.parse(response, false)
output += “Reed Title: #{result.channel.title}

result.items.each_with_index do |item, i|
output += “#{i+1}. ‘#{item.title}’
” if i output
end
end

You are almost done guys, now open up your views/feeds/fetch.html.erb and write a single line:

That is it. You are done.. start your server and you should see all the feeds there…
like this:

Rss Feeds
Rss Feeds

Don’t forget to drop me your inputs, suggestions, feedback on this..

With Love
Puneet

Advertisements

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