Class: TwitterOAuth::Client

Inherits:
Object
  • Object
show all
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

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(options = {})
  @consumer_key = options[:consumer_key]
  @consumer_secret = options[:consumer_secret]
  @token = options[:token]
  @secret = options[: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(options={})
  consumer.options[:authorize_path] = '/oauth/authenticate'
  request_token(options)
end

#authorize(token, secret, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/twitter_oauth/client.rb', line 23

def authorize(token, secret, options = {})
  request_token = OAuth::RequestToken.new(
    consumer, token, secret
  )
  @access_token = request_token.get_access_token(options)
  @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.

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/twitter_oauth/account.rb', line 6

def authorized?
  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?.

Returns:

  • (Boolean)


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

#favoriteObject



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(options={})
  args = options.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(options={})
  args = options.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(options={})
  args = options.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(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  oauth_response = access_token.get("/statuses/home_timeline.json?#{args}")
  JSON.parse(oauth_response.body)
end

#infoObject

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(options={})
  args = options.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 message(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 message_destroy(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 messages(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(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  oauth_response = access_token.get("/statuses/public_timeline.json?#{args}")
  JSON.parse(oauth_response.body)
end

#rate_limit_statusObject

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(options={})
  consumer.get_request_token(options)
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(options={})
  args = options.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(options={})
  args = options.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(options={})
  args = options.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, options={})
  options[:page] ||= 1
  options[:per_page] ||= 20
  response = open("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{options[:page]}&rpp=#{options[:per_page]}&since_id=#{options[: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_messagesObject

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 sent_messages
  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

#unfavoriteObject



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(message, options={})
  oauth_response = access_token.post('/statuses/update.json', options.merge(:status => message))
  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(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  oauth_response = access_token.get("/statuses/user_timeline.json?#{args}")
  JSON.parse(oauth_response.body)
end