Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
620 lines (511 sloc) 19.3 KB

The Twitter Ruby Gem

Gem Version Build Status Dependency Status Code Climate Coverage Status Gittip

A Ruby interface to the Twitter API.


gem install twitter


Looking for the Twitter command-line interface? It was removed from this gem in version 0.5.0 and now exists as a separate project.




You should follow @gem on Twitter for announcements and updates about this library.

Mailing List

Please direct questions about this library to the mailing list.

Apps Wiki

Does your project or organization use this gem? Add it to the apps wiki!

What's New in Version 5?


Global configuration has been removed, as it was not threadsafe. Instead, you can configure a Twitter::REST::Client by passing it a block when it's initialized.

client = do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"

Note: oauth_token has been renamed to access_token and oauth_token_secret is now access_token_secret to conform to the terminology used in Twitter's developer documentation.

Streaming (Experimental)

This library now offers support for the Twitter Streaming API. We previously recommended using TweetStream for this, however TweetStream does not work on Ruby 2.0.0.

Site Streams are restricted to whitelisted accounts. To apply for access, follow the steps in the Site Streams documentation. User Streams do not require prior approval.

Unlike the rest of this library, this feature is not well tested and not recommended for production applications. That said, if you need to do Twitter streaming on Ruby 2.0.0, this is probably your best option. I've decided to ship it as an experimental feature and make it more robust over time. Patches in this area are particularly welcome.

Hopefully, by the time version 6 is released, this gem can fully replace TweetStream, em-twitter, twitterstream, and twitter-stream. Special thanks to Steve Agalloco, Tim Carey-Smith, and Tony Arcieri for helping to develop this feature.

Configuration works just like Twitter::REST::Client

client = do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"

Stream mentions of coffee or tea

topics = ["coffee", "tea"]
client.filter(:track => topics.join(",")) do |object|
  puts object.text if object.is_a?(Twitter::Tweet)

Stream a random sample of all tweets

client.sample do |object|
  puts object.text if object.is_a?(Twitter::Tweet)

Stream tweets, events, and direct messages for the authenticated user

client.user do |object|
  case object
  when Twitter::Tweet
    puts "It's a tweet!"
  when Twitter::DirectMessage
    puts "It's a direct message!"
  when Twitter::Streaming::StallWarning
    warn "Falling behind!"

An object may be one of the following:

  • Twitter::DirectMessage
  • Twitter::Streaming::DeletedTweet
  • Twitter::Streaming::Event
  • Twitter::Streaming::FriendList
  • Twitter::Streaming::StallWarning
  • Twitter::Tweet


The Twitter::Cursor class has been completely redesigned with a focus on simplicity and performance.

Notes Version 4 Version 5
Are you at the start of the cursor?
Return your most recent follower.
Return an array of all your friends.
Collect your 20 most recent friends.
Collect your 20 most recent friends twice.
friends = client.friends
2.times.collect do
friends = client.friends
2.times.collect do

In the examples above, n varies with the number of people the authenticated user follows on Twitter. This resource returns up to 20 friends per HTTP GET, so if the authenticated user follows 200 people, calling client.friends.take(20) would make 11 HTTP requests in version 4. In version 5, it makes just 1 HTTP request. Keep in mind, eliminating a single HTTP request to the Twitter API will reduce the latency of your application by about 500 ms.

The last example might seem contrived ("Why would I call client.friends.take(20) twice?") but it applies to any Enumerable method you might call on a cursor, including: #all?, #collect, #count, #each, #inject, #max, #min, #reject, #reverse_each, #select, #sort, #sort_by, and #to_a. In version 4, each time you called one of those methods, it would perform n+1 HTTP requests. In version 5, it only performs those HTTP requests the first time any one of those methods is called. Each subsequent call fetches data from a cache.

The performance improvements are actually even better than the table above indicates. In version 5, calling Twitter::Cursor#each (or any Enumerable method) starts yielding results immediately and continues yielding as each response comes back from the server. In version 4, #each made a series of requests and waited for the last one to complete before yielding any data.

Here is a list of the interface changes to Twitter::Cursor:

  • #all has been replaced by #to_a.
  • #last has been replaced by #last?.
  • #first has been replaced by #first?.
  • #first now returns the first element in the collection, as prescribed by Enumerable.
  • #collection and its aliases have been removed.

Search Results

The Twitter::SearchResults class has also been redesigned to have an Enumerable interface. The #statuses method and its aliases (#collection and #results) have been replaced by #to_a. Additionally, this class no longer inherits from Twitter::Base. As a result, the #[] method has been removed.

Trend Results

The #trends method now returns an Enumerable Twitter::TrendResults object instead of an array. This object provides methods to determinte the recency of the trend (#as_of), when the trend started (#created_at), and the location of the trend (#location). This data was previously unavailable.

Geo Results

Similarly, the #reverse_geocode, #geo_search, and #similar_places methods now return an Enumerable Twitter::GeoResults object instead of an array. This object provides access to the token to create a new place (#token), which was previously unavailable.


The Twitter::Tweet object has been cleaned up. The following methods have been removed:

  • #from_user
  • #from_user_id
  • #from_user_name
  • #to_user
  • #to_user_id
  • #to_user_name
  • #profile_image_url
  • #profile_image_url_https

These attributes can be accessed via the Twitter::User object, returned through the #user method.


The Twitter::User object has also been cleaned up. The following aliases have been removed:

  • #favorite_count (use #favorites_count)
  • #favoriters_count (use #favorites_count)
  • #favourite_count (use #favorites_count)
  • #favouriters_count (use #favorites_count)
  • #follower_count (use #followers_count)
  • #friend_count (use #friends_count)
  • #status_count (use #statuses_count)
  • #tweet_count (use #tweets_count)
  • #update_count (use #tweets_count)
  • #updates_count (use #tweets_count)
  • #translator (use #translator?)

Remove British English aliases

Earlier versions of this library aliased favourites to favorites. These aliases have been removed. Ruby is implemented in American English. The initialize method is spelled with a "z", not an "s", and Ruby provides no alias. Likewise, this library does not provide aliases for Commonwealthers. Merica. :us:

More natural method names

All create, destroy, add, and remove methods have been renamed to put the verb at the beginning:

  • #direct_message_create is now #create_direct_message
  • #direct_message_destroy is now #destroy_direct_message
  • #list_create is now #create_list
  • #list_destroy is now #destroy_list
  • #list_remove_member is now #remove_list_member
  • #list_remove_members is now #remove_list_members
  • #list_add_member is now #add_list_member
  • #list_add_members is now #add_list_members
  • #lists_owned is now #owned_list
  • #saved_search_create is now #create_saved_search
  • #saved_search_destroy is now #destroy_saved_search
  • #status_destroy is now #destroy_status


The Twitter::Error::ClientError and Twitter::Error::ServerError class hierarchy has been removed. All errors now inherit directly from Twitter::Error.

Null Objects

In version 4, methods you would expect to return a Twitter object would return nil if that object was missing. This may have resulted in a NoMethodError. To prevent such errors, you may have introduced checks for the truthiness of the response, for example:

status = client.status(55709764298092545)
  # Do something with the Twitter::Place object
elsif status.geo
  # Do something with the Twitter::Geo object

In version 5, all such methods will return a Twitter::NullObject instead of nil. This should prevent NoMethodError but may result in unexpected behavior if you have truthiness checks in place, since everything is truthy in Ruby except false and nil. For these cases, there are now predicate methods:

status = client.status(55709764298092545)
  # Do something with the Twitter::Place object
elsif status.geo?
  # Do something with the Twitter::Geo object

URI Methods

The Twitter::List, Twitter::Tweet, and Twitter::User objects all have a #uri method, which returns an HTTPS URI to This clobbers the Twitter::List#uri method, which previously returned the list URI's path (not a URI).

These methods are aliased to #url for users who prefer that nomenclature. Twitter::User previously had a #url method, which returned the user's website. This URI is now available via the #website method.

All #uri methods now return Addressable::URI objects instead of strings. To convert an Addressable::URI object to a string, call #to_s on it.


Twitter API v1.1 requires you to authenticate via OAuth, so you'll need to register your application with Twitter. Once you've registered an application, make sure to set the correct access level, otherwise you may see the error:

Read-only application cannot POST

Your new application will be assigned a consumer key/secret pair and you will be assigned an OAuth access token/secret pair for that application. You'll need to configure these values before you make a request or else you'll get the error:

Bad Authentication data

You can pass configuration options as a block to

client = do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"

After configuration, requests can be made like so:

client.update("I'm tweeting with @gem!")


The Faraday middleware stack is fully configurable and is exposed as a Faraday::RackBuilder object. You can modify the default middleware in-place:

client.middleware.insert_after Twitter::Response::RaiseError, CustomMiddleware

A custom adapter may be set as part of a custom middleware stack:

client.middleware = do |faraday|
  # Specify a middleware stack here
  faraday.adapter :some_other_adapter

Usage Examples

All examples require an authenticated Twitter client. See the section on configuration.

Tweet (as the authenticated user)

client.update("I'm tweeting with @gem!")

Follow a user (by screen name or user ID)


Fetch a user (by screen name or user ID)


Fetch a cursored list of followers with profile details (by screen name or user ID, or by implicit authenticated user)


Fetch a cursored list of friends with profile details (by screen name or user ID, or by implicit authenticated user)


Fetch a collection of user_ids that the currently authenticated user does not want to receive retweets from


Fetch the timeline of Tweets by a user


Fetch the timeline of Tweets from the authenticated user's home page


Fetch the timeline of Tweets mentioning the authenticated user


Fetch a particular Tweet by ID


Collect the 3 most recent marriage proposals to @justinbieber"to:justinbieber marry me", :count => 3, :result_type => "recent").take(3).collect do |tweet|
  "#{tweet.user.screen_name}: #{tweet.text}"

Find a Japanese-language Tweet tagged #ruby (excluding retweets)"#ruby -rt", :lang => "ja").first.text

For more usage examples, please see the full documentation.

Object Graph

Entity-relationship diagram

This entity-relationship diagram is generated programatically. If you add or remove any Twitter objects, please regenerate the ERD with the following command:

bundle exec rake erd

Supported Ruby Versions

This library aims to support and is tested against the following Ruby versions:

  • Ruby 1.8.7
  • Ruby 1.9.2
  • Ruby 1.9.3
  • Ruby 2.0.0
  • Ruby 2.1.0

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.


This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:

spec.add_dependency 'twitter', '~> 5.0'


Copyright (c) 2006-2013 Erik Michaels-Ober, John Nunemaker, Wynn Netherland, Steve Richert, Steve Agalloco. See LICENSE for details.

Jump to Line
Something went wrong with that request. Please try again.