views:

360

answers:

3

I want to add a twitter feed for a specific keyword search to my rails application. Where should I start?

+3  A: 

You might start with one of the Twitter API libraries written for Ruby.

Amber
I'm taking a look a the twitter gem by John Nunemaker. Thanks
Nick
+1  A: 

You may want to consider grabbing the RSS feed for the search and parsing that. I show that in Railscasts episode 168. If you need something more fancy the API is the way to go as Dav mentioned.

But whichever solution you choose, it's important to cache the search results locally on your end. This way your site isn't hitting Twitter every time someone goes to the page. This improves performance and will make your site more stable (not breaking when twitter breaks). You can have the cache auto-update every 10 minutes (or whatever fits) using a cron task.

ryanb
This looks like the simplest solution so I'm going to give it a go. Thanks. And thanks for Railscasts!
Nick
Hi Ryan, the screencast is great and easy to follow the only thing I can't work out is how to name the entries that will correspond to the columns in my model. e.g. in feed_entry.rb you've got: create!( :name => entry.title ... )How do I work out from the twitter RSS feed what my tweet entries should be called? Thanks again, Nick
Nick
Do you mean how to determine which methods you can call on entry to fetch the appropriate content? The best thing to do is play around with it in the console: http://pastie.org/574731
ryanb
Yes that's exactly what I meant. Perfect!
Nick
I'm almost there, however I get the following error http://pastie.org/575442 when I try and run the update_from_feed method from the console. My rails app is v2.2.2 so I loaded activerecord 2.2.2 while in the console to make sure that wasn't the issue. I've loaded feedzirra and my model file http://pastie.org/575466. My database connection works for the other models in my app. Any advice would be great. Many thanks
Nick
How strange. I'm assuming you are using script/console (not irb) and in the normal development environment? Does Tweet.create! call work directly in the console? It should work if other models work.
ryanb
My apologies, I was using irb instead of script/console. It is working properly. I can't thank you enough. I've learnt a useful new technique from this exercise.
Nick
A: 

We download and store the tweets in a local database. I recently wrote a blog post about how I achieved this:

http://www.arctickiwi.com/blog/16-download-you-twitter-feed-using-ruby-on-rails-with-oauth

You can then use will_paginate to handle your pagination and go back as far as you want.

Jonathon Horsman