A Ruby wrapper for the Twitter REST and Search APIs
Pull request Compare This branch is 1489 commits behind sferik:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


The Twitter Ruby Gem

A Ruby wrapper for the Twitter API.


gem install twitter



Follow @gem on Twitter

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

Mailing List

Please direct any questions about the library to the mailing list.

Apps Wiki

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

Continuous Integration

Build Status

What new in version 2?

This version introduces 11 new classes:

  1. Twitter::Cursor
  2. Twitter::DirectMessage
  3. Twitter::List
  4. Twitter::Photo
  5. Twitter::Place
  6. Twitter::Point
  7. Twitter::Polygon
  8. Twitter::SavedSearch
  9. Twitter::Size
  10. Twitter::Status
  11. Twitter::User

These classes (plus Ruby primitives) have replaced all instances of Hashie::Mash. This allows us to remove the gem's dependency on hashie and eliminate a layer in the middleware stack.

This should have the effect of making object instantiation and method invocation faster and less susceptible to typos. For example, if you typed Twitter.user("sferik").loctaion, a Hashie::Mash would return nil instead of raising a NoMethodError.

Another benefit of these new objects is instance methods like created_at now return a Time instead of a String. This should make the objects easier to work with and better fulfills the promise of a Ruby wrapper for the Twitter API.

Any instance method that returns a boolean can now be called with a trailing question mark, for example:


Version 2 also includes some advanced Tweet-parsing methods, for example:

# Fetch the Tweet at https://twitter.com/twitter/statuses/76360760606986241
status = Twitter.status(76360760606986241)

# Return all hashtags in the Tweet

# Return all URLs in the Tweet

# Return all users mentioned in the Tweet

This tweet parsing is performed by twitter-text, Twitter's official text processing library, so it should be consistent with all other Twitter services.

Note: All error classes have been moved inside the Twitter::Error namespace. If you were previously rescuing Twitter::NotFound you'll need to change that to Twitter::Error::NotFound.


You can improve performance by preloading a faster JSON parsing library. By default, JSON will be parsed with okjson. For faster JSON parsing, we recommend yajl.

Usage Examples

Return @sferik's location


Return @sferik's most recent Tweet


Return the text of the Tweet at https://twitter.com/sferik/statuses/27558893223


Find the 3 most recent marriage proposals to @justinbieber

search = Twitter::Search.new
search.containing("marry me").to("justinbieber").result_type("recent").per_page(3).map do |status|
  "#{status["from_user"]}: #{status["text"]}"

Enough about Justin Bieber. Let's find a Japanese-language tweet tagged #ruby.

search = Twitter::Search.new

Certain methods require authentication. To get your Twitter OAuth credentials, register an app at http://dev.twitter.com/apps

Twitter.configure do |config|
  config.consumer_key = YOUR_CONSUMER_KEY
  config.consumer_secret = YOUR_CONSUMER_SECRET
  config.oauth_token = YOUR_OAUTH_TOKEN
  config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET

Update your status

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

Read the most recent Tweet in your timeline


Get your rate limit status

Twitter.rate_limit_status['remaining_hits'].to_s + " Twitter API request(s) remaining this hour"

Configuration for API Proxy Services

Use of API proxy services, like Apigee, can be used to attain higher rate limits to the Twitter API.

Twitter.gateway = YOUR_GATEWAY_HOSTNAME # e.g 'twitter.apigee.com'


In the spirit of free software, everyone is encouraged to help improve this project.

Here are some ways you can contribute:

  • by using alpha, beta, and prerelease versions
  • by reporting bugs
  • by suggesting new features
  • by writing or editing documentation
  • by writing specifications
  • by writing code (no patch is too small: fix typos, add comments, clean up inconsistent whitespace)
  • by refactoring code
  • by closing issues
  • by reviewing patches
  • financially

All contributors will be added to the history and will receive the respect and gratitude of the community.

Submitting an Issue

We use the GitHub issue tracker to track bugs and features. Before submitting a bug report or feature request, check to make sure it hasn't already been submitted. You can indicate support for an existing issuse by voting it up. When submitting a bug report, please include a gist that includes a stack trace and any details that may be necessary to reproduce the bug, including your gem version, Ruby version, and operating system. Ideally, a bug report should include a pull request with failing specs.

Submitting a Pull Request

  1. Fork the project.
  2. Create a topic branch.
  3. Implement your feature or bug fix.
  4. Add documentation for your feature or bug fix.
  5. Run bundle exec rake doc:yard. If your changes are not 100% documented, go back to step 4.
  6. Add specs for your feature or bug fix.
  7. Run bundle exec rake spec. If your changes are not 100% covered, go back to step 6.
  8. Commit and push your changes.
  9. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)

Supported Rubies

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

If something doesn't work on one of these interpreters, it should be considered a bug.

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

If you would like this library to support another Ruby version, 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 personally 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.


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