RTwitter 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/RTwitter.rb +255 -253
- data/lib/RTwitter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b81417e68608d69cff2db081f404408db52f1215
|
4
|
+
data.tar.gz: 081b5c45b66a168a00229e1dfa70ec4331997d77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbb05d5385bf47779a71f807e13c410afa11df8d84d4eb87ba1d89887e7c5f96c63bda4958405c62e9dc60906922d1399079b36b112f0dc6b75ee67ba9298cef
|
7
|
+
data.tar.gz: 75fc34a0d7d99f507c8407708ce281a55dac4684baf94c32b27b1282a7669b5edf462ecfc7445acd9b3b3668c76c2ed6616a702cb553c97157299dce3ca333f2
|
data/lib/RTwitter.rb
CHANGED
@@ -7,259 +7,261 @@ require'json'
|
|
7
7
|
require'net/http'
|
8
8
|
|
9
9
|
module RTwitter
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
10
|
+
class OAuth
|
11
|
+
|
12
|
+
attr_reader :consumer_key,:consumer_key_secret,:access_token,:access_token_secret,:user_id,:screen_name
|
13
|
+
attr_accessor :userAgent
|
14
|
+
def initialize(ck ,cks ,at = nil ,ats = nil)
|
15
|
+
@consumer_key = ck
|
16
|
+
@consumer_key_secret = cks
|
17
|
+
@access_token = at
|
18
|
+
@access_token_secret = ats
|
19
|
+
@userAgent = 'RTwitter'
|
20
|
+
end
|
21
|
+
def request_token
|
22
|
+
|
23
|
+
oauth_params = oauth
|
24
|
+
oauth_params.delete('oauth_token')
|
25
|
+
oauth_params['oauth_callback'] = 'oob'
|
26
|
+
base_params = Hash[oauth_params.sort]
|
27
|
+
query = build_query(base_params)
|
28
|
+
url = 'https://api.twitter.com/oauth/request_token'
|
29
|
+
base = 'POST&' + escape(url) + '&' + escape(query)
|
30
|
+
key = @consumer_key_secret + '&'
|
31
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
32
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
33
|
+
response = post_request(url,'',header)
|
34
|
+
|
35
|
+
items = response.body.split('&')
|
36
|
+
@request_token = items[0].split('=')[1]
|
37
|
+
@request_token_secret = items[1].split('=')[1]
|
38
|
+
return [@request_token,@request_token_secret]
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
def access_token(verifier)
|
43
|
+
|
44
|
+
oauth_params = oauth
|
45
|
+
oauth_params.delete('oauth_token')
|
46
|
+
oauth_params['oauth_verifier'] = verifier
|
47
|
+
oauth_params['oauth_token'] = @request_token
|
48
|
+
base_params = Hash[oauth_params.sort]
|
49
|
+
query = build_query(base_params)
|
50
|
+
url = 'https://api.twitter.com/oauth/access_token'
|
51
|
+
base = 'POST&' + escape(url) + '&' + escape(query)
|
52
|
+
key = @consumer_key_secret + '&' + @request_token_secret
|
53
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
54
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
55
|
+
body = ''
|
56
|
+
response = post_request(url,body,header)
|
57
|
+
|
58
|
+
access_tokens = response.body.split('&')
|
59
|
+
@access_token = access_tokens[0].split('=')[1]
|
60
|
+
@access_token_secret = access_tokens[1].split('=')[1]
|
61
|
+
@user_id = access_tokens[2].split('=')[1]
|
62
|
+
@screen_name = access_tokens[3].split('=')[1]
|
63
|
+
|
64
|
+
return [@access_token,@access_token_secret,@user_id,@screen_name]
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def post(endpoint,additional_params = Hash.new)
|
69
|
+
|
70
|
+
url = url(endpoint)
|
71
|
+
header = signature('POST',url,additional_params)
|
72
|
+
body = build_body(additional_params)
|
73
|
+
response = post_request(url,body,header)
|
74
|
+
return decode(response)
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
def get(endpoint,additional_params = Hash.new)
|
79
|
+
|
80
|
+
url = url(endpoint)
|
81
|
+
header = signature('GET',url,additional_params)
|
82
|
+
body = build_body(additional_params)
|
83
|
+
response = get_request(url,body,header)
|
84
|
+
return decode(response)
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
def streaming(endpoint,additional_params = Hash.new)
|
89
|
+
|
90
|
+
url = url(endpoint)
|
91
|
+
header = signature('GET',url,additional_params)
|
92
|
+
body = build_body(additional_params)
|
93
|
+
buffer = ''
|
94
|
+
streaming_request(url,body,header){|chunk|
|
95
|
+
if buffer != ''
|
96
|
+
chunk = buffer + chunk
|
97
|
+
buffer = ''
|
98
|
+
end
|
99
|
+
begin
|
100
|
+
status = JSON.parse(chunk)
|
101
|
+
rescue
|
102
|
+
buffer << chunk
|
103
|
+
next
|
104
|
+
end
|
105
|
+
|
106
|
+
yield status
|
107
|
+
}
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
def signature(method,url,additional_params)
|
113
|
+
oauth_params = oauth
|
114
|
+
base_params = oauth_params.merge(additional_params)
|
115
|
+
base_params = Hash[base_params.sort]
|
116
|
+
query = build_query(base_params)
|
117
|
+
base = method + '&' + escape(url) + '&' + escape(query)
|
118
|
+
key = @consumer_key_secret + '&' + @access_token_secret
|
119
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
120
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
121
|
+
return header
|
122
|
+
end
|
123
|
+
|
124
|
+
def oauth
|
125
|
+
{
|
126
|
+
'oauth_consumer_key' => @consumer_key,
|
127
|
+
'oauth_signature_method' => 'HMAC-SHA1',
|
128
|
+
'oauth_timestamp' => Time.now.to_i.to_s,
|
129
|
+
'oauth_version' => '1.0',
|
130
|
+
'oauth_nonce' => Random.new_seed.to_s,
|
131
|
+
'oauth_token' => @access_token
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
def decode(response)
|
136
|
+
if response.body == nil
|
137
|
+
raise RTwitterException,'Failed to receive response.'
|
138
|
+
end
|
139
|
+
if response.body == ''
|
140
|
+
raise RTwitterException,'Empty response.'
|
141
|
+
end
|
142
|
+
begin
|
143
|
+
obj = JSON.parse(response.body)
|
144
|
+
rescue
|
145
|
+
return response.body
|
146
|
+
end
|
147
|
+
if obj.include?('error')
|
148
|
+
raise RTwitterException,obj['error']
|
149
|
+
end
|
150
|
+
if obj.include?('errors')
|
151
|
+
if obj['errors'].kind_of?(String)
|
152
|
+
raise RTwitterException,obj['errors']
|
153
|
+
else
|
154
|
+
messages = []
|
155
|
+
obj['errors'].each{|errors|
|
156
|
+
messages << errors['message']
|
157
|
+
}
|
158
|
+
raise RTwitterException,messages.join("\n")
|
159
|
+
end
|
160
|
+
end
|
161
|
+
return obj
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
def escape(value)
|
166
|
+
|
167
|
+
URI.escape(value.to_s,/[^a-zA-Z0-9\-\.\_\~]/)
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
def post_request(url,body,header)
|
172
|
+
|
173
|
+
uri = URI.parse(url)
|
174
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
175
|
+
if uri.port == 443
|
176
|
+
https.use_ssl = true
|
177
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
178
|
+
end
|
179
|
+
response = https.start{|https|
|
180
|
+
https.post(uri.request_uri,body,header)
|
181
|
+
}
|
182
|
+
return response
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
def get_request(url,body,header)
|
187
|
+
|
188
|
+
uri = URI.parse(url)
|
189
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
190
|
+
if uri.port == 443
|
191
|
+
https.use_ssl = true
|
192
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
193
|
+
end
|
194
|
+
response = https.start{|https|
|
195
|
+
https.get(uri.request_uri + '?' + body, header)
|
196
|
+
}
|
197
|
+
return response
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
def streaming_request(url,body,header)
|
202
|
+
|
203
|
+
uri = URI.parse(url)
|
204
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
205
|
+
if uri.port == 443
|
206
|
+
https.use_ssl = true
|
207
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
208
|
+
end
|
209
|
+
request = Net::HTTP::Get.new(uri.request_uri + '?' + body,header)
|
210
|
+
https.request(request){|response|
|
211
|
+
response.read_body{|chunk|
|
212
|
+
yield chunk
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
def url(endpoint)
|
219
|
+
if /^https:/ =~ endpoint
|
220
|
+
return endpoint
|
221
|
+
end
|
222
|
+
list = {
|
223
|
+
'media/upload' => 'https://upload.twitter.com/1.1/media/upload.json',
|
224
|
+
'statuses/filter' => 'https://stream.twitter.com/1.1/statuses/filter.json',
|
225
|
+
'statuses/sample' => 'https://stream.twitter.com/1.1/statuses/sample.json',
|
226
|
+
'user' => 'https://userstream.twitter.com/1.1/user.json',
|
227
|
+
'site' => 'https://sitestream.twitter.com/1.1/site.json'
|
228
|
+
}
|
229
|
+
if list.include?(endpoint)
|
230
|
+
return list[endpoint]
|
231
|
+
else
|
232
|
+
return "https://api.twitter.com/1.1/#{endpoint}.json"
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
def build_query(params)
|
238
|
+
|
239
|
+
query = params.map{|key,value|
|
240
|
+
"#{escape(key)}=#{escape(value)}"
|
241
|
+
}.join('&')
|
242
|
+
return query
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
def build_header(params)
|
247
|
+
|
248
|
+
header = params.map{|key,value|
|
249
|
+
"#{escape(key)}=\"#{escape(value)}\""
|
250
|
+
}.join(',')
|
251
|
+
return header
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
def build_body(params)
|
256
|
+
|
257
|
+
body = params.map{|key,value|
|
258
|
+
"#{escape(key)}=#{escape(value)}"
|
259
|
+
}.join('&')
|
260
|
+
return body
|
261
|
+
|
262
|
+
end
|
261
263
|
|
262
|
-
|
263
|
-
|
264
|
+
class RTwitterException < RuntimeError; end
|
265
|
+
end
|
264
266
|
|
265
267
|
end
|
data/lib/RTwitter/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: RTwitter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Seurix
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|