A Ruby wrapper for the Twitter API.
gem install twitter
You should follow @gem on Twitter for announcements and updates about the gem.
Please direct any questions about the library to the mailing list.
Does your project or organization use this gem? Add it to the apps wiki!
This version introduces 11 new classes:
Twitter::Cursor
Twitter::DirectMessage
Twitter::List
Twitter::Photo
Twitter::Place
Twitter::Point
Twitter::Polygon
Twitter::SavedSearch
Twitter::Size
Twitter::Status
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:
Twitter.user("sferik").protected?
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
status.hashtags
# Return all URLs in the Tweet
status.urls
# Return all users mentioned in the Tweet
status.user_mentions
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.
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
search = Twitter::Search.new
search.containing("marry me").to("justinbieber").result_type("recent").per_page(3).map do |status|
"#{status["from_user"]}: #{status["text"]}"
end
Enough about Justin Bieber. Let's find a Japanese-language tweet tagged #ruby.
search = Twitter::Search.new
search.hashtag("ruby").language("ja").no_retweets.per_page(1).fetch.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"
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.
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.
- Fork the project.
- Create a topic branch.
- Implement your feature or bug fix.
- Add documentation for your feature or bug fix.
- Run bundle exec rake doc:yard. If your changes are not 100% documented, go back to step 4.
- Add specs for your feature or bug fix.
- Run bundle exec rake spec. If your changes are not 100% covered, go back to step 6.
- Commit and push your changes.
- 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.)
This library aims to support and is tested against the following Ruby implementations:
- Ruby 1.8.7
- Ruby 1.9.1
- Ruby 1.9.2
- JRuby
- Rubinius
- Ruby Enterprise Edition
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.