RTwitter 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/RTwitter.gemspec +23 -0
- data/Rakefile +1 -0
- data/lib/RTwitter/version.rb +3 -0
- data/lib/RTwitter.rb +267 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# RTwitter
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'RTwitter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install RTwitter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/RTwitter.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'RTwitter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "RTwitter"
|
8
|
+
spec.version = RTwitter::VERSION
|
9
|
+
spec.authors = ["Seurix"]
|
10
|
+
spec.email = ["yfmke.0304@gmail.com"]
|
11
|
+
spec.description = %q{Twitter OAuth Library for Ruby}
|
12
|
+
spec.summary = %q{Twitter OAuth Library for Ruby}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/RTwitter.rb
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
require "RTwitter/version"
|
2
|
+
|
3
|
+
require'base64'
|
4
|
+
require'openssl'
|
5
|
+
require'uri'
|
6
|
+
require'json'
|
7
|
+
require'net/http'
|
8
|
+
|
9
|
+
module RTwitter
|
10
|
+
|
11
|
+
class OAuth
|
12
|
+
|
13
|
+
attr_reader :consumer_key,:consumer_key_secret,:access_token,:access_token_secret,:user_id,:screen_name
|
14
|
+
attr_accessor :userAgent
|
15
|
+
def initialize(ck ,cks ,at = nil ,ats = nil)
|
16
|
+
@consumer_key = ck
|
17
|
+
@consumer_key_secret = cks
|
18
|
+
@access_token = at
|
19
|
+
@access_token_secret = ats
|
20
|
+
@userAgent = 'RTwitter'
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def request_token
|
25
|
+
|
26
|
+
oauth_params = oauth
|
27
|
+
oauth_params.delete('oauth_token')
|
28
|
+
oauth_params['oauth_callback'] = 'oob'
|
29
|
+
base_params = Hash[oauth_params.sort]
|
30
|
+
query = build_query(base_params)
|
31
|
+
url = 'https://api.twitter.com/oauth/request_token'
|
32
|
+
base = 'POST&' + escape(url) + '&' + escape(query)
|
33
|
+
key = @consumer_key_secret + '&'
|
34
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
35
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
36
|
+
response = post_request(url,'',header)
|
37
|
+
|
38
|
+
items = response.body.split('&')
|
39
|
+
@request_token = items[0].split('=')[1]
|
40
|
+
@request_token_secret = items[1].split('=')[1]
|
41
|
+
return "https://api.twitter.com/oauth/authenticate?oauth_token=#{@request_token}"
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def access_token(pin)
|
46
|
+
|
47
|
+
oauth_params = oauth
|
48
|
+
oauth_params.delete('oauth_token')
|
49
|
+
oauth_params['oauth_verifier'] = pin.chomp
|
50
|
+
oauth_params['oauth_token'] = @request_token
|
51
|
+
base_params = Hash[oauth_params.sort]
|
52
|
+
query = build_query(base_params)
|
53
|
+
url = 'https://api.twitter.com/oauth/access_token'
|
54
|
+
base = 'POST&' + escape(url) + '&' + escape(query)
|
55
|
+
key = @consumer_key_secret + '&' + @request_token_secret
|
56
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
57
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
58
|
+
body = ''
|
59
|
+
response = post_request(url,body,header)
|
60
|
+
|
61
|
+
access_tokens = response.body.split('&')
|
62
|
+
@access_token = access_tokens[0].split('=')[1]
|
63
|
+
@access_token_secret = access_tokens[1].split('=')[1]
|
64
|
+
@user_id = access_tokens[2].split('=')[1]
|
65
|
+
@screen_name = access_tokens[3].split('=')[1]
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def post(endpoint,additional_params = Hash.new)
|
70
|
+
|
71
|
+
url = url(endpoint)
|
72
|
+
header = signature('POST',url,additional_params)
|
73
|
+
body = build_body(additional_params)
|
74
|
+
response = post_request(url,body,header)
|
75
|
+
return decode(response)
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def get(endpoint,additional_params = Hash.new)
|
80
|
+
|
81
|
+
url = url(endpoint)
|
82
|
+
header = signature('GET',url,additional_params)
|
83
|
+
body = build_body(additional_params)
|
84
|
+
response = get_request(url,body,header)
|
85
|
+
return decode(response)
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def streaming(endpoint,additional_params = Hash.new)
|
90
|
+
|
91
|
+
url = url(endpoint)
|
92
|
+
header = signature('GET',url,additional_params)
|
93
|
+
body = build_body(additional_params)
|
94
|
+
buffer = ''
|
95
|
+
streaming_request(url,body,header){|chunk|
|
96
|
+
if buffer != ''
|
97
|
+
chunk = buffer + chunk
|
98
|
+
buffer = ''
|
99
|
+
end
|
100
|
+
|
101
|
+
begin
|
102
|
+
status = JSON.parse(chunk)
|
103
|
+
rescue
|
104
|
+
buffer << chunk
|
105
|
+
next
|
106
|
+
end
|
107
|
+
|
108
|
+
yield status
|
109
|
+
}
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def signature(method,url,additional_params)
|
115
|
+
oauth_params = oauth
|
116
|
+
base_params = oauth_params.merge(additional_params)
|
117
|
+
base_params = Hash[base_params.sort]
|
118
|
+
query = build_query(base_params)
|
119
|
+
base = method + '&' + escape(url) + '&' + escape(query)
|
120
|
+
key = @consumer_key_secret + '&' + @access_token_secret
|
121
|
+
oauth_params['oauth_signature'] = Base64.encode64(OpenSSL::HMAC.digest("sha1",key, base)).chomp
|
122
|
+
header = {'Authorization' => 'OAuth ' + build_header(oauth_params),'User-Agent' => @userAgent}
|
123
|
+
return header
|
124
|
+
end
|
125
|
+
|
126
|
+
def oauth
|
127
|
+
{
|
128
|
+
'oauth_consumer_key' => @consumer_key,
|
129
|
+
'oauth_signature_method' => 'HMAC-SHA1',
|
130
|
+
'oauth_timestamp' => Time.now.to_i.to_s,
|
131
|
+
'oauth_version' => '1.0',
|
132
|
+
'oauth_nonce' => Random.new_seed.to_s,
|
133
|
+
'oauth_token' => @access_token
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
137
|
+
def decode(response)
|
138
|
+
if response.body == nil
|
139
|
+
raise RTwitterException,'Failed to receive response.'
|
140
|
+
end
|
141
|
+
if response.body == ''
|
142
|
+
raise RTwitterException,'Empty response.'
|
143
|
+
end
|
144
|
+
begin
|
145
|
+
obj = JSON.parse(response.body)
|
146
|
+
rescue
|
147
|
+
return response.body
|
148
|
+
end
|
149
|
+
if obj.include?('error')
|
150
|
+
raise RTwitterException,obj['error']
|
151
|
+
end
|
152
|
+
if obj.include?('errors')
|
153
|
+
if obj['errors'].kind_of?(String)
|
154
|
+
raise RTwitterException,obj['errors']
|
155
|
+
else
|
156
|
+
messages = []
|
157
|
+
obj['errors'].each{|errors|
|
158
|
+
messages << errors['message']
|
159
|
+
}
|
160
|
+
raise RTwitterException,messages.join("\n")
|
161
|
+
end
|
162
|
+
end
|
163
|
+
return obj
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def escape(value)
|
168
|
+
|
169
|
+
URI.escape(value.to_s,/[^a-zA-Z0-9\-\.\_\~]/)
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
def post_request(url,body,header)
|
174
|
+
|
175
|
+
uri = URI.parse(url)
|
176
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
177
|
+
if uri.port == 443
|
178
|
+
https.use_ssl = true
|
179
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
180
|
+
end
|
181
|
+
response = https.start{|https|
|
182
|
+
https.post(uri.request_uri,body,header)
|
183
|
+
}
|
184
|
+
return response
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
def get_request(url,body,header)
|
189
|
+
|
190
|
+
uri = URI.parse(url)
|
191
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
192
|
+
if uri.port == 443
|
193
|
+
https.use_ssl = true
|
194
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
195
|
+
end
|
196
|
+
response = https.start{|https|
|
197
|
+
https.get(uri.request_uri + '?' + body, header)
|
198
|
+
}
|
199
|
+
return response
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
def streaming_request(url,body,header)
|
204
|
+
|
205
|
+
uri = URI.parse(url)
|
206
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
207
|
+
if uri.port == 443
|
208
|
+
https.use_ssl = true
|
209
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
210
|
+
end
|
211
|
+
request = Net::HTTP::Get.new(uri.request_uri + '?' + body,header)
|
212
|
+
https.request(request){|response|
|
213
|
+
response.read_body{|chunk|
|
214
|
+
yield chunk
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
def url(endpoint)
|
221
|
+
|
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
|
263
|
+
|
264
|
+
class RTwitterException < RuntimeError; end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: RTwitter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seurix
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Twitter OAuth Library for Ruby
|
47
|
+
email:
|
48
|
+
- yfmke.0304@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- RTwitter.gemspec
|
58
|
+
- Rakefile
|
59
|
+
- lib/RTwitter.rb
|
60
|
+
- lib/RTwitter/version.rb
|
61
|
+
homepage: ''
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: -2960072803349443897
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: -2960072803349443897
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Twitter OAuth Library for Ruby
|
92
|
+
test_files: []
|