Ruby Twitter Gem

November 26th, 2006

Well, I’ve been using this relatively new service called Twitter for a few weeks and have really enjoyed it. You can get an idea of what your friends are doing and update them on what your doing from IM, the web or your phone. It’s been a little while since my last api wrapper so I decided to make one for twitter.

Command Line

I have felt inspired of late by all the command line gems, so this twitter library does more than just wrap the twitter api. It also uses it! You can post updates to your twitter and check in on your friends all from your fav terminal app.

$ sudo gem install hpricot
$ sudo gem install twitter

Then you can post updates…

$ twitter post "watching veronica mars"

check in on your friends…

$ twitter friends

or even check in on one friend at a time…

$ twitter friend jnunemaker

Pretty sweet, eh? You can check out the rest of the current functionality in the docs or on the rubyforge project page.

Update: There seems to be an issue with parsing elements named ‘text’ using Hpricot 0.5+. For now, just uninstall all 0.5 versions and install 0.4.86 (sudo gem install hpricot –source http://code.whytheluckystiff.net -v 0.4.86) and everything should work fine.

Update: All the hpricot issues are fixed. I’m parsing the text element in a different way.

Aug 5, 2007:Just released version 0.2.0.

47 Responses to “Ruby Twitter Gem”

  1. Wow…okay, I’m slow but it took me forever to find $HOME/.twitter to configure my credentials.

  2. @Pat - Yeah, maybe I should make a bigger note of that.

    Just for those who wander across this, you have to add a .twitter file to your home directory (~/.twitter) on a mac. If you install the gem and run twitter from your command line, it will create the file and you just have to edit it.

  3. This is a wicked awesome concept, but I keep getting the error “Twitter what?. Something went wrong and your status could not be updated.” even after updating my ~/.twitter file. Any ideas?

  4. Yeah, don’t you love generic error messages. I should probably give a more specific message so it’s easier for me to troubleshoot. I’ll IM with you and we’ll get it figured out. I’ll post back here what the problem was so everyone can benefit.

  5. Hey John, I added this to the Twitter Fan wiki! Nice work!

  6. @Chris - Sweet! I didn’t even know that existed. Thanks for adding it. Let me know if you have any ideas for improving it. I have a few myself, but I’m interested in what others have to say.

  7. Yeah, seeing those same errors. Sometimes (usually during a post) it will work and also print the error. I’ve been having twitter issues with their IM interface tonight as well, so it might be on their end.

  8. Figured out the issue most people are having. It has to do with the version of hpricot available on rubyforge. If twitter is not working for you from the command line just install hpricot from why’s website like so:

    sudo gem install hpricot --source code.whytheluckystiff.net
  9. Aha! Thanks for tracking that down. I was getting tired of looking at tcpflow and trying to figure out wtf was going on — mainly because I just didn’t know what I was supposed to be looking for. My journey to management land has put a big dent in my debugging skills…

    Hilariously enough, as soon as I get the latest hpricot, twitter is down.

  10. @Pat - no problem.

  11. avatar Jestre December 11th, 2006 9:28 pm

    I’ve signed up for Twitter tonight, and as I am in a term window all day, this looks like the perfect tool for updating it. I’m on Ubuntu and after several misstarts due to missing libs/apps (ruby-dev, rdoc, etc), I finally got it to seemingly build, though I still get the following error. Can this be ignored, or am I still missing something?

    Building native extensions. This could take a while…

    ext/hpricot_scan/hpricot_scan.rl: In function ‘hpricot_scan’:

    ext/hpricot_scan/hpricot_scan.rl:306: warning: control reaches end of non-void function

    This is with ruby 1.8x

  12. @Jestre - install the hpricot from why’s site using sudo gem install hpricot –source code.whytheluckystiff.net

    That might fix your problem. If you were already installing it that way, then you might want to check the Hpricot website for possible bugs on linux.

  13. avatar Jestre December 12th, 2006 10:13 am

    Thanks, I opened a ticket on Why’s site re: these as it occurred on my Dapper at work today as well. Everything seems to be working, so they appear minor. The only related error was from a group of folks who did not have the build-essential package installed, and who therefore could not build the .so file.

    Thanks for the awesome little app!

  14. […] There’s a small ecosystem springing up around Twitter. There are Ruby command line tools (sadly, I couldn’t get them to work because Ruby Gems is messed up on OS 10.4), Python/Growl notifiers (works, but high maintenance), and shiny Mac tools like twitterific. Someone has even written an Applescript Quicksilver action, Tweet, for posting. There are still a few things on my wishlist: 1) for Twitterific to both have it’s little history window and to deliver changes via Growl, and 2) to be able to set my status in Adium, Snak, and Skype based on Tweets. […]

  15. Well, probably pilot error, but even after installing Why’s latest hpricot and editing in my credentials in .twitter, no joy on OS X this morning. :(

  16. Hi. Nice work here, but i can’t use it on my fedora machine.
    I’ve installed hpricot and i keep getting the same error.

    [moura@yoda ~]$ sudo gem install hpricot –source http://code.whytheluckystiff.net
    Select which gem to install for your platform (i386-linux)
    1. hpricot 0.5.110 (mswin32)
    2. hpricot 0.5.110 (ruby)
    3. hpricot 0.5.110 (jruby)
    4. hpricot 0.5 (ruby)
    5. hpricot 0.5 (mswin32)
    6. hpricot 0.5 (mswin32)
    7. hpricot 0.5 (ruby)
    8. Skip this gem
    9. Cancel installation
    > 7
    Building native extensions. This could take a while…
    Successfully installed hpricot-0.5
    Installing ri documentation for hpricot-0.5…
    Installing RDoc documentation for hpricot-0.5…
    [moura@yoda ~]$ twitter timeline

    Something went wrong!!!

    Most likely: The twitter gem requires hpricot version >= 0.4.59. Check to make sure that you have at least that version installed. To install the newest version of hpricot:

    sudo gem install hpricot –source http://code.whytheluckystiff.net
    [moura@yoda ~]$

    Help?

  17. Did you fill out your .twitter file in your home directory with your username and password?

  18. In my case, yes - .twitter is filled out; tried both email and username with no luck. Same credentials working fine from Twitterific. Tried both hpricot 0.5 and 0.5.110.

  19. @John: yes. i did fill the .twitter file … i guess maybe it’s a problem with hpricot or something …

  20. @Rui - What version of Ruby are you running? If you aren’t on ruby 1.8.4 or 1.8.5, then I’m not sure what could be going wrong.

    Also, I would check _why’s site for hpricot/fedora issues.

  21. @John - I am on 1.8.5 - are you saying that’s a known problem?

  22. @Mike - No, not saying, I was just curious if it was something off older. As far as I can tell it’s something with hpricot on linux. That’s just a hunch as all the mac users are fine.

  23. @John - Well, not all :) as I’m on Mac. Latest hpricot from Why’s site, Ruby 1.8.5, fresh install of the twitter gem itself. But it’s entirely possible I boogered something up building up a Rails dev environment from source, or doing something equally silly; Mac is not yet my native land. Anyhow, if you want to chase this more we can, or we can chalk it up to one silly person who has some bizarre unreplicable issue.

  24. @Mike - I’m on a Mac as well and can’t get it to work. I think the underlying problem is that the native portion of the hpricot install fails silently, making it appear as though hpricot has been installed successfully. I’ve tried a few different hpricot versions with similar results.

  25. hpricot itself appears to be installed fine - for example, I can run the code from http://code.whytheluckystiff.net/hpricot/wiki without any problem.

  26. Another mac user that is not able to get this to function.

    hpricot (0.5.110) and twitter (0.0.4) installed.

    I had to manually add the .twitter file, and it didn’t set up the settings. You can contact me if you need me to test something.

  27. I finally got this working by installing an older version of hpricot (hpricot-0.4.59) later versions didnt work

  28. @memespring - Hmm…just noticed that it does the same thing for me when I upgraded to the latest hpricot. Something must have changed. I’ll look into it. I want to use hpricot because it’s pretty cool, but maybe it’s time to fall back on simplexml or something.

  29. Ding, ding, ding: Ticket 55. Hpricot doesn’t like text xml elements. Sounds like it is fixed in the latest trunk. I’ll try to hunt it down and figure out what is the deal.

  30. For now, uninstall all 0.5 versions and install 0.4.86 (sudo gem install hpricot -v 0.4.86) and everything should work fine.

    You can see which versions you have installed by calling ‘gem search hpricot’. Then uninstall 0.5+ versions by calling ’sudo gem uninstall hpricot -v 0.5.110′ replacing 0.5.110 with whatever 0.5 version you have installed.

  31. Thanks, John. That nailed it.

  32. Not sure if this is a Ruby Twitter gem issue or not, but I am trying to use this to display a timeline on my Desktop with GeekTool. The commands work fine from the command line, e.g. twitter timeline actually gives me a timeline, but running the same command in GeekTool does not produce the same output. Anyone have any suggestions?

  33. So I’m still having problems (OS X 10.4.9, Ruby 1.8.6, hpricot 0.4.86) — post and timeline work fantastically but friend(s) and follower(s) fail with “Something went wrong!!!”

  34. Same here and it was working for a day or so, then it puked. It suggested the following:

    “Most likely: The twitter gem requires hpricot version >= 0.4.59. Check to make sure that you have at least that version installed.”

    It posts successfully, though it claims it didn’t. Nothing else works. Its seems to have failed in stages…first went the ‘friends’ command, then the ‘post’ command. Its all going to hell in a hand-basket I say! lol

    I too am willing to test things as needed…

  35. I’ll fix it this weekend. I’m 99% sure it’s the relative_created_at field being removed. I just need to update the gem. I noticed it today as well.

  36. […] # to make it work: # 1. install ruby on your machine; # 2. install the gems specified here http://addictedtonew.com/archives/187/ruby-twitter-gem/ : # gem install hpricot –source http://code.whytheluckystiff.net -v 0.4.86 # gem install twitter # 3. change the line s = Twitter::Base.new(’my_email’, ‘my_password’).timeline(:user)[0] # by entering your login credentials for the twitter account # 4. start yahoo messenger # 5. run the script […]

  37. Hi, thanks for a great gem.
    Thanks to it, it took me half an hour to create a (Windows only) Yahoo Messenger Twitter updater

    Hope people will find it useful as well. Feedback is welcome!

  38. avatar spp May 7th, 2007 12:30 pm

    There is also a Twitter4R which provides a Pure Ruby interface to Twitter instead of just command-line.

    See http://twitter4r.rubyforge.org for details.

  39. @spp - The ruby twitter gem is a pure ruby interface. The command line client is just a bonus written on top of it.

  40. avatar Ryan May 18th, 2007 10:21 pm

    I posted this on the Google group, but might as well mention it here: You don’t have to downgrade to an earlier (non-broken) hpricot. Just change line 12 in status.rb in the twitter gem to use: get_elements_by_tag_name(’text’) instead of at(’text’) and it will work.

    s.text =
    (xml).get_elements_by_tag_name(’text’).innerHTML

    You’ll have to do a similar change in direct_message.rb on line 13 or so (except there it’s d.text = …).

    Hope this helps.

  41. avatar kaerast May 28th, 2007 10:30 am

    It’s not doing any error handling what so ever, meaning the frequent downtimes of the Twitter servers cause horrible error messages.

    Also, I needed to gem install activesupport, you should have activesupport listed as a requirement in your gem.

  42. avatar RMB July 28th, 2007 12:05 am

    Great gem, so far so good! I can retreive direct messages which is what my app needs to do, though there doesn’t appear to be a gem method for destroying direct_messages on twitter once I’ve stored them application side.

    That would be a nice enhancement. If I’m missing something, or if you have an alternate way of doing it outside the gem, please let me know. I tried doing it with Net::HTTP.get but i got a response saying that i couldn’t be authenticated. Any help here destroying private messages would be greatly appreciated.

  43. @RMB - There is a method in the api. I haven’t actually implemented it in the gem. I’m behind on the methods because I’m swamped right now.

  44. John, thanks for the quick reply. Back on direct_messages, i was reading the Twitter Gem Doc, and you mention that you can specifcy an option to grab direct messages specifying for instance a previous retreival date/time value.

    Can you provide an example of useage? I have an http formatted date which i’m storing in a variable called ‘last_update.’ How would I include that in the direct message call? I’m trying a structure like this:

    @messages = Twitter::Base.new(’XXXX’, ‘XXXX’).direct_messages(since=Fri2C++2+Aug+2007+013A363A12+GMT)

    The date format above works when i curl the API. I think though i’m missing the syntax. Let me know please. Thanks!

  45. @RMB - @messages = Twitter::Base.new(’XXXX’, ‘XXXX’).direct_messages(”Fri2C++2+Aug+2007+013A363A12+GMT”)

    Should work like that. Just takes a string. I should make it take a date or time as well. I’ll make note of that.

  46. John, I don’t think the method is working as expected, you should test it. I pasted the example you posted above in my console and ran it with proper login credentials. I got back all direct messages, not just those posted after Auguest 2nd. Let me know if you experience the same and if you have any suggestions as to how to fix it. Thanks.

  47. How To Update Facebook, Twitter, and Kopete’s Status All At Once…

    I have whipped up a short script to update Facebook’s status, Twitter, and Kopete’s status all at once.  The Facebook portion of the code comes directly from Kerry Buckley’s script…

Leave a Reply

About This Site

Addicted to New is the personal website of John Nunemaker, a Web Developer enamored of Ruby on Rails and a wide-eyed fan of all things new and cool.