James

Tale of three methods to display Twitter in Rails

November 19th, 2008 by James

PocketSmith is using Twitter to keep our users up-to-date with what is going on behind the scenes in real-time within the application. If you haven’t already, you can follow us here: http://twitter.com/PocketSmith (and if you would like to follow me personally, you can do so here: http://twitter.com/wigsgiw).

In order to get our tweets to display within PocketSmith, I investigated what options were available. Unfortunately for me, I spent a great deal of time on the first and second solutions, without knowledge of the third after a cursory glance at Twitter (I should have looked harder, but in reality I shouldn’t have had to. More on this later).

Twitter Method One - The Easy-ish Solution

This method was pretty quickly disregarded due to the API limits of Twitter of 70 requests per hour. There is a tasty Rails gem that allows all sorts of RESTful Twitter API calls and the like - twitter4r. Using it was a cinch; however as all we wanted to do was pull in tweets, it was complete overkill. And it took 1.5 seconds for a page to load with the requested tweets. Not ideal.

Twitter Method Two - The Hard Solution

The second option I went for was a bit of home-brew that involved parsing the public Twitter XML / RSS feed into something that was usable by our application. The advantage of this over the old method was that the API wasn’t used, and was basically a form of page scraping and transforming our tweets.

Using the Hpricot HTML parser gem for Rails, we were grabbing the RSS feed for our timeline and breaking up individual parts into an array to throw it into the view. I also included a check to see if the status “I’m ticking along quite nicely” was included, thereby removing the tweets from display - in the case we wanted to use Twitter just for important system messages.

Issues with this were that this made the Twitter feed nigh-on useless for people who wanted to keep up-to-date with PocketSmith outside of the application itself - i.e. people who actually use Twitter. In addition, if Twitter went down, PocketSmith would too (as the RSS feed was inaccessible) - although the strides in stability of Twitter meant this was nearly impossible to test, the theory was sound.

So I built in a check to ensure that a successful HTTP Response was received by Twitter before we did anything else, the theory was that this would fix the issue. So with this sorted, we got it live and all was well.

Until Twitter actually went down for the first ‘live’ test of what would happen in this situation. And it was bad.

So I removed tweets from the view, commented out the code and moved on to other things. We could solve this issue later. But I now knew how to parse things from Hpricot and check HTTP response headers. Learning = good.

Twitter Method Three - The Hand-Meet-Forehead Solution

A week ago I was perusing the settings for my personal Twitter account when I saw an innocuous link within the settings page muttering “You can also add Twitter to your site here”. Oh really?

I would go into details, however it was so drop-kick simple to set up that I won’t. So now we are up and running, with a total of 6 lines of code written and a couple of javascript files included. Can’t find the link within Twitter? I’m not surprised. It is here: http://twitter.com/badges.

Another post is coming about the marketing failure on Twitters behalf in the placement of this link, so I’ll save my rant for that.

However good news - PocketSmith does not go down with Twitter, and everyone is kept up-to-date. Success, finally :D

Tags: ,

Leave a Reply