Code Monkey home page Code Monkey logo

twitter's Introduction

The Twitter Ruby Gem

A Ruby wrapper for the Twitter REST and Search APIs

Does your project or company use this gem?

Add it to the apps wiki!

What's new in 1.0?

This gem has been completely rewritten for version 1.0 thanks to contributions from numerous people. This rewrite breaks compatibility with version 0.9.12 and earlier versions of the gem. Most notably, the Twitter::Base, Twitter:Geo, Twitter::LocalTrends, and Twitter::Trends classes have all been merged into the Twitter::Client class. Whenever possible, we display deprecation warnings and forward method calls to the Twitter::Client class. In a handful of cases, method names were changed to resolve namespace conflicts.

  • Pre-1.0 Twitter::Base.new.user("sferik").name
  • Post-1.0 Twitter::Client.new.user("sferik").name

The Twitter::Search class has remained largely the same, however it no longer accepts a query in its constructor. You can specify a query using the #containing method, which is aliased to #q.

  • Pre-1.0 Twitter::Search.new("query").fetch.first.text
  • Post-1.0 Twitter::Search.new.q("query").fetch.first.text

The error classes have gone through a transformation to make them consistent with Twitter's documented response codes. These changes should make it easier to rescue from specific errors and take action accordingly. We've also added support for two new classes of error returned by the Twitter Search API.

Response Code Pre-1.0 Post-1.0
400 Twitter::RateLimitExceeded Twitter::BadRequest
401 Twitter::Unauthorized Twitter::Unauthorized
403 Twitter::General Twitter::Forbidden
404 Twitter::NotFound Twitter::NotFound
406 N/A Twitter::NotAcceptable
420 N/A Twitter::EnhanceYourCalm
500 Twitter::InformTwitter Twitter::InternalServerError
502 Twitter::Unavailable Twitter::BadGateway
503 Twitter::Unavailable Twitter::ServiceUnavailable

The Twitter::OAuth class has been removed. This class was just a wrapper to get access tokens via the oauth gem. Given that there are a variety of gems that do the same thing (twitter-auth, omniauth, and devise, to name a few) we decided to decouple this functionality so you can use the authentication library of your choosing, or none at all. If you would like to continue using the oauth gem, simply require it and make the following changes:

  • Pre-1.0 options = {:api_endpoint => "http://api.twitter.com", :signing_endpoint => "http://api.twitter.com"} oauth_wrapper = Twitter::OAuth.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, options) oauth_wrapper.set_callback_url(CALLBACK_URL) signing_consumer = oauth_wrapper.signing_consumer request_token = signing_consumer.get_request_token redirect_to request_token.authorize_url oauth_wrapper.authorize_from_request(request_token.token, request_token.secret, params[:oauth_verifier])

  • Post-1.0 options = {:site => "http://api.twitter.com", :request_endpoint => "http://api.twitter.com"} signing_consumer = OAuth::Consumer.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, options) request_token = signing_consumer.get_request_token(:oauth_callback => CALLBACK_URL) redirect_to request_token.authorize_url OAuth::RequestToken.new(signing_consumer, request_token.token, request_token.secret).get_access_token(:oauth_verifier => params[:oauth_verifier])

The public APIs defined in version 1.0 of this gem will maintain backwards compatibility until the next major version, following the best practice of Semantic Versioning. You are free to continue using the 0.9 series of the gem, however it will not be maintained, so upgrading to 1.0 is strongly recommended.

Here are a few more reasons to upgrade to 1.0:

  • Ruby 1.9 compatibility: All code and specs now work in the latest version of Ruby
  • Support for HTTP proxies: Access Twitter from from China, Iran, or inside your office firewall
  • Support for multiple HTTP adapters: NetHttp (default), Typhoeus, Patron, or ActionDispatch
  • Support for multiple request formats: JSON (default) or XML
  • More flexible: Parse JSON or XML with the engine or your choosing via MultiJSON and MultiXML
  • More RESTful: Use HTTP DELETE (instead of POST) when calling destructive resources
  • More methods: Request any documented resource in the Twitter API
  • Send all requests over SSL: Faster and more secure
  • Improved error handling: More easily retry after rate-limit errors or fail whales

For more information, please see the full documentation and examples of the gem's usage below.

Help! I'm getting: "Did not recognize your engine specification. Please specify either a symbol or a class. (RuntimeError)"

If you're using the JSON request format (i.e., the default), you'll need to explicitly require a JSON library. We recommend yajl-ruby. If you're using the XML request format, we recommend requiring libxml-ruby for dramatically improved performance over REXML.

Documentation

http://rdoc.info/gems/twitter

Usage Examples

require "rubygems"
require "twitter"

# Get a user's location
puts Twitter.user("sferik").location

# Get a user's most recent status update
puts Twitter.user_timeline("sferik").first.text

# Get a status update by id
puts Twitter.status(27558893223).text

# Initialize a Twitter search
search = Twitter::Search.new

# Find the 3 most recent marriage proposals to @justinbieber
search.containing("marry me").to("justinbieber").result_type("recent").per_page(3).each do |r|
  puts "#{r.from_user}: #{r.text}"
end

# Enough about Justin Bieber
search.clear

# Let's find a Japanese-language status update tagged #ruby
puts search.hashtag("ruby").language("ja").no_retweets.per_page(1).fetch.first.text

# And another
puts search.fetch_next_page.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

# Initialize your Twitter client
client = Twitter::Client.new

# Post a status update
client.update("I just posted a status update via the Twitter Ruby Gem!")

# Read the most recent status update in your home timeline
puts client.home_timeline.first.text

# Who's your most popular friend?
puts client.friends.sort{|a, b| a.followers_count <=> b.followers_count}.reverse.first.name

# Who's your most popular follower?
puts client.followers.sort{|a, b| a.followers_count <=> b.followers_count}.reverse.first.name

# Get your rate limit status
puts client.rate_limit_status.remaining_hits.to_s + " Twitter API request(s) remaining this hour"

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:rcov. If your changes are not 100% covered, go back to step 6.
  8. 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.)

Copyright

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

twitter's People

Contributors

sferik avatar jnunemaker avatar pengwynn avatar laserlemon avatar zmoazeni avatar dcrec1 avatar secobarbital avatar cyu avatar danielmorrison avatar kevn avatar billymeltdown avatar dustin avatar duncan avatar queso avatar pope avatar ivey avatar urajat avatar rizwanreza avatar stve avatar boz avatar cdb avatar chris avatar danicuki avatar duylam avatar vangberg avatar jacqui avatar ja avatar coderifous avatar jschairb avatar crafterm avatar

Stargazers

延 avatar

Watchers

延 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.