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/status.rb,
lib/twitter_oauth/account.rb,
lib/twitter_oauth/timeline.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(options = {}) ⇒ Object
-
#friend(id) ⇒ Object
friend this user.
-
#friends(page = 1) ⇒ Object
Returns the 100 last friends.
- #friends_ids(options = {}) ⇒ Object
-
#friends_timeline(options = {}) ⇒ Object
Returns the 20 most recent statuses posted by the authenticating user and that user’s friends.
-
#home_timeline(options = {}) ⇒ Object
Returns the 20 most recent statuses, including retweets, 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(options = {}) ⇒ Object
(also: #replies)
Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
-
#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(options = {}) ⇒ 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.
- #request_token(options = {}) ⇒ Object
-
#retweet(id) ⇒ Object
Retweets the tweet specified by the id parameter.
-
#retweeted_by_me(options = {}) ⇒ Object
Returns the 20 most recent retweets posted by the authenticating user.
-
#retweeted_to_me(options = {}) ⇒ Object
Returns the 20 most recent retweets posted by the authenticating user’s friends.
-
#retweets_of_me(options = {}) ⇒ Object
Returns the 20 most recent tweets of the authenticated user that have been retweeted by others.
- #search(q, options = {}) ⇒ 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_timeline(options = {}) ⇒ Object
(also: #user)
Returns the 20 most recent statuses posted from the authenticating user.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 |
# File 'lib/twitter_oauth/client.rb', line 16 def initialize( = {}) @consumer_key = [:consumer_key] @consumer_secret = [:consumer_secret] @token = [:token] @secret = [:secret] end |
Instance Method Details
#authentication_request_token(options = {}) ⇒ Object
42 43 44 45 |
# File 'lib/twitter_oauth/client.rb', line 42 def authentication_request_token(={}) consumer.[:authorize_path] = '/oauth/authenticate' request_token() end |
#authorize(token, secret, options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/twitter_oauth/client.rb', line 23 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?.
29 30 31 32 |
# File 'lib/twitter_oauth/friendships.rb', line 29 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(options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/twitter_oauth/friendships.rb', line 10 def followers_ids(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/followers/ids.json?#{args}") JSON.parse(oauth_response.body) end |
#friend(id) ⇒ Object
friend this user.
17 18 19 20 |
# File 'lib/twitter_oauth/friendships.rb', line 17 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(options = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/twitter_oauth/friendships.rb', line 4 def friends_ids(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/friends/ids.json?#{args}") JSON.parse(oauth_response.body) end |
#friends_timeline(options = {}) ⇒ Object
Returns the 20 most recent statuses posted by the authenticating user and that user’s friends.
20 21 22 23 24 |
# File 'lib/twitter_oauth/timeline.rb', line 20 def friends_timeline(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/friends_timeline.json?#{args}") JSON.parse(oauth_response.body) end |
#home_timeline(options = {}) ⇒ Object
Returns the 20 most recent statuses, including retweets, posted by the authenticating user and that user’s friends. This is the equivalent of /timeline/home on the Web.
13 14 15 16 17 |
# File 'lib/twitter_oauth/timeline.rb', line 13 def home_timeline(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/home_timeline.json?#{args}") 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(options = {}) ⇒ Object Also known as: replies
Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
35 36 37 38 39 |
# File 'lib/twitter_oauth/timeline.rb', line 35 def mentions(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/mentions.json?#{args}") JSON.parse(oauth_response.body) 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(options = {}) ⇒ Object
Returns the 20 most recent statuses from non-protected users who have set a custom user icon.
5 6 7 8 9 |
# File 'lib/twitter_oauth/timeline.rb', line 5 def public_timeline(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/public_timeline.json?#{args}") 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 |
#request_token(options = {}) ⇒ Object
38 39 40 |
# File 'lib/twitter_oauth/client.rb', line 38 def request_token(={}) consumer.get_request_token() end |
#retweet(id) ⇒ Object
Retweets the tweet specified by the id parameter. Returns the original tweet with retweet details embedded.
23 24 25 26 |
# File 'lib/twitter_oauth/status.rb', line 23 def retweet(id) oauth_response = access_token.post("/statuses/retweet/#{id}.json") JSON.parse(oauth_response.body) end |
#retweeted_by_me(options = {}) ⇒ Object
Returns the 20 most recent retweets posted by the authenticating user
43 44 45 46 47 |
# File 'lib/twitter_oauth/timeline.rb', line 43 def retweeted_by_me(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/retweeted_by_me.json?#{args}") JSON.parse(oauth_response.body) end |
#retweeted_to_me(options = {}) ⇒ Object
Returns the 20 most recent retweets posted by the authenticating user’s friends.
50 51 52 53 54 |
# File 'lib/twitter_oauth/timeline.rb', line 50 def retweeted_to_me(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/retweeted_to_me.json?#{args}") JSON.parse(oauth_response.body) end |
#retweets_of_me(options = {}) ⇒ Object
Returns the 20 most recent tweets of the authenticated user that have been retweeted by others.
57 58 59 60 61 |
# File 'lib/twitter_oauth/timeline.rb', line 57 def retweets_of_me(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/retweets_of_me.json?#{args}") JSON.parse(oauth_response.body) end |
#search(q, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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]}&since_id=#{[:since_id]}", 'User-Agent' => 'socialtriage.com') search_result = JSON.parse(response.read) search_result = OpenStruct.new(search_result) klass = make_klass(search_result.results.first) search_result.results = search_result.results.collect do |res| klass.new(res) end 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
33 34 35 36 |
# File 'lib/twitter_oauth/client.rb', line 33 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.
5 6 7 8 |
# File 'lib/twitter_oauth/status.rb', line 5 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
17 18 19 20 |
# File 'lib/twitter_oauth/status.rb', line 17 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.
23 24 25 26 |
# File 'lib/twitter_oauth/friendships.rb', line 23 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.
11 12 13 14 |
# File 'lib/twitter_oauth/status.rb', line 11 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_timeline(options = {}) ⇒ Object Also known as: user
Returns the 20 most recent statuses posted from the authenticating user.
27 28 29 30 31 |
# File 'lib/twitter_oauth/timeline.rb', line 27 def user_timeline(={}) args = .map{|k,v| "#{k}=#{v}"}.join('&') oauth_response = access_token.get("/statuses/user_timeline.json?#{args}") JSON.parse(oauth_response.body) end |