Used to query or post to the Twitter REST API to simplify code.

Methods
Included Modules
Attributes
[RW] login
[RW] password
Public Class methods
configure() {|@@config| ...}

Yields to given block to configure the Twitter4R API.

    # File lib/twitter/config.rb, line 65
65:       def configure(&block)
66:         raise ArgumentError, "Block must be provided to configure" unless block_given?
67:         yield @@config
68:       end
from_config(config_file, env = 'test')

Helper method mostly for irb shell prototyping.

Reads in login/password Twitter credentials from YAML file found at the location given by config_file that has the following format:

 envname:
   login: mytwitterlogin
   password: mytwitterpassword

Where envname is the name of the environment like ‘test’, ‘dev’ or ‘prod’. The env argument defaults to ‘test’.

To use this in the shell you would do something like the following examples:

 twitter = Twitter::Client.from_config('config/twitter.yml', 'dev')
 twitter = Twitter::Client.from_config('config/twitter.yml')
    # File lib/twitter/console.rb, line 22
22:       def from_config(config_file, env = 'test')
23:         yaml_hash = YAML.load(File.read(config_file))
24:         self.new yaml_hash[env]
25:       end
Public Instance methods
authenticate?(login, password)

Provides access to the Twitter verify credentials API.

You can verify Twitter user credentials with minimal overhead using this method.

Example:

 client.authenticate?("osxisforlightweights", "l30p@rd_s^cks!")
    # File lib/twitter/client/auth.rb, line 12
12:         def authenticate?(login, password)
13:     verify_credentials(login, password)
14:         end
favorite(action, value)

Provides access to the Twitter add/remove favorite API.

You can add and remove favorite status using this method.

action can be any of the following values:

The value must be either the status object to add or remove or the integer unique status ID.

Examples:

 id = 126006103423
 client.favorite(:add, id)
 client.favorite(:remove, id)
 status = Twitter::Status.find(id, client)
 client.favorite(:add, status)
 client.favorite(:remove, status)
    # File lib/twitter/client/favorites.rb, line 41
41:         def favorite(action, value)
42:           raise ArgumentError, "Invalid favorite action provided: #{action}" unless @@FAVORITES_URIS.keys.member?(action)
43:                 value = value.to_i.to_s unless value.is_a?(String)
44:                 uri = "#{@@FAVORITES_URIS[action]}/#{value}.json"
45:                 case action
46:                 when :add
47:                   response = http_connect {|conn| create_http_post_request(uri) }
48:                 when :remove
49:                   response = http_connect {|conn| create_http_delete_request(uri) }
50:                 end
51:                 bless_model(Twitter::Status.unmarshal(response.body))
52:         end
favorites(options = nil)

Provides access to the Twitter list favorites API.

You can access the authenticated [Twitter] user‘s favorites list using this method.

By default you will receive the last twenty statuses added to your favorites list. To get a previous page you can provide options to this method. For example,

 statuses = client.favorites(:page => 2)

The above one-liner will get the second page of favorites for the authenticated user.

    # File lib/twitter/client/favorites.rb, line 16
16:         def favorites(options = nil)
17:           def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
18:     uri = '/favorites.json' + uri_suffix(options)
19:     response = http_connect {|conn|     create_http_get_request(uri) }
20:     bless_models(Twitter::Status.unmarshal(response.body))
21:         end
featured(type)

Provides access to the Featured Twitter API.

Currently the only value for type accepted is :users, which will return an Array of blessed Twitter::User objects that represent Twitter‘s featured users.

    # File lib/twitter/extras.rb, line 19
19:   def featured(type)
20:     uri = @@FEATURED_URIS[type]
21:     response = http_connect {|conn| create_http_get_request(uri) }
22:     bless_models(Twitter::User.unmarshal(response.body))
23:   end
friend(action, value)

Provides access to the Twitter Friendship API.

You can add and remove friends using this method.

action can be any of the following values:

  • :add - to add a friend, you would use this action value
  • :remove - to remove an existing friend from your friends list use this.

The value must be either the user to befriend or defriend‘s screen name, integer unique user ID or Twitter::User object representation.

Examples:

 screen_name = 'dictionary'
 client.friend(:add, 'dictionary')
 client.friend(:remove, 'dictionary')
 id = 1260061
 client.friend(:add, id)
 client.friend(:remove, id)
 user = Twitter::User.find(id, client)
 client.friend(:add, user)
 client.friend(:remove, user)
    # File lib/twitter/client/friendship.rb, line 28
28:         def friend(action, value)
29:           raise ArgumentError, "Invalid friend action provided: #{action}" unless @@FRIENDSHIP_URIS.keys.member?(action)
30:                 value = value.to_i unless value.is_a?(String)
31:                 uri = "#{@@FRIENDSHIP_URIS[action]}/#{value}.json"
32:                 response = http_connect {|conn| create_http_get_request(uri) }
33:                 bless_model(Twitter::User.unmarshal(response.body))
34:         end
message(action, value, user = nil)

Provides access to Twitter‘s Messaging API for sending and deleting direct messages to other users.

action can be:

value should be:

user should be:

  • Twitter::User, Integer or String object when action is :post. The Integer must be the unique ID of the Twitter user you wish to send the direct message to and any Strings passed in must be the screen name of the user you wish to send the direct message to.
  • totally ignore when action is :delete. It has no purpose in this use case scenario.

Examples: The example below sends the message text ‘Are you coming over at 6pm for the BBQ tonight?’ to user with screen name ‘myfriendslogin’…

 @twitter.message(:post, 'Are you coming over at 6pm for the BBQ tonight?', 'myfriendslogin')

The example below sends the same message text as above to user with unique integer ID of 1234567890… the example below sends the same message text as above to user represented by user object instance of Twitter::User

 @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)

the example below delete‘s the message send directly above to user with unique ID 1234567890…

 @twitter.message(:delete, message)

Or the following can also be done…

 @twitter.message(:delete, message.id)

In both scenarios (action is :post or :delete) a blessed Twitter::Message object is returned that represents the newly posted or newly deleted message.

An ArgumentError will be raised if an invalid action is given. Valid actions are:

  • +:post+
  • +:delete+

An ArgumentError is also raised when no user argument is supplied when action is +:post+.

    # File lib/twitter/client/messaging.rb, line 66
66:   def message(action, value, user = nil)
67:     raise ArgumentError, "Invalid messaging action: #{action}" unless [:post, :delete].member?(action)
68:     raise ArgumentError, "User argument must be supplied for :post case" if action.eql?(:post) and user.nil?
69:     uri = @@MESSAGING_URIS[action]
70:     user = user.to_i if user and user.is_a?(Twitter::User)
71:     case action
72:     when :post
73:       response = http_connect({:text => value, :user => user, :source => @@config.source}.to_http_str) {|conn| create_http_post_request(uri) }
74:     when :delete
75:       response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
76:     end
77:     message = Twitter::Message.unmarshal(response.body)
78:     bless_model(message)
79:   end
messages(action, options = nil)

Provides access to Twitter‘s Messaging API for received and sent direct messages.

Example:

 received_messages = @twitter.messages(:received)

An ArgumentError will be raised if an invalid action is given. Valid actions are:

  • +:received+
  • +:sent+
    # File lib/twitter/client/messaging.rb, line 20
20:   def messages(action, options = nil)
21:     def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
22:     raise ArgumentError, "Invalid messaging action: #{action}" unless [:sent, :received].member?(action)
23:     uri = @@MESSAGING_URIS[action] + uri_suffix(options)
24:     response = http_connect {|conn|     create_http_get_request(uri) }
25:     bless_models(Twitter::Message.unmarshal(response.body))
26:   end
my(action)

Syntactic sugar for queries relating to authenticated user in Twitter‘s User API

When action is:

  • :info - Returns user instance for the authenticated user.
  • :friends - Returns Array of users that are authenticated user‘s friends
  • :followers - Returns Array of users that are authenticated user‘s followers

An ArgumentError will be raised if an invalid action is given. Valid actions are:

  • +:info+
  • +:friends+
  • +:followers+
    # File lib/twitter/client/user.rb, line 48
48:   def my(action)
49:     raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action)
50:         response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], :id => @login) }
51:         users = Twitter::User.unmarshal(response.body)
52:         bless_models(users)
53:   end
status(action, value)

Provides access to individual statuses via Twitter‘s Status APIs

action can be of the following values:

  • :get to retrieve status content. Assumes value given responds to :to_i message in meaningful way to yield intended status id.
  • :post to publish a new status
  • :delete to remove an existing status. Assumes value given responds to :to_i message in meaningful way to yield intended status id.

value should be set to:

  • the status identifier for :get case
  • the status text message for :post case
  • none necessary for :delete case

Examples:

 twitter.status(:get, 107786772)
 twitter.status(:post, "New Ruby open source project Twitter4R version 0.2.0 released.")
 twitter.status(:delete, 107790712)

An ArgumentError will be raised if an invalid action is given. Valid actions are:

  • +:get+
  • +:post+
  • +:delete+
    # File lib/twitter/client/status.rb, line 30
30:   def status(action, value)
31:     raise ArgumentError, "Invalid status action: #{action}" unless @@STATUS_URIS.keys.member?(action)
32:     return nil unless value
33:         uri = @@STATUS_URIS[action]
34:         response = nil
35:     case action
36:     when :get
37:         response = http_connect {|conn|    create_http_get_request(uri, :id => value.to_i) }
38:     when :post
39:         response = http_connect({:status => value, :source => @@config.source}.to_http_str) {|conn| create_http_post_request(uri) }
40:     when :delete
41:         response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
42:     end
43:     bless_model(Twitter::Status.unmarshal(response.body))
44:   end
timeline_for(type, options = {}) {|status if block_given?| ...}

Provides access to Twitter‘s Timeline APIs

Returns timeline for given type.

type can take the following values:

:id is on key applicable to be defined in </tt>options</tt>:

  • the id or screen name (aka login) for :friends
  • the id or screen name (aka login) for :user
  • meaningless for the :me case, since twitter.timeline_for(:user, ‘mylogin’) and twitter.timeline_for(:me) are the same assuming ‘mylogin’ is the authenticated user‘s screen name (aka login).

Examples:

 # returns the public statuses since status with id of 6543210
 twitter.timeline_for(:public, id => 6543210)
 # returns the statuses for friend with user id 43210
 twitter.timeline_for(:friend, :id => 43210)
 # returns the statuses for friend with screen name (aka login) of 'otherlogin'
 twitter.timeline_for(:friend, :id => 'otherlogin')
 # returns the statuses for user with screen name (aka login) of 'otherlogin'
 twitter.timeline_for(:user, :id => 'otherlogin')

options can also include the following keys:

  • :id is the user ID, screen name of Twitter::User representation of a Twitter user.
  • :since is a Time object specifying the date-time from which to return results for. Applicable for the :friend, :friends, :user and :me cases.
  • :count specifies the number of statuses to retrieve. Only applicable for the :user case.
  • since_id is the status id of the public timeline from which to retrieve statuses for :public. Only applicable for the :public case.

You can also pass this method a block, which will iterate through the results of the requested timeline and apply the block logic for each status returned.

Example:

 twitter.timeline_for(:public) do |status|
   puts status.user.screen_name, status.text
 end

 twitter.timeline_for(:friend, :id => 'myfriend', :since => 30.minutes.ago) do |status|
   puts status.user.screen_name, status.text
 end

 timeline = twitter.timeline_for(:me) do |status|
   puts status.text
 end

An ArgumentError will be raised if an invalid type is given. Valid types are:

    # File lib/twitter/client/timeline.rb, line 63
63:   def timeline_for(type, options = {}, &block)
64:     raise ArgumentError, "Invalid timeline type: #{type}" unless @@TIMELINE_URIS.keys.member?(type)
65:     uri = @@TIMELINE_URIS[type]
66:     response = http_connect {|conn| create_http_get_request(uri, options) }
67:     timeline = Twitter::Status.unmarshal(response.body)
68:     timeline.each {|status| bless_model(status); yield status if block_given? }
69:     timeline
70:   end
uri_suffix(opts)
    # File lib/twitter/client/favorites.rb, line 17
17:           def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
uri_suffix(opts)
    # File lib/twitter/client/messaging.rb, line 21
21:     def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
user(id, action = :info)

Provides access to Twitter‘s User APIs

Returns user instance for the id given. The id can either refer to the numeric user ID or the user‘s screen name.

For example,

 @twitter.user(234943) #=> Twitter::User object instance for user with numeric id of 234943
 @twitter.user('mylogin') #=> Twitter::User object instance for user with screen name 'mylogin'

An ArgumentError will be raised if an invalid action is given. Valid actions are:

  • +:info+
  • +:friends+

+Note:+ You should not use this method to attempt to retrieve the authenticated user‘s followers. Please use any of the following ways of accessing this list:

 followers = client.my(:followers)

OR

 followers = client.my(:info).followers
    # File lib/twitter/client/user.rb, line 28
28:   def user(id, action = :info)
29:     raise ArgumentError, "Invalid user action: #{action}" unless @@USER_URIS.keys.member?(action)
30:     raise ArgumentError, "Unable to retrieve followers for user: #{id}" if action.eql?(:followers) and not id.eql?(@login)
31:     id = id.to_i if id.is_a?(Twitter::User)
32:         response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], :id => id) }
33:         bless_models(Twitter::User.unmarshal(response.body))
34:   end
Protected Instance methods
bless_model(model)

"Blesses" model object with client information

    # File lib/twitter/client/base.rb, line 19
19:     def bless_model(model)
20:         model.bless(self) if model
21:     end
bless_models(list)
    # File lib/twitter/client/base.rb, line 23
23:     def bless_models(list)
24:       return bless_model(list) if list.respond_to?(:client=)
25:         list.collect { |model| bless_model(model) } if list.respond_to?(:collect)
26:     end
http_connect(body = nil, require_auth = true) {|connection if block_given?| ...}

Returns the response of the HTTP connection.

    # File lib/twitter/client/base.rb, line 6
 6:     def http_connect(body = nil, require_auth = true, &block)
 7:         require_block(block_given?)
 8:         connection = create_http_connection
 9:         connection.start do |connection|
10:                 request = yield connection if block_given?
11:                 request.basic_auth(@login, @password) if require_auth
12:                 response = connection.request(request, body)
13:                 handle_rest_response(response)
14:                 response
15:       end
16:     end