The Twitter Ruby Gem Build Status Dependency Status

A Ruby wrapper for the Twitter API.

Installation

gem install twitter

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

gem install t

Documentation

http://rdoc.info/gems/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!

What's new in version 2?

This version introduces a number of new classes, notably:

Twitter::Configuration  Twitter::List       Twitter::Polygon            Twitter::Settings
Twitter::Cursor         Twitter::Metadata   Twitter::RateLimitStatus    Twitter::Size
Twitter::DirectMessage  Twitter::Mention    Twitter::Relationship       Twitter::Status
Twitter::Favorite       Twitter::Photo      Twitter::Reply              Twitter::Suggestion
Twitter::Follow         Twitter::Place      Twitter::Retweet            Twitter::Trend
Twitter::Language       Twitter::Point      Twitter::SavedSearch        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 this library as 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:

Twitter.user("sferik").protected?

The Twitter::Search class has been replaced by the Twitter::Client#search method. This unifies the library's interfaces and will make the code easier to maintain over time. As a result, you can no longer build queries by chaining methods (ARel-style). The new syntax is more consistent and concise.

This version also introduces object equivalence, so objects that are logically equivalent are considered equal, even if they don't occupy the same address in memory, for example:

Twitter.user("sferik") == Twitter.user("sferik") #=> true
Twitter.user("sferik") == Twitter.user(7505382) #=> true

In previous versions of this gem, both of the above statements would have returned false. We've stopped short of implementing a true identity map, such that:

Twitter.user("sferik").object_id == Twitter.user("sferik").object_id

A true identity map may be implemented in future versions of this library.

Additional Notes

  • All deprecated methods have been removed.
  • Twitter::Client#totals has been removed. Use Twitter::Client#user instead.
  • Twitter.faraday_options has been renamed to Twitter.connection_options.
  • Twitter::Client#friendships now takes up to 3 arguments instead of 1.
  • Support for the XML response format has been removed. This decision was guided largely by Twitter, which has started removing XML responses available for some resources. This allows us to remove the gem's dependency on multi_xml. Using JSON is faster than XML, both in terms of parsing speed and time over the wire.
  • 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.

Performance

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

Twitter.user("sferik").location

Return @sferik's most recent Tweet

Twitter.user_timeline("sferik").first.text

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

Twitter.status(27558893223).text

Find the 3 most recent marriage proposals to @justinbieber

Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").map do |status|
  "#{status.from_user}: #{status.text}"
end

Let's find a Japanese-language Tweet tagged #ruby (no retweets)

Twitter.search("#ruby -rt", :lang => "ja", :rpp => 1).first.text

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
end

Update your status

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

Read the most recent Tweet in your timeline

Twitter.home_timeline.first.text

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'

Contributing

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 issue 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 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 Ruby Versions

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.