A Ruby wrapper for 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 is maintained as a separate project:
gem install t
You should follow @gem on Twitter for announcements and updates about this library.
Please direct questions about the library to the mailing list.
Does your project or organization use this gem? Add it to the apps wiki!
The Active Support dependency has been removed!
The Twitter::Client#follow
and Twitter::Client#unfollow
methods now accept
multiple users as arguments and return an array instead of a Twitter::User
.
Additionally, the Twitter::Client#follow
method now checks to make sure the
user isn't already being followed. If you don't wish to perform that check
(which requires an extra HTTP request), you can use the new
Twitter::Client#follow!
method instead. Note: This may re-send an email
notification to the user, even if they are already being followed.
This version introduces an identity map, which ensures that the same objects only get initialized once:
Twitter.user("sferik").object_id == Twitter.user("sferik").object_id #=> true
(In all previous versions of this gem, this statement would have returned false.)
The Twitter::Client#search
now returns a Twitter::SearchResult
object,
which contains metadata and a results array. In the previous major version,
this method returned an array of Twitter::Status
objects, which is now
accessible by sending the results
message to a Twitter::SearchResults
object.
# Version 2
Twitter::Client.search("query").each do |status|
puts status.full_text
end
# Version 3
Twitter::Client.search("query").results.each do |status|
puts status.full_text
end
The Twitter::Status#expanded_urls
method has been removed. Use
Twitter::Status#urls
instead.
This library is now more SOLID! In the previous version, the Twitter::Cursor
class violated the Liskov substitution principle. This came back to bite
us when trying to implement the identity map. We regret the error.
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 Oj.
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").results.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).results.first.text
The search result object returned by Twitter::Client#search
includes some metadata about the search
results:
Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").max_id => 28857935752
The max_id
attribute can be used in your next search query as the :since_id
parameter to only return newer
tweets.
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"
Twitter.gateway = YOUR_GATEWAY_HOSTNAME # e.g 'gateway.example.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 fixing issues
- by reviewing patches
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. 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 repository.
- Create a topic branch.
- Add specs for your unimplemented feature or bug fix.
- Run
bundle exec rake spec
. If your specs pass, return to step 3. - Implement your feature or bug fix.
- Run
bundle exec rake spec
. If your specs fail, return to step 5. - Run
open coverage/index.html
. If your changes are not completely covered by your tests, return to step 3. - Add documentation for your feature or bug fix.
- Run
bundle exec rake yard
. If your changes are not 100% documented, go back to step 8. - Add, commit, and push your changes.
- Submit a pull request.
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.