Class: TwitterClient

Inherits:
Object
  • Object
show all
Defined in:
lib/yatc.rb

Overview

This is the Twitter client class that will allow you to query the Twitter API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials, wait = false, force_retrieval = false) ⇒ TwitterClient

Returns a new instance of TwitterClient.

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
# File 'lib/yatc.rb', line 121

def initialize(credentials, wait = false, force_retrieval = false)
  raise ArgumentError unless credentials.class == Array
  raise 'Must provide at least one credential' if credentials.empty?
  @credentials = credentials
  @credential_index = 0
  activate_next_credential
  @should_wait = wait
  @force_retrieval = force_retrieval
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#consumer_keyObject

Returns the value of attribute consumer_key.



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

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



13
14
15
# File 'lib/yatc.rb', line 13

def consumer_secret
  @consumer_secret
end

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

#force_retrievalObject

Returns the value of attribute force_retrieval.



16
17
18
# File 'lib/yatc.rb', line 16

def force_retrieval
  @force_retrieval
end

#should_waitObject

Returns the value of attribute should_wait.



14
15
16
# File 'lib/yatc.rb', line 14

def should_wait
  @should_wait
end

Instance Method Details

#bearer_token(ck, cs) ⇒ Object



131
132
133
134
135
# File 'lib/yatc.rb', line 131

def bearer_token(ck, cs)
  ck = URI.encode(ck)
  cs = URI.encode(cs)
  Base64.strict_encode64(ck + ':' + cs)
end

#followers_ids(user, count = 5000) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yatc.rb', line 23

def followers_ids(user, count = 5000)
  retrieved = 0
  params = {}
  if user.class == Fixnum
    params[:user_id] = user
  else
    params[:screen_name] = user
  end
  ids = []
  cursor = nil
  force_retrieval = true
  loop do
    if count == -1
      params[:count] = Yatc::Settings::MAX_FOLLOWER_IDS
    else
      params[:count] = [Yatc::Settings::MAX_FOLLOWER_IDS, count].min
      count -= params[:count]
    end

    params[:cursor] = cursor unless cursor.nil?

    encoded_params = encode_params(params)
    url = "#{BASE_URL}/followers/ids.json?#{encoded_params}"
    data = JSON.parse(execute(:get, url))
    ids += data['ids']
    cursor = data['next_cursor']
    retrieved += params[:count]
    break if cursor == 0
    break if count == 0
  end
  force_retrieval = false
  ids
end

#friends_ids(user, count = 5000) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yatc.rb', line 57

def friends_ids(user, count = 5000)
  params = {}
  if user.class == Fixnum
    params[:user_id] = user
  else
    params[:screen_name] = user
  end
  ids = []
  cursor = nil
  while count > 0
    params[:count] = [Yatc::Settings::MAX_FOLLOWER_IDS, count].min
    count -= params[:count]
    unless cursor.nil?
      params[:cursor] = cursor
    end

    encoded_params = encode_params(params)
    url = "#{BASE_URL}/friends/ids.json?#{encoded_params}"
    data = JSON.parse(execute(:get, url))
    ids += data['ids']
    cursor = data['next_cursor']
  end
  ids
end

#request_access_token(ck, cs) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/yatc.rb', line 137

def request_access_token(ck, cs)
  bearer_token = bearer_token(ck, cs)
  begin
    resp = RestClient::Request.execute(
      method: :post,
      url: 'https://api.twitter.com/oauth2/token',
      headers: {
        'User-Agent' => 'My twitter App',
        'Authorization' => "Basic #{bearer_token}",
        'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
        'Content-Length' => 29,
        'Accept-Encoding' => 'gzip'
      },
      payload: 'grant_type=client_credentials'
    )
    JSON.parse(resp)['access_token']
  rescue => e
    raise e.http_code == 403 ? AuthenticationError : e
  end
end

#statuses_user_timeline(user, count = 200) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/yatc.rb', line 94

def statuses_user_timeline(user, count = 200)
  params = {}
  if user.class == Fixnum
    params[:user_id] = user
  else
    params[:screen_name] = user
  end
  tweets = []
  max_id = nil
  while count > 0
    params[:count] = [Yatc::Settings::MAX_TWEETS, count].min
    count -= params[:count]
    unless max_id.nil?
      params[:max_id] = max_id
    end
    encoded_params = encode_params(params)
    url = "#{BASE_URL}/statuses/user_timeline.json?#{encoded_params}"
    t = JSON.parse(execute(:get, url))
    unless t.empty?
      max_id = t.map{ |tweet| tweet['id'] }.min - 1
    end
    tweets += t
  end
  tweets
end

#test_access(ck, cs) ⇒ Object



19
20
21
# File 'lib/yatc.rb', line 19

def test_access(ck, cs)
  !request_access_token(ck, cs).empty?
end

#users_show(user) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/yatc.rb', line 82

def users_show(user)
  params = {}
  if user.class == Fixnum
    params[:user_id] = user
  else
    params[:screen_name] = user
  end
  params = encode_params(params)
  url = "#{BASE_URL}/users/show.json?#{params}"
  JSON.parse(execute(:get, url))
end