Majorly Pimpin' The Twitter Gem

Behind closed doors, over the past few weeks, I have been hacking away on the twitter gem. The last time I wrote about it was because I added STDIN. The updates I’ll run through right now are way cooler than that, I promise. Below are some of the highlights including a complete rewrite of the command line interface and support for the search API.

When Stuff Goes Wrong (0.2.7)

Originally the twitter gem pretty much either worked or didn’t and provided very little help as to which happened. In 0.2.7, I added a few more exceptions, RateExceeded and Unavailable, to better give you an idea of what is happening in the abyss known as the twitter gem. I also added the source parameter, so now when you command line tweet it says ‘twitter gem’ and it’s easier for those using the gem with their applications to add their source.

CLI with Multiple Account Support (0.3.0)

During a fit of frustration with Twitterrific and all other twitter clients, I completely rewrote the command line interface for the gem using Main and Highline. Other than cleaning up the command line code, this rewrite also brought a few new features.

First, the twitter gem now supports multiple accounts. It creates and maintains a sqlite database locally to store your accounts, tweets and last seen information. You can see most of the code here. If you already have a .twitter file in your home directory, it will attempt to import that account and run with it when you use a command the first time. See the rubyforge page for more installation instructions and such.

Second, anytime you check your timelines and replies, it remembers what you’ve seen. Next time you check, it will only show you the new tweets.

Third, and finally, I updated the API wrapping part of the gem to support all of twitter’s API updates. Methods like, friendship_exists?, update_location, update_delivery_device, favorites, create_favorite, destroy_favorite, block and unblock were added.

Holy Sexy Formatting Batman! (0.3.1)

In one of my faovrite updates, I spent some time formatting the tweets to optimize readability. The usernames are right aligned and the tweet text wraps neatly beside them with short simple lines. The usernames and the tweet text are now really easy to scan quickly and read. In fact, I’ve actually started using the gem as my main twitter client which previously I had not.

terminal_output.png

Each line of tweet text is about 5 words (space separated). Also, a small dashed line above and a blank line below the date helps separate one tweet from the next. You know you have too much time on your hands when…

Identi.ca Support (0.3.3)

I almost didn’t add it as I don’t really want to support something that comes up against Twitter, my favorite application, but then I thought what the heck. The initialize method now has an option key for :api_host so you can use it with Identi.ca’s API.

Twitter::Base.new('username', 'password', :api_host => 'identi.ca/api')

Search! (0.3.4)

I decided to use my brand spanking new HTTParty gem to wrap the Twitter Search API (formerly Summize). With this update you can do some nifty searching of all those tweets. I decided to use chaining when applying the search criteria and I think it turned out kind of cool. Check out a few of the examples below.

#searches all tweets for httparty
Twitter::Search.new('httparty').each { |r| puts r.inspect }

# searches all of jnunemaker's tweets for httparty
Twitter::Search.new('httparty').from('jnunemaker').each { |r| puts r.inspect }

# searches all tweets from jnunemaker to oaknd1
Twitter::Search.new.from('jnunemaker').to('oaknd1').each { |r| puts r.inspect }

# you can also use fetch to actually just get the parsed response
# if you don't feel like iterating through the objects right away
# or if you need pagination information
Twitter::Search.new.from('jnunemaker').to('oaknd1').fetch()

If you are curious as to what all can be searched, just check out the simple spec I have for the class. Heck, you can just check out the code for the search class itself which rings in at less than 100 lines thanks to the HTTPartying I was doing.

I’m really excited about the command line interface (I have a few more ideas for it) and the search code. The new gem is on rubyforge (twitter) and github (jnunemaker-twitter). Install and enjoy!

5 Comments

  1. I just googled “twitter gem for rails” and this post came up as the top result - on the same day you wrote it! Mad SEO skillz ;)

    Can’t wait to play around with the gem, it looks to be just what I need. Installing it now to my new Mephisto blog I’m working on.

  2. John, you are an animal. A beast, some would say.

    Great work.

  3. @Ryan – Yes, yes, I know I’m hairy. Thanks for the reminder. :)

  4. Does this require Rails 2.1? I’m trying to get it working on my Mephisto installation (which has to run under 2.0.2), but whenever I require ‘twitter’, the app won’t start because it seems to be trying to load activesupport 2.1 when activesupport 2.0.2 is already loaded. Can you help me get around this?

  5. ActiveSupport 2.1 is a requirement but it would probably work fine with a lesser version. I just required 2.1 because it is installed on my system so I can verify that it works with that. Haven’t checked it with lesser versions.

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.