Twitter integration from your Rails app
We’ve learnt a bit about Twitter from writing ykyat.com.
I particularly want to write about how we fetch Twitter user icons. The icons are stored on AWS (Amazon Web Services) and cannot be deduced from the user name. You have to go through the Twitter API to find the image location.
The first thing I tried was the Twitter4R library. This seems to be a very powerful library, and if you were writing a full Twitter client in Ruby you’d definitely want to consider it. It was simple to get the user icons, but the library just felt a little over-the-top for our needs.
Twitter4R seemed to require authentication with every request, and we soon hit the API limit. I realised this is odd because you really don’t need to authenticate to look at the XML or JSON data about a Twitter user. I decided to go back to basics and do it myself. This is the code for parsing the JSON data and picking up the user icon URL:
def icon_url_for_user(username)
require 'open-uri'
require 'json'
buffer = open("http://twitter.com/users/show/#{username}.json").read
result = JSON.parse(buffer)
result['profile_image_url']
end
See! Easy!
Most people don’t change their user icon very often, so once we know where to find a user’s icon, we don’t need to ask Twitter for it again for an arbitrary amount of time. A week seems quite sensible. To that end, we created a lookup table in our database to match Twitter user names to their user icon URL. We added an index to the user name column because it acts as the primary key lookup.
When we want to know a user’s icon, we first look up in our table. If we don’t yet have it, or if the updated_at date is more than a week ago, we check with Twitter for the image location. Otherwise we use our cached location.
Fast and easy! :)

Grrrrrr
thank you first of all for this article
i did not understand where to put this code :
def icon_url_for_user(username)
require 'open-uri'
require 'json'
buffer = open(”http://twitter.com/users/show/#{username}.json”).read
result = JSON.parse(buffer)
result['profile_image_url']
end
could you please explain more
regards
You have found really simple way for work with icons.
Besides it will save server resources.
As a pancake lover, I have long searched for a blog covering its different variations. My kids inherited the same passion as well. Now with the links that you've shared, I'll definitely be enlightened with lots of new recipes hrsaccount to surprise my kids with! Thanks!
This is my first look at twitter / rails integration but it seems like using any library is silly when it is this easy to grab key values from the json.
I agree. Quite often if the task is simple enough, it's easier just do do it yourself! :)
test
test
we created a lookup table in our database to match Twitter user names to their user icon URL. We added an index to the user name column because it acts as the primary key lookup.
Thanks and Regards
Wow! With each day, I'm getting to learn more about Twitter from informative posts like this and at the same time find new and more uses for it! Good one Aimee! Keep it coming! :)
Cheers,
Joni
Really Easy!!! I didn’t know it’s that simple to use basic to get data about tweeter users. Twitter 4R library is really disgusting and slow to use no matter how powerful it is. Great post! Thanks for sharing.
I'm really looking forward to this going live. Should be interesting to see how it affects the way people use the conference. Lots of potential for increasing the ability to network effectively.