Twitter Ruby Gem

I been playing around with writing my own Twitter bot and client to backup my tweet feed and other natural language processing experiments. The good thing is that there are a ton of Twitter libraries in a wide variety of programming languages. At first I picked up a Java library, Twitter4J, but then quickly opted for a Ruby version instead because I just didn’t want to create large enterprise application before I can process with my Twitter timeline. As of this writing, Ruby has two good Twitter gems, Twitter4R and Twitter. I opted to use Twitter since it also has support for identi.ca micro blogging service.

Before you can get started, you need to have the Twitter gem installed.

sudo gem install twitter

Require the Twitter gem to start using it in your Ruby scripts.

require 'rubygems'
require 'yaml'
require 'twitter'

To access your Twitter account you will need to log in via your Twitter account username and password. For this example, I keep the login information in a yaml file. If you are more security conscious you might want to prompt for the password at run time or maybe simply encrypt it instead of keeping in a plain text file. That said, here is how to sign in…

tweet = YAML::load(File.open('tweet.yml'))
twitter = Twitter::Base.new(tweet['username'], tweet['password'])

That is it, to get your 100 most recent followers you can do so with the following.

twitter.followers().each { |f|
  puts f.name
}

To get the second page, the next 100 most recent followers, just add the :page argument, such as the following

twitter.followers(:page => 2).each { |f|
  puts f.name
}

The #followers method returns a list of Twitter users. The following attributes are present for users, id, name, screen_name, location, description, url, and profile_image_url. Users may also have values for the following attributes such as profile_background_color, profile_text_color, profile_link_color, friends_count, followers_count, statuses_count, status, amongst others.

The Ruby Twitter gem exposes not just your followers but it allows you to query all replies and favorites, allows you to search twitter public stream, update your location, send direct message, and update your status.

The author of this gem has posted a ton of useful examples with functional sample code on his GitHub account. That should get any butting Twitter bot author up and running. It would also be a good idea to be familiar with the public and official Twitter API since this gem maps nicely on top of that public API.

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

  1. Download Twitter Profile Images Using Ruby
  2. Jamming with Ruby YAML
  3. Twittering Tweets
  4. Scobleizing Twitter Tweets
  5. ActiveRecord: Ruby on Rails Optional

This entry was posted in Ruby, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Posted January 13, 2009 at 9:37 am | Permalink

    You can define the dependencies on the twitter gem in your own gem;)

    Nice article about this subject: What’s New in Edge Rails: Gem Dependencies

  2. Posted January 13, 2009 at 9:41 am | Permalink

    sorry wrong link (this is for rails gem dependencies) but you get the idea ;)

  3. Posted January 31, 2009 at 10:26 am | Permalink

    Hi,

    Just two weeks after you, I started working on a Twitter bot. My current progress can be seen on Twitter and identi.ca under the @logbot account.

    My goal is to launch a base framework under a F/LOSS license for ~Twitter bots everyone can easily use and extend.

    Currently the development takes place non-publicly. However, if anyone is interested to join, let me know. (That is @dagobart.) I’d just move my svn repository over to sf.net or thelike.

    –dag

  4. 5h4rk
    Posted May 6, 2009 at 8:04 pm | Permalink

    Hey, where should I put the ‘tweet.yml’ file?

  5. Posted May 6, 2009 at 11:01 pm | Permalink

    @5h4rk – You put it anywhere and access it with a relative or absolute path.

  6. Posted May 22, 2009 at 2:37 pm | Permalink

    Nice article, I’ll try to make my own ruby-twitter bot or something else. Anyway, thanks.

  7. Posted August 6, 2009 at 5:29 pm | Permalink

    Just noticed that the current Twitter gem doesn’t support identi.ca anymore. Actually, since v0.5.0. Is anybody aware of a gem that handles identi.ca?

    BTW, my Twitter/Identi.ca bot repository is at http://mb-chatbot.svn.sourceforge.net/viewvc/mb-chatbot/. If you want to use it, check out tag v0.1.2 and set the required Twitter gem version to ‘=0.4.1′, in micro_blog_connector.rb.

3 Trackbacks

  1. By Interagindo no Twitter com Ruby | Ruby Brasil on January 16, 2009 at 8:53 am

    [...] Leia o artigo, onde um pequeno tutorial mostra mostra como usar a gem para “twittar”. [...]

  2. [...] http://juixe.com/techknow/index.php/2009/01/12/twitter-ruby-gem/ [...]

  3. By Download Twitter Profile Images Using Ruby on October 27, 2009 at 10:22 pm

    [...] profile image from each Twitter user that replied to me. To access my Twitter replies I used the Twitter Ruby Gem. I am using Twitter gem version [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*