Skip to content

Instantly share code, notes, and snippets.

@bitops
Forked from mbbx6spp/t4r-client-examples.rb
Created August 28, 2011 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitops/1176429 to your computer and use it in GitHub Desktop.
Save bitops/1176429 to your computer and use it in GitHub Desktop.
Examples of using Twitter4R (Ruby gem library to access the Twitter.com REST and Search APIs).
# Below are examples of how to initialize and use a Twitter::Client object for each Twitter user
# Can initialize by passing in Hash of oauth_access information for the authenticated user driving calls
# through Twitter4R
twitter = Twitter::Client.new(:oauth_access => {
:key => ACCESS_KEY, :secret => ACCESS_SECRET })
# Can also initialize by loading in file configuration file and specifying YAML key name to use for user:
twitter = Twitter::Client.from_config("file/name/to/config.yml", "key") # YAML file will look like twitter.yml
##### STATUS APIs ######
# You can get a status by ID (old ID style, pre-snowflake update)
status = twitter.status(:get, 107786772)
# You can post a new status to Twitter as the authenticated user
status = twitter.status(:post, "Using Ruby open source project Twitter4R version 0.7.0 to post this status update /cc @t4ruby #twitter4r")
# You can delete one of the acting user's status updates like so
status = twitter.status(:delete, 107790712)
# You can reply to another status update (tweet) keeping reply-to information
status = twitter.status(:reply, :in_reply_to_status_id => 1390482942342,
:status => "@t4ruby This new v0.7.0 release is da bomb! #ruby #twitterapi #twitter4r")
# You can post latitude and longitude information with the Tweet (you can also use :place_id too).
status = twitter.status(:post, "My brand new status in all its glory here tweeted from somewhere in Africa.",
:lat => 0, :long => 0)
##### ACCOUNT APIs #######
# You can find out about your account's rate limit information with
account_info = twitter.account_info
# This returns a Twitter::RateLimitStatus with following attributes:
account_info.class.attributes # => [:remaining_hits, :hourly_limit, :reset_time_in_seconds, :reset_time]
##### BLOCK APIs ######
screen_name = 'dictionary'
twitter.block(:add, 'dictionary')
twitter.block(:remove, 'dictionary')
id = 1260061
twitter.block(:add, id)
twitter.block(:remove, id)
user = Twitter::User.find(id, client)
twitter.block(:add, user)
twitter.block(:remove, user)
##### FAVORITES APIs ######
id = 126006103423
twitter.favorite(:add, id)
twitter.favorite(:remove, id)
status = Twitter::Status.find(id, client)
twitter.favorite(:add, status)
twitter.favorite(:remove, status)
# Now favorites...
statuses = client.favorites(:page => 2)
##### FRIENDING API ######
screen_name = 'dictionary'
twitter.friend(:add, 'dictionary')
twitter.friend(:remove, 'dictionary')
id = 1260061
twitter.friend(:add, id)
twitter.friend(:remove, id)
user = Twitter::User.find(id, client)
twitter.friend(:add, user)
twitter.friend(:remove, user)
##### DIRECT MESSAGING API #####
twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 'myfriendslogin')
twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', user)
message = @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 1234567890)
puts message.id, message.recipient, message.sender, message.text, message.geo, message.created_at
##### SEARCH APIs #####
twitter.search(:q => "twitter4r")
# various other options available too...TODO. In meantime, ask @SusanPotter questions
##### TIMELINE APIs #####
twitter.timeline_for(:user, :id => "SusanPotter", :since_id => 107244390145204224) do |tweet|
# do something interesting with each tweet
end
# various other options available too...TODO. In meantime, ask @SusanPotter questions
##### TRENDS APIs #####
trends = twitter.trends # by default gets global trends
puts trends.as_of
trends.each { |t| puts t.name }
# Other goodies too, but too tired to add examples now:)
# You usually need to configure the Twitter::Client object before invoking REST API calls through the library
# Note: if you are configuring Twitter4R to use in a Rails (v >= 2.3.x) application then add your configuration
# overrides to a twitter4r.rb initializer under config/initializer directory (or equivalent if you override its
# path in your web app).
Twitter::Client.configure do |conf|
# We can set Twitter4R to use <tt>:ssl</tt> or <tt>:http</tt> to connect to the Twitter API.
# Defaults to <tt>:ssl</tt>
conf.protocol = :ssl
# We can set Twitter4R to use another host name (perhaps for internal
# testing purposes).
# Defaults to 'twitter.com'
conf.host = 'twitter.com'
# We can set Twitter4R to use another port (also for internal
# testing purposes).
# Defaults to 443
conf.port = 443
# We can set proxy information for Twitter4R
# By default all following values are set to <tt>nil</tt>.
conf.proxy_host = 'myproxy.host'
conf.proxy_port = 8080
conf.proxy_user = 'myuser'
conf.proxy_pass = 'mypass'
# proxy protocol to use. Defaults to :http.
conf.proxy_protocol = :http
# OAuth settings for your application and the Twitter REST API URIs
conf.oauth_consumer_token = "<YOUR APP'S CONSUMER TOKEN HERE>"
conf.oauth_consumer_secret = "<YOUR APP'S CONSUMER SECRETE HERE>"
# You shouldn't need to change the default URIs for the Twitter REST API unless Twitter changes them
conf.oauth_request_token_path = "/oauth/request_token2"
conf.oauth_access_token_path = "/oauth/access_token2"
conf.oauth_authorize_path = "/oauth/authorize"
# path to prefix URIs of REST API calls. Defaults to "".
conf.path_prefix = "/v1"
# :http, :https or :ssl supported. :ssl is an alias for :https. Defaults to :ssl
conf.search_protocol = :ssl
# port to connect to for the Twitter Search service. Defaults to 443.
conf.search_port = 443
# path to prefix URIs of Search API calls. Defaults to "".
conf.search_path_prefix = ""
# Set timeout in seconds for HTTP requests
conf.timeout = 15
# Set OAuth consumer token and secret in configuration
conf.oauth_consumer_token = "<the OAuth consumer token for your application>"
conf.oauth_consumer_secret - "<the OAuth consumer secret for your application>"
end

Assumes twitter4r version >= 0.5.0 is installed for the gemset and Ruby version being used, then:

  1. Setup a ${HOME}/.twitter.yml file with a format like that found in twitter.yml example below.
  2. In your shell startup script (e.g. ${HOME}/.bashrc, ${HOME}/.zshrc, etc) set and export the variable T4R_ENV such that it is set to the name of the environment you want to use by default specified in ${HOME}/.twitter.yml. e.g. test, production.
  3. Now anytime you want to test the Twitter4R API in the console just type the following in your shell: t4rsh
test:
login: test_user_screen_name_here
oauth_consumer:
key: ZZZZZZZZZZZZZZZZZ
secret: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
oauth_access:
key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
secret: WWWWWWWWWWWWWWWWWWWWWWWWWWW
production:
login: prod_user_screen_name_here
oauth_consumer:
key: ZZZZZZZZZZZZZZZZZ
secret: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
oauth_access:
key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
secret: WWWWWWWWWWWWWWWWWWWWWWWWWWW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment