Class: TwitterOAuth::Client
- Inherits:
-
Object
- Object
- TwitterOAuth::Client
- Defined in:
- lib/twitter_oauth/user.rb,
lib/twitter_oauth/utils.rb,
lib/twitter_oauth/blocks.rb,
lib/twitter_oauth/client.rb,
lib/twitter_oauth/search.rb,
lib/twitter_oauth/account.rb,
lib/twitter_oauth/statuses.rb,
lib/twitter_oauth/favorites.rb,
lib/twitter_oauth/friendships.rb,
lib/twitter_oauth/notifications.rb,
lib/twitter_oauth/direct_messages.rb
Constant Summary collapse
- CRLF =
"\r\n"
Instance Method Summary collapse
- #authentication_request_token(options = {}) ⇒ Object
- #authorize(token, secret, options = {}) ⇒ Object
-
#authorized? ⇒ Boolean
Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not.
-
#block(id) ⇒ Object
unblock this user.
-
#exists?(a, b) ⇒ Boolean
exists?.
- #favorite ⇒ Object
- #favorites(page = 1) ⇒ Object
-
#follow(id) ⇒ Object
follow this user.
-
#followers(page = 1) ⇒ Object
Returns the 100 last followers.
- #followers_ids ⇒ Object
-
#friend(id) ⇒ Object
friend this user.
-
#friends(page = 1) ⇒ Object
Returns the 100 last friends.
- #friends_ids ⇒ Object
-
#friends_timeline(rpp = 20, page = 1) ⇒ Object
Returns the 20 most recent statuses posted by the authenticating user and that user’s friends.
-
#info ⇒ Object
Returns client info.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#leave(id) ⇒ Object
unfollow.
-
#mentions ⇒ Object
alias.
-
#message(user, text) ⇒ Object
Sends a new direct message to the specified user from the authenticating user.
-
#message_destroy(id) ⇒ Object
Destroys the direct message specified in the required ID parameter.
-
#messages(page = 1) ⇒ Object
Returns a list of the 20 most recent direct messages sent to the authenticating user.
-
#public_timeline ⇒ Object
Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
-
#rate_limit_status ⇒ Object
Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
-
#replies(page = 1) ⇒ Object
Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
- #request_token(options = {}) ⇒ Object
- #search(q, page = 1, per_page = 20) ⇒ Object
-
#sent_messages ⇒ Object
Returns a list of the 20 most recent direct messages sent by the authenticating user.
- #show(username) ⇒ Object
-
#status(id) ⇒ Object
Returns a single status, specified by the id parameter below.
-
#status_destroy(id) ⇒ Object
Destroys the status specified by the required ID parameter.
-
#unblock(id) ⇒ Object
block this user.
- #unfavorite ⇒ Object
-
#unfriend(id) ⇒ Object
unfriend.
-
#update(message, options = {}) ⇒ Object
Updates the authenticating user’s status.
-
#update_profile_background_image(image, tile = false) ⇒ Object
Updates profile background image.
-
#update_profile_colors(colors) ⇒ Object
colors hash must contain at least one or more of the following keys :profile_background_color, :profile_text_color, :profile_link_color, :profile_sidebar_fill_color, :profile_sidebar_border_color returns extended user info object.
-
#update_profile_image(image) ⇒ Object
Updates profile avatar image.
-
#user(page = 1) ⇒ Object
Returns the 20 most recent statuses posted from the authenticating user.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 |
# File 'lib/twitter_oauth/client.rb', line 15 def initialize( = {}) @consumer_key = [:consumer_key] @consumer_secret = [:consumer_secret] @token = [:token] @secret = [:secret] end |
Instance Method Details
#authentication_request_token(options = {}) ⇒ Object
32 33 34 35 |
# File 'lib/twitter_oauth/client.rb', line 32 def authentication_request_token( = {}) consumer.[:authorize_path] = '/oauth/authenticate' request_token() end |
#authorize(token, secret, options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/twitter_oauth/client.rb', line 22 def (token, secret, = {}) request_token = OAuth::RequestToken.new( consumer, token, secret ) @access_token = request_token.get_access_token() @token = @access_token.token @secret = @access_token.secret @access_token end |
#authorized? ⇒ Boolean
Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not.
6 7 8 9 |
# File 'lib/twitter_oauth/account.rb', line 6 def oauth_response = access_token.get('/account/verify_credentials.json') return oauth_response.class == Net::HTTPOK end |
#block(id) ⇒ Object
unblock this user.
5 6 7 8 |
# File 'lib/twitter_oauth/blocks.rb', line 5 def block(id) oauth_response = access_token.post("/blocks/create/#{id}.json") JSON.parse(oauth_response.body) end |
#exists?(a, b) ⇒ Boolean
exists?.
27 28 29 30 |
# File 'lib/twitter_oauth/friendships.rb', line 27 def exists?(a, b) oauth_response = access_token.get("/friendships/exists.json?user_a=#{a}&user_b=#{b}") oauth_response.body.strip == 'true' end |
#favorite ⇒ Object
9 10 11 12 |
# File 'lib/twitter_oauth/favorites.rb', line 9 def favorite oauth_response = access_token.post("/favorites/create/#{id}.json") JSON.parse(oauth_response.body) end |
#favorites(page = 1) ⇒ Object
4 5 6 7 |
# File 'lib/twitter_oauth/favorites.rb', line 4 def favorites(page=1) oauth_response = access_token.get("/favorites.json?page=#{page}") JSON.parse(oauth_response.body) end |
#follow(id) ⇒ Object
follow this user.
5 6 7 8 |
# File 'lib/twitter_oauth/notifications.rb', line 5 def follow(id) oauth_response = access_token.post("/notifications/follow/#{id}.json") JSON.parse(oauth_response.body) end |
#followers(page = 1) ⇒ Object
Returns the 100 last followers
11 12 13 14 |
# File 'lib/twitter_oauth/user.rb', line 11 def followers(page=1) oauth_response = access_token.get("/statuses/followers.json?page=#{page}") JSON.parse(oauth_response.body) end |
#followers_ids ⇒ Object
9 10 11 12 |
# File 'lib/twitter_oauth/friendships.rb', line 9 def followers_ids oauth_response = access_token.get("/followers/ids.json") JSON.parse(oauth_response.body) end |
#friend(id) ⇒ Object
friend this user.
15 16 17 18 |
# File 'lib/twitter_oauth/friendships.rb', line 15 def friend(id) oauth_response = access_token.post("/friendships/create/#{id}.json") JSON.parse(oauth_response.body) end |
#friends(page = 1) ⇒ Object
Returns the 100 last friends
5 6 7 8 |
# File 'lib/twitter_oauth/user.rb', line 5 def friends(page=1) oauth_response = access_token.get("/statuses/friends.json?page=#{page}") JSON.parse(oauth_response.body) end |
#friends_ids ⇒ Object
4 5 6 7 |
# File 'lib/twitter_oauth/friendships.rb', line 4 def friends_ids oauth_response = access_token.get("/friends/ids.json") JSON.parse(oauth_response.body) end |
#friends_timeline(rpp = 20, page = 1) ⇒ Object
Returns the 20 most recent statuses posted by the authenticating user and that user’s friends.
11 12 13 14 |
# File 'lib/twitter_oauth/statuses.rb', line 11 def friends_timeline(rpp=20, page=1) oauth_response = access_token.get("/statuses/friends_timeline.json?count=#{rpp}&page=#{page}") JSON.parse(oauth_response.body) end |
#info ⇒ Object
Returns client info
12 13 14 15 |
# File 'lib/twitter_oauth/account.rb', line 12 def info oauth_response = access_token.get('/account/verify_credentials.json') JSON.parse(oauth_response.body) end |
#leave(id) ⇒ Object
unfollow.
11 12 13 14 |
# File 'lib/twitter_oauth/notifications.rb', line 11 def leave(id) oauth_response = access_token.post("/notifications/leave/#{id}.json") JSON.parse(oauth_response.body) end |
#mentions ⇒ Object
alias
41 42 43 |
# File 'lib/twitter_oauth/statuses.rb', line 41 def mentions replies end |
#message(user, text) ⇒ Object
Sends a new direct message to the specified user from the authenticating user.
17 18 19 20 |
# File 'lib/twitter_oauth/direct_messages.rb', line 17 def (user, text) oauth_response = access_token.post('/direct_messages/new.json', :user => user, :text => text) JSON.parse(oauth_response.body) end |
#message_destroy(id) ⇒ Object
Destroys the direct message specified in the required ID parameter.
23 24 25 26 |
# File 'lib/twitter_oauth/direct_messages.rb', line 23 def (id) oauth_response = access_token.post("/direct_messages/destroy/#{id}.json") JSON.parse(oauth_response.body) end |
#messages(page = 1) ⇒ Object
Returns a list of the 20 most recent direct messages sent to the authenticating user.
5 6 7 8 |
# File 'lib/twitter_oauth/direct_messages.rb', line 5 def (page=1) oauth_response = access_token.get("/direct_messages.json?page=#{page}") JSON.parse(oauth_response.body) end |
#public_timeline ⇒ Object
Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
5 6 7 8 |
# File 'lib/twitter_oauth/statuses.rb', line 5 def public_timeline oauth_response = access_token.get('/statuses/public_timeline.json') JSON.parse(oauth_response.body) end |
#rate_limit_status ⇒ Object
Returns the remaining number of API requests available to the requesting user before the API limit is reached for the current hour.
18 19 20 21 |
# File 'lib/twitter_oauth/account.rb', line 18 def rate_limit_status oauth_response = access_token.get('/account/rate_limit_status.json') JSON.parse(oauth_response.body) end |
#replies(page = 1) ⇒ Object
Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
35 36 37 38 |
# File 'lib/twitter_oauth/statuses.rb', line 35 def replies(page=1) oauth_response = access_token.get("/statuses/mentions.json?page=#{page}") JSON.parse(oauth_response.body) end |
#request_token(options = {}) ⇒ Object
42 43 44 |
# File 'lib/twitter_oauth/client.rb', line 42 def request_token(={}) consumer.get_request_token() end |
#search(q, page = 1, per_page = 20) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/twitter_oauth/search.rb', line 7 def search(q, page = 1, per_page = 20) response = open("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{page}&rpp=#{per_page}") search_result = JSON.parse(response.read) search_result = OpenStruct.new(search_result) search_result.results = search_result.results.collect{|x| OpenStruct.new(x)} search_result end |
#sent_messages ⇒ Object
Returns a list of the 20 most recent direct messages sent by the authenticating user.
11 12 13 14 |
# File 'lib/twitter_oauth/direct_messages.rb', line 11 def oauth_response = access_token.get('/direct_messages/sent.json') JSON.parse(oauth_response.body) end |
#show(username) ⇒ Object
37 38 39 40 |
# File 'lib/twitter_oauth/client.rb', line 37 def show(username) oauth_response = access_token.get("/users/show/#{username}.json") JSON.parse(oauth_response.body) end |
#status(id) ⇒ Object
Returns a single status, specified by the id parameter below.
23 24 25 26 |
# File 'lib/twitter_oauth/statuses.rb', line 23 def status(id) oauth_response = access_token.get("/statuses/show/#{id}.json") JSON.parse(oauth_response.body) end |
#status_destroy(id) ⇒ Object
Destroys the status specified by the required ID parameter
46 47 48 49 |
# File 'lib/twitter_oauth/statuses.rb', line 46 def status_destroy(id) oauth_response = access_token.post("/statuses/destroy/#{id}.json") JSON.parse(oauth_response.body) end |
#unblock(id) ⇒ Object
block this user.
11 12 13 14 |
# File 'lib/twitter_oauth/blocks.rb', line 11 def unblock(id) oauth_response = access_token.post("/blocks/destroy/#{id}.json") JSON.parse(oauth_response.body) end |
#unfavorite ⇒ Object
14 15 16 17 |
# File 'lib/twitter_oauth/favorites.rb', line 14 def unfavorite oauth_response = access_token.post("/favorites/destroy/#{id}.json") JSON.parse(oauth_response.body) end |
#unfriend(id) ⇒ Object
unfriend.
21 22 23 24 |
# File 'lib/twitter_oauth/friendships.rb', line 21 def unfriend(id) oauth_response = access_token.post("/friendships/destroy/#{id}.json") JSON.parse(oauth_response.body) end |
#update(message, options = {}) ⇒ Object
Updates the authenticating user’s status.
29 30 31 32 |
# File 'lib/twitter_oauth/statuses.rb', line 29 def update(, = {}) oauth_response = access_token.post('/statuses/update.json', .merge(:status => )) JSON.parse(oauth_response.body) end |
#update_profile_background_image(image, tile = false) ⇒ Object
Updates profile background image. Takes a File object and optional tile argument. Returns extended user info object.
25 26 27 28 |
# File 'lib/twitter_oauth/account.rb', line 25 def update_profile_background_image(image, tile = false) body, headers = http_multipart_data({:image => image, :tile => tile}) oauth_response = access_token.post('/account/update_profile_background_image.json', body, headers) end |
#update_profile_colors(colors) ⇒ Object
colors hash must contain at least one or more of the following keys :profile_background_color, :profile_text_color, :profile_link_color, :profile_sidebar_fill_color, :profile_sidebar_border_color returns extended user info object.
39 40 41 42 |
# File 'lib/twitter_oauth/account.rb', line 39 def update_profile_colors(colors) oauth_response = access_token.post('/account/update_profile_colors.json', colors) JSON.parse(oauth_response.body) end |
#update_profile_image(image) ⇒ Object
Updates profile avatar image. Takes a File object which should be an image. Returns extended user info object.
32 33 34 35 |
# File 'lib/twitter_oauth/account.rb', line 32 def update_profile_image(image) body, headers = http_multipart_data({:image => image}) oauth_response = access_token.post('/account/update_profile_image.json', body, headers) end |
#user(page = 1) ⇒ Object
Returns the 20 most recent statuses posted from the authenticating user.
17 18 19 20 |
# File 'lib/twitter_oauth/statuses.rb', line 17 def user(page=1) oauth_response = access_token.get("/statuses/user_timeline.json?page=#{page}") JSON.parse(oauth_response.body) end |