Used to query or post to the Twitter REST API to simplify code.
- bless_model
- bless_models
- configure
- featured
- friend
- from_config
- http_connect
- message
- messages
- my
- status
- timeline_for
- user
- Twitter::ClassUtilMixin
| [RW] | login | |
| [RW] | password |
Yields to given block to configure the Twitter4R API.
[ show source ]
# File lib/twitter/config.rb, line 63
63: def configure(&block)
64: raise ArgumentError, "Block must be provided to configure" unless block_given?
65: yield @@config
66: end
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')
[ show source ]
# 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
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.
[ show source ]
# 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
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)
[ show source ]
# File lib/twitter/client/friendship.rb, line 28
28: def friend(action, value)
29: value = value.to_i unless value.is_a?(String)
30: uri = "#{@@FRIENDSHIP_URIS[action]}/#{value}.json"
31: response = http_connect {|conn| create_http_get_request(uri) }
32: bless_model(Twitter::User.unmarshal(response.body))
33: end
Provides access to Twitter‘s Messaging API for sending and deleting direct messages to other users.
action can be:
- :post - to send a new direct message, value, to user given.
- :delete - to delete direct message with message ID value.
value should be:
- String when action is :post. Will be the message text sent to given user.
- Integer or Twitter::Message object when action is :delete. Will refer to the unique message ID to delete. When passing in an instance of Twitter::Message that Status will be
user should be:
- Twitter::User or Integer object when action is :post.
- 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.
[ show source ]
# File lib/twitter/client/messaging.rb, line 50
50: def message(action, value, user = nil)
51: uri = @@MESSAGING_URIS[action]
52: case action
53: when :post
54: response = http_connect({:text => value, :user => user.to_i}.to_http_str) {|conn| create_http_post_request(uri) }
55: when :delete
56: response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
57: end
58: message = Twitter::Message.unmarshal(response.body)
59: bless_model(message)
60: end
[ show source ]
# File lib/twitter/client/messaging.rb, line 14
14: def messages(action)
15: uri = @@MESSAGING_URIS[action]
16: response = http_connect {|conn| create_http_get_request(uri) }
17: bless_models(Twitter::Message.unmarshal(response.body))
18: end
[ show source ]
# File lib/twitter/client/user.rb, line 27
27: def my(action)
28: response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], :id => @login) }
29: json = response.body
30: users = Twitter::User.unmarshal(json)
31: bless_models(users)
32: end
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)
[ show source ]
# File lib/twitter/client/status.rb, line 25
25: def status(action, value)
26: return nil unless value
27: uri = @@STATUS_URIS[action]
28: response = nil
29: case action
30: when :get
31: response = http_connect {|conn| create_http_get_request(uri, :id => value.to_i) }
32: when :post
33: response = http_connect({:status => value}.to_http_str) {|conn| create_http_post_request(uri) }
34: when :delete
35: response = http_connect {|conn| create_http_delete_request(uri, :id => value.to_i) }
36: end
37: bless_model(Twitter::Status.unmarshal(response.body))
38: end
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
[ show source ]
# File lib/twitter/client/timeline.rb, line 55
55: def timeline_for(type, options = {}, &block)
56: uri = @@TIMELINE_URIS[type]
57: response = http_connect {|conn| create_http_get_request(uri, options) }
58: timeline = Twitter::Status.unmarshal(response.body)
59: timeline.each {|status| bless_model(status); yield status if block_given? }
60: timeline
61: end
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'
[ show source ]
# File lib/twitter/client/user.rb, line 16
16: def user(id, action = :info)
17: response = http_connect {|conn| create_http_get_request(@@USER_URIS[action], :id => id) }
18: bless_models(Twitter::User.unmarshal(response.body))
19: end
"Blesses" model object with client information
[ show source ]
# File lib/twitter/client/base.rb, line 19
19: def bless_model(model)
20: model.bless(self) if model
21: end
[ show source ]
# 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
Returns the response of the HTTP connection.
[ show source ]
# 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