Class Twitter::Client
In: lib/twitter/extras.rb
lib/twitter/config.rb
lib/twitter/client/timeline.rb
lib/twitter/client/messaging.rb
lib/twitter/client/base.rb
lib/twitter/client/status.rb
lib/twitter/client/friendship.rb
lib/twitter/client/user.rb
lib/twitter/client.rb
lib/twitter/console.rb
Parent: Object

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

Methods

Included Modules

Twitter::ClassUtilMixin

Attributes

login  [RW] 
password  [RW] 

Public Class methods

Yields to given block to configure the Twitter4R API.

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

Public Instance methods

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.

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)

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+.

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+

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+

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+

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:

method scoped method

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

Protected Instance methods

"Blesses" model object with client information

[Validate]