tida_sina_weibo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -3
- data/lib/tida_sina_weibo/api_wrapper.rb +58 -46
- data/lib/tida_sina_weibo/exceptions.rb +41 -0
- data/lib/tida_sina_weibo/rest_client_wrapper.rb +21 -0
- data/lib/tida_sina_weibo/version.rb +1 -1
- data/lib/tida_sina_weibo.rb +11 -0
- data/spec/api_wrapper_spec.rb +61 -0
- data/tida_sina_weibo.gemspec +5 -0
- metadata +55 -5
- data/lib/tida_sina_weibo/api_settings.rb +0 -8
- data/lib/tida_sina_weibo/api_settings.yml +0 -32
data/Gemfile
CHANGED
@@ -1,71 +1,96 @@
|
|
1
1
|
require "rest-client"
|
2
|
-
require "api_settings"
|
3
2
|
|
4
3
|
module TidaSinaWeibo
|
4
|
+
|
5
|
+
API = {}
|
6
|
+
API[:status] = {}
|
7
|
+
API[:status][:update] = 'https://api.weibo.com/2/statuses/update.json'
|
8
|
+
API[:status][:upload] = 'https://upload.api.weibo.com/2/statuses/upload.json'
|
9
|
+
API[:status][:user_timeline] = 'https://api.weibo.com/2/statuses/user_timeline.json'
|
10
|
+
API[:status][:user_timeline_ids] = 'https://api.weibo.com/2/statuses/user_timeline/ids.json'
|
11
|
+
API[:status][:show] = 'https://api.weibo.com/2/statuses/show.json'
|
12
|
+
API[:status][:repost_timeline] = 'https://api.weibo.com/2/statuses/repost_timeline.json'
|
13
|
+
API[:status][:repost_timeline_ids] = 'https://api.weibo.com/2/statuses/repost_timeline/ids.json'
|
14
|
+
API[:status][:queryid] = 'https://api.weibo.com/2/statuses/queryid.json'
|
15
|
+
API[:status][:querymid] = 'https://api.weibo.com/2/statuses/querymid.json'
|
16
|
+
API[:comments] = {}
|
17
|
+
API[:comments][:show] = 'https://api.weibo.com/2/comments/show.json'
|
18
|
+
API[:user] = {}
|
19
|
+
API[:user][:show] = 'https://api.weibo.com/2/users/show.json'
|
20
|
+
API[:user][:domain_show] = 'https://api.weibo.com/2/users/domain_show.json'
|
21
|
+
API[:tags] = 'https://api.weibo.com/2/tags.json'
|
22
|
+
API[:friendships] = {}
|
23
|
+
API[:friendships][:followers] = 'https://api.weibo.com/2/friendships/followers.json'
|
24
|
+
API[:friendships][:followers_ids] = 'https://api.weibo.com/2/friendships/followers/ids.json'
|
25
|
+
API[:friendships][:show] = 'https://api.weibo.com/2/friendships/show.json'
|
26
|
+
|
5
27
|
class ApiWrapper
|
28
|
+
|
6
29
|
attr_reader :access_token
|
7
|
-
attr_reader :token_provider
|
8
30
|
attr_reader :request_data
|
31
|
+
attr_reader :token_provider
|
32
|
+
attr_reader :rest_client
|
9
33
|
|
10
|
-
def initialize(
|
11
|
-
@
|
12
|
-
@
|
34
|
+
def initialize(rest_client, tp)
|
35
|
+
@rest_client = rest_client
|
36
|
+
@token_provider = tp
|
37
|
+
@access_token = tp.token
|
13
38
|
end
|
14
39
|
|
15
40
|
def status_update(content)
|
16
|
-
post_request(
|
41
|
+
post_request(API[:status][:update], :status => content)
|
17
42
|
end
|
18
43
|
|
19
44
|
def status_upload(content, image_path)
|
20
|
-
post_request(
|
45
|
+
post_request(API[:status][:upload], :status => content, :pic => File.new(image_path, 'rb'))
|
21
46
|
end
|
22
47
|
|
23
48
|
def status_user_timeline(uid, page = 1, count = 100)
|
24
|
-
get_request(
|
49
|
+
get_request(API[:status][:user_timeline], :params => {:uid => uid, :page => page, :count => count})
|
25
50
|
end
|
26
51
|
|
27
52
|
def status_repost_timeline(post_id, page = 1)
|
28
|
-
get_request(
|
53
|
+
get_request(API[:status][:repost_timeline], :params => {:id => post_id, :page => page, :count => 50})
|
29
54
|
end
|
30
55
|
|
31
56
|
def comments_show(post_id, page = 1)
|
32
|
-
get_request(
|
57
|
+
get_request(API[:comments][:show], :params => {:id => post_id, :page => page, :count => 50})
|
33
58
|
end
|
34
59
|
|
35
60
|
def status_show(wb_post_id)
|
36
|
-
get_request(
|
61
|
+
get_request(API[:status][:show], :params => {:id => wb_post_id})
|
37
62
|
end
|
38
63
|
|
39
64
|
def user_show(uid, is_number_id = true)
|
40
65
|
if is_number_id
|
41
|
-
get_request(
|
66
|
+
get_request(API[:user][:show], {:params => {:uid => uid}})
|
42
67
|
else
|
43
|
-
get_request(
|
68
|
+
get_request(API[:user][:show], {:params => {:screen_name => uid}})
|
44
69
|
end
|
45
70
|
end
|
46
71
|
|
47
72
|
def user_domain_show(domain)
|
48
|
-
get_request(
|
73
|
+
get_request(API[:user][:domain_show], :params => {:domain => domain})
|
49
74
|
end
|
50
75
|
|
51
76
|
def friendships_followers(uid, cursor)
|
52
|
-
get_request(
|
77
|
+
get_request(API[:friendships][:followers], :params => {:uid => uid, :cursor => cursor})
|
53
78
|
end
|
54
79
|
|
55
80
|
def friendships_followers_ids(uid, cursor)
|
56
|
-
get_request(
|
81
|
+
get_request(API[:friendships][:followers_ids], :params => {:uid => uid, :cursor => cursor})
|
57
82
|
end
|
58
83
|
|
59
84
|
def tags(uid)
|
60
|
-
get_request(
|
85
|
+
get_request(API[:tags], :params => {:uid => uid})
|
61
86
|
end
|
62
87
|
|
63
88
|
def queryid(mid)
|
64
|
-
|
89
|
+
get_request(API[:status][:queryid], {:params => {:mid => mid, :type => 1, :isBase62 => 1}})
|
65
90
|
end
|
66
91
|
|
67
92
|
def querymid(id, batch = false)
|
68
|
-
|
93
|
+
get_request(API[:status][:querymid], {:params => {:id => id, :type => 1, :is_batch => batch ? 1 : 0}})
|
69
94
|
end
|
70
95
|
|
71
96
|
def querymids(ids)
|
@@ -80,8 +105,7 @@ module TidaSinaWeibo
|
|
80
105
|
end
|
81
106
|
|
82
107
|
def get_request(url, data)
|
83
|
-
|
84
|
-
return r
|
108
|
+
rest_request(url, data, false)
|
85
109
|
end
|
86
110
|
|
87
111
|
def rest_request(url, data, method_post)
|
@@ -99,23 +123,16 @@ module TidaSinaWeibo
|
|
99
123
|
rd = data.clone
|
100
124
|
params = {:access_token => @access_token}
|
101
125
|
rd[:params] = data[:params].merge(params)
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
else
|
110
|
-
r = JSON.parse RestClient.get(url, rd)
|
111
|
-
Rails.logger.debug "- Request response: #{r}"
|
112
|
-
return r, 0
|
113
|
-
end
|
126
|
+
|
127
|
+
if method_post
|
128
|
+
r = rest_client.post(url, rd)
|
129
|
+
return r, 0
|
130
|
+
else
|
131
|
+
r = rest_client.get(url, rd)
|
132
|
+
return r, 0
|
114
133
|
end
|
115
|
-
rescue
|
116
|
-
|
117
|
-
r = JSON.parse e.http_body
|
118
|
-
Rails.logger.error "! Sina Weibo API Error Code: #{r["error_code"]} Error: #{r["error"]}"
|
134
|
+
rescue RestClientException => e
|
135
|
+
r = e.body
|
119
136
|
api_error_code = r["error_code"].to_i
|
120
137
|
|
121
138
|
if invalid_access_token? api_error_code
|
@@ -127,15 +144,12 @@ module TidaSinaWeibo
|
|
127
144
|
end
|
128
145
|
|
129
146
|
if reached_ip_access_limit? api_error_code
|
130
|
-
|
147
|
+
e = IPAccessLimitException.new self.access_token
|
148
|
+
raise e
|
131
149
|
end
|
132
150
|
|
133
151
|
return nil, -2
|
134
152
|
end
|
135
|
-
|
136
|
-
def parse_response(&block)
|
137
|
-
JSON.parse(block.call)
|
138
|
-
end
|
139
153
|
|
140
154
|
def invalid_access_token?(error_code)
|
141
155
|
return error_code == 21332 || error_code == 21327
|
@@ -150,13 +164,11 @@ module TidaSinaWeibo
|
|
150
164
|
end
|
151
165
|
|
152
166
|
def change_access_token
|
153
|
-
Rails.logger.info "* Changing access token}"
|
154
|
-
Rails.logger.info "- Old access token is #{@access_token}"
|
155
167
|
self.token_provider.change
|
156
168
|
@access_token = self.token_provider.token
|
157
|
-
Rails.logger.info "- New access token is #{@access_token}"
|
158
169
|
if @access_token.nil?
|
159
|
-
|
170
|
+
e = NoAccessTokenAvaiableException.new
|
171
|
+
raise e
|
160
172
|
end
|
161
173
|
end
|
162
174
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module TidaSinaWeibo
|
4
|
+
|
5
|
+
class Exception < RuntimeError
|
6
|
+
attr_accessor :message
|
7
|
+
|
8
|
+
def initialize message = nil
|
9
|
+
@message = message
|
10
|
+
end
|
11
|
+
|
12
|
+
def inspect
|
13
|
+
self.message
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
inspect
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class NoAccessTokenAvaiableException < Exception
|
22
|
+
def initialize
|
23
|
+
super "There is not any valid access token"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class IPAccessLimitException < Exception
|
28
|
+
def initialize(token)
|
29
|
+
super "The access token #{token} reached IP access limit"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class RestClientException < RestClient::Exception
|
34
|
+
attr_reader :body
|
35
|
+
|
36
|
+
def initialize(rest_client_exception)
|
37
|
+
@body = ::JSON.parse rest_client_exception.http_body if rest_client_exception
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module TidaSinaWeibo
|
4
|
+
|
5
|
+
class RestClientWrapper
|
6
|
+
def get(url, data)
|
7
|
+
JSON.parse RestClient.get(url, data)
|
8
|
+
rescue RestClient::Exception => e
|
9
|
+
ex = RestClientException.new e
|
10
|
+
raise ex
|
11
|
+
end
|
12
|
+
|
13
|
+
def post(url, data)
|
14
|
+
JSON.parse RestClient.post(url, data)
|
15
|
+
rescue RestClient::Exception => e
|
16
|
+
ex = RestClientException.new e
|
17
|
+
raise ex
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/tida_sina_weibo.rb
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
|
1
2
|
require "tida_sina_weibo/version"
|
3
|
+
require 'tida_sina_weibo/exceptions'
|
2
4
|
require 'tida_sina_weibo/api_wrapper'
|
5
|
+
require 'tida_sina_weibo/rest_client_wrapper'
|
6
|
+
|
7
|
+
module TidaSinaWeibo
|
8
|
+
|
9
|
+
def api_wrapper(tp)
|
10
|
+
ApiWrapper.new RestClientWrapper.new, tp
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
3
14
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'tida_sina_weibo'
|
2
|
+
|
3
|
+
describe TidaSinaWeibo::ApiWrapper do
|
4
|
+
it 'get setting property' do
|
5
|
+
TidaSinaWeibo::API[:status][:update].should eql('https://api.weibo.com/2/statuses/update.json')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'get request success' do
|
9
|
+
request_wrapper = double('request_wrapper')
|
10
|
+
request_wrapper.stub(:get) {{'result' => 'ok'}}
|
11
|
+
|
12
|
+
token_provider = double('token_provider')
|
13
|
+
token_provider.stub(:token) { 'token'}
|
14
|
+
|
15
|
+
target = TidaSinaWeibo::ApiWrapper.new request_wrapper, token_provider
|
16
|
+
result = target.status_show '1'
|
17
|
+
result.should eql({'result' =>'ok'})
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'get request raise invalid_access_token exception' do
|
21
|
+
rest_client_exception = TidaSinaWeibo::RestClientException.new nil
|
22
|
+
|
23
|
+
request_wrapper = double('request_wrapper')
|
24
|
+
request_wrapper.should_receive(:get).and_raise(rest_client_exception)
|
25
|
+
|
26
|
+
rest_client_exception.stub(:body) { {'error_code' => '21332'} }
|
27
|
+
|
28
|
+
token_provider = double('token_provider')
|
29
|
+
token_provider.stub(:token) { 'token' }
|
30
|
+
token_provider.stub(:change)
|
31
|
+
token_provider.stub(:token) { nil }
|
32
|
+
|
33
|
+
target = TidaSinaWeibo::ApiWrapper.new request_wrapper, token_provider
|
34
|
+
begin
|
35
|
+
result = target.status_show '1'
|
36
|
+
raise 'Expect exception raise'
|
37
|
+
rescue TidaSinaWeibo::NoAccessTokenAvaiableException
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'get request raise reached_account_access_limit exception' do
|
42
|
+
rest_client_exception = TidaSinaWeibo::RestClientException.new nil
|
43
|
+
|
44
|
+
request_wrapper = double('request_wrapper')
|
45
|
+
request_wrapper.should_receive(:get).and_raise(rest_client_exception)
|
46
|
+
|
47
|
+
rest_client_exception.stub(:body) { {'error_code' => '10023'} }
|
48
|
+
|
49
|
+
token_provider = double('token_provider')
|
50
|
+
token_provider.stub(:token) { 'token' }
|
51
|
+
token_provider.stub(:change)
|
52
|
+
token_provider.stub(:token) { nil }
|
53
|
+
|
54
|
+
target = TidaSinaWeibo::ApiWrapper.new request_wrapper, token_provider
|
55
|
+
begin
|
56
|
+
result = target.status_show '1'
|
57
|
+
raise 'Expect exception raise'
|
58
|
+
rescue TidaSinaWeibo::NoAccessTokenAvaiableException
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/tida_sina_weibo.gemspec
CHANGED
@@ -16,4 +16,9 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec", "~> 2.6"
|
21
|
+
|
22
|
+
gem.add_dependency "settingslogic"
|
23
|
+
gem.add_dependency "rest-client"
|
19
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tida_sina_weibo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,55 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2013-01-08 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.6'
|
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: '2.6'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: settingslogic
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rest-client
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
14
62
|
description: SinaWeibo API gem
|
15
63
|
email:
|
16
64
|
- babaru.trinit@gmail.com
|
@@ -24,10 +72,11 @@ files:
|
|
24
72
|
- README.md
|
25
73
|
- Rakefile
|
26
74
|
- lib/tida_sina_weibo.rb
|
27
|
-
- lib/tida_sina_weibo/api_settings.rb
|
28
|
-
- lib/tida_sina_weibo/api_settings.yml
|
29
75
|
- lib/tida_sina_weibo/api_wrapper.rb
|
76
|
+
- lib/tida_sina_weibo/exceptions.rb
|
77
|
+
- lib/tida_sina_weibo/rest_client_wrapper.rb
|
30
78
|
- lib/tida_sina_weibo/version.rb
|
79
|
+
- spec/api_wrapper_spec.rb
|
31
80
|
- tida_sina_weibo.gemspec
|
32
81
|
homepage: ''
|
33
82
|
licenses: []
|
@@ -53,4 +102,5 @@ rubygems_version: 1.8.24
|
|
53
102
|
signing_key:
|
54
103
|
specification_version: 3
|
55
104
|
summary: SinaWeibo API gem
|
56
|
-
test_files:
|
105
|
+
test_files:
|
106
|
+
- spec/api_wrapper_spec.rb
|
@@ -1,32 +0,0 @@
|
|
1
|
-
defaults: &defaults
|
2
|
-
api:
|
3
|
-
status:
|
4
|
-
update: 'https://api.weibo.com/2/statuses/update.json'
|
5
|
-
upload: 'https://upload.api.weibo.com/2/statuses/upload.json'
|
6
|
-
user_timeline: 'https://api.weibo.com/2/statuses/user_timeline.json'
|
7
|
-
user_timeline_ids: 'https://api.weibo.com/2/statuses/user_timeline/ids.json'
|
8
|
-
show: 'https://api.weibo.com/2/statuses/show.json'
|
9
|
-
repost_timeline: 'https://api.weibo.com/2/statuses/repost_timeline.json'
|
10
|
-
repost_timeline_ids: 'https://api.weibo.com/2/statuses/repost_timeline/ids.json'
|
11
|
-
queryid: 'https://api.weibo.com/2/statuses/queryid.json'
|
12
|
-
querymid: 'https://api.weibo.com/2/statuses/querymid.json'
|
13
|
-
comments:
|
14
|
-
show: 'https://api.weibo.com/2/comments/show.json'
|
15
|
-
user:
|
16
|
-
show: 'https://api.weibo.com/2/users/show.json'
|
17
|
-
domain_show: 'https://api.weibo.com/2/users/domain_show.json'
|
18
|
-
tags: 'https://api.weibo.com/2/tags.json'
|
19
|
-
friendships:
|
20
|
-
followers: 'https://api.weibo.com/2/friendships/followers.json'
|
21
|
-
followers_ids: 'https://api.weibo.com/2/friendships/followers/ids.json'
|
22
|
-
show: 'https://api.weibo.com/2/friendships/show.json'
|
23
|
-
|
24
|
-
|
25
|
-
development:
|
26
|
-
<<: *defaults
|
27
|
-
|
28
|
-
test:
|
29
|
-
<<: *defaults
|
30
|
-
|
31
|
-
production:
|
32
|
-
<<: *defaults
|