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



51
52
53
54
# File 'lib/twitter_oauth/client.rb', line 51

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
# File 'lib/twitter_oauth/blocks.rb', line 5

def block(id)
  post("/blocks/create/#{id}.json")
end

#exists?(a, b) ⇒ Boolean

exists?.

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/twitter_oauth/friendships.rb', line 25

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



8
9
10
# File 'lib/twitter_oauth/favorites.rb', line 8

def favorite
  post("/favorites/create/#{id}.json")
end

#favorites(page = 1) ⇒ Object



4
5
6
# File 'lib/twitter_oauth/favorites.rb', line 4

def favorites(page=1)
  get("/favorites.json?page=#{page}")
end

#follow(id) ⇒ Object

follow this user.



5
6
7
# File 'lib/twitter_oauth/notifications.rb', line 5

def follow(id)
  post("/notifications/follow/#{id}.json")
end

#followers(page = 1) ⇒ Object

Returns the 100 last followers



10
11
12
# File 'lib/twitter_oauth/user.rb', line 10

def followers(page=1)
  get("/statuses/followers.json?page=#{page}")
end

#followers_ids(options = {}) ⇒ Object



9
10
11
12
# File 'lib/twitter_oauth/friendships.rb', line 9

def followers_ids(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/followers/ids.json?#{args}")
end

#friend(id) ⇒ Object

friend this user.



15
16
17
# File 'lib/twitter_oauth/friendships.rb', line 15

def friend(id)
  post("/friendships/create/#{id}.json")
end

#friends(page = 1) ⇒ Object

Returns the 100 last friends



5
6
7
# File 'lib/twitter_oauth/user.rb', line 5

def friends(page=1)
  get("/statuses/friends.json?page=#{page}")
end

#friends_ids(options = {}) ⇒ Object



4
5
6
7
# File 'lib/twitter_oauth/friendships.rb', line 4

def friends_ids(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/friends/ids.json?#{args}")
end

#friends_timeline(options = {}) ⇒ Object

Returns the 20 most recent statuses posted by the authenticating user and that user’s friends.



18
19
20
21
# File 'lib/twitter_oauth/timeline.rb', line 18

def friends_timeline(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/friends_timeline.json?#{args}")
end

#get(url) ⇒ Object



33
34
35
36
# File 'lib/twitter_oauth/client.rb', line 33

def get(url)
  oauth_response = access_token.get(url)
  Yajl::Parser.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.



12
13
14
15
# File 'lib/twitter_oauth/timeline.rb', line 12

def home_timeline(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/home_timeline.json?#{args}")
end

#infoObject

Returns client info



12
13
14
# File 'lib/twitter_oauth/account.rb', line 12

def info
  get('/account/verify_credentials.json')
end

#leave(id) ⇒ Object

unfollow.



10
11
12
# File 'lib/twitter_oauth/notifications.rb', line 10

def leave(id)
  post("/notifications/leave/#{id}.json")
end

#mentions(options = {}) ⇒ Object Also known as: replies

Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.



31
32
33
34
# File 'lib/twitter_oauth/timeline.rb', line 31

def mentions(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/mentions.json?#{args}")
end

#message(user, text) ⇒ Object

Sends a new direct message to the specified user from the authenticating user.



15
16
17
# File 'lib/twitter_oauth/direct_messages.rb', line 15

def message(user, text)
  post('/direct_messages/new.json', :user => user, :text => text)
end

#message_destroy(id) ⇒ Object

Destroys the direct message specified in the required ID parameter.



20
21
22
# File 'lib/twitter_oauth/direct_messages.rb', line 20

def message_destroy(id)
  post("/direct_messages/destroy/#{id}.json")
end

#messages(page = 1) ⇒ Object

Returns a list of the 20 most recent direct messages sent to the authenticating user.



5
6
7
# File 'lib/twitter_oauth/direct_messages.rb', line 5

def messages(page=1)
  get("/direct_messages.json?page=#{page}")
end

#post(url, body = '', headers = {}) ⇒ Object



38
39
40
41
# File 'lib/twitter_oauth/client.rb', line 38

def post(url, body = '', headers = {})
  oauth_response = access_token.post(url, body, headers)
  Yajl::Parser.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
# File 'lib/twitter_oauth/timeline.rb', line 5

def public_timeline(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/public_timeline.json?#{args}")
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.



17
18
19
# File 'lib/twitter_oauth/account.rb', line 17

def rate_limit_status
  get('/account/rate_limit_status.json')
end

#request_token(options = {}) ⇒ Object



47
48
49
# File 'lib/twitter_oauth/client.rb', line 47

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.



20
21
22
# File 'lib/twitter_oauth/status.rb', line 20

def retweet(id)
  post("/statuses/retweet/#{id}.json")
end

#retweeted_by_me(options = {}) ⇒ Object

Returns the 20 most recent retweets posted by the authenticating user



38
39
40
41
# File 'lib/twitter_oauth/timeline.rb', line 38

def retweeted_by_me(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/retweeted_by_me.json?#{args}")
end

#retweeted_to_me(options = {}) ⇒ Object

Returns the 20 most recent retweets posted by the authenticating user’s friends.



44
45
46
47
# File 'lib/twitter_oauth/timeline.rb', line 44

def retweeted_to_me(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/retweeted_to_me.json?#{args}")
end

#retweets_of_me(options = {}) ⇒ Object

Returns the 20 most recent tweets of the authenticated user that have been retweeted by others.



50
51
52
53
# File 'lib/twitter_oauth/timeline.rb', line 50

def retweets_of_me(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/retweets_of_me.json?#{args}")
end

#search(q, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/twitter_oauth/search.rb', line 8

def search(q, options={})
  options[:page] ||= 1
  options[:per_page] ||= 20
  uri = URI.parse("http://search.twitter.com/search.json?q=#{URI.escape(q)}&page=#{options[:page]}&rpp=#{options[:per_page]}&since_id=#{options[:since_id]}")

  search_result = Yajl::HttpStream.get(uri)
  search_result = OpenStruct.new(search_result)
  search_result.results = search_result.results.collect{|x| OpenStruct.new(x)}
  search_result
end

#sent_messagesObject

Returns a list of the 20 most recent direct messages sent by the authenticating user.



10
11
12
# File 'lib/twitter_oauth/direct_messages.rb', line 10

def sent_messages
  get('/direct_messages/sent.json')
end

#show(username) ⇒ Object



43
44
45
# File 'lib/twitter_oauth/client.rb', line 43

def show(username)
  get("/users/show/#{username}.json")
end

#status(id) ⇒ Object

Returns a single status, specified by the id parameter below.



5
6
7
# File 'lib/twitter_oauth/status.rb', line 5

def status(id)
  get("/statuses/show/#{id}.json")
end

#status_destroy(id) ⇒ Object

Destroys the status specified by the required ID parameter



15
16
17
# File 'lib/twitter_oauth/status.rb', line 15

def status_destroy(id)
  post("/statuses/destroy/#{id}.json")
end

#unblock(id) ⇒ Object

block this user.



10
11
12
# File 'lib/twitter_oauth/blocks.rb', line 10

def unblock(id)
  post("/blocks/destroy/#{id}.json")
end

#unfavoriteObject



12
13
14
# File 'lib/twitter_oauth/favorites.rb', line 12

def unfavorite
  post("/favorites/destroy/#{id}.json")
end

#unfriend(id) ⇒ Object

unfriend.



20
21
22
# File 'lib/twitter_oauth/friendships.rb', line 20

def unfriend(id)
  post("/friendships/destroy/#{id}.json")
end

#update(message, options = {}) ⇒ Object

Updates the authenticating user’s status.



10
11
12
# File 'lib/twitter_oauth/status.rb', line 10

def update(message, options={})
  post('/statuses/update.json', options.merge(:status => message))
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.



23
24
25
26
# File 'lib/twitter_oauth/account.rb', line 23

def update_profile_background_image(image, tile = false)
  body, headers = http_multipart_data({:image => image, :tile => tile})
  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.



37
38
39
# File 'lib/twitter_oauth/account.rb', line 37

def update_profile_colors(colors)
  post('/account/update_profile_colors.json', colors)
end

#update_profile_image(image) ⇒ Object

Updates profile avatar image. Takes a File object which should be an image. Returns extended user info object.



30
31
32
33
# File 'lib/twitter_oauth/account.rb', line 30

def update_profile_image(image)
  body, headers = http_multipart_data({:image => image})
  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.



24
25
26
27
# File 'lib/twitter_oauth/timeline.rb', line 24

def user_timeline(options={})
  args = options.map{|k,v| "#{k}=#{v}"}.join('&')
  get("/statuses/user_timeline.json?#{args}")
end