baidu-sdk 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/HISTORY.md +13 -0
- data/README.md +13 -0
- data/lib/baidu/oauth.rb +2 -0
- data/lib/baidu/oauth/rest_client.rb +334 -0
- data/lib/baidu/version.rb +1 -1
- data/spec/baidu/oauth/client_spec.rb +57 -63
- data/spec/baidu/oauth/rest_client_spec.rb +313 -0
- data/spec/fixtures/are_friends.json +14 -0
- data/spec/fixtures/get_friends.json +12 -0
- data/spec/spec_helper.rb +16 -4
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3af6ada389f6a63f94e817ce4a13d344b73461f5
|
4
|
+
data.tar.gz: c769fddcdea66b5cb027fd123f7e366e17bae183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f354a1ae052d10dacc1160c23f94f90c2eeea602dd437e134383e4771d5bc43e1ece0e168445f7c9c6ce74518855e47175911e825bac7f45a1f157e1b078abb
|
7
|
+
data.tar.gz: 613e6c1f4b7ab7a2b0bdc5c72c9f8382cf26fdc0e2006922ac85cd9cedc02d920f57cfa1066df76b516154c5847d87bd34496810a9b22fb0b5c4ee085fe9ee14
|
data/.travis.yml
CHANGED
data/HISTORY.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## 0.0.3 (2013-12-30)
|
2
|
+
* 添加部分 REST API, `Baidu::OAuth::RESTClient`
|
3
|
+
* get_logged_in_user
|
4
|
+
* get_info
|
5
|
+
* app_user?
|
6
|
+
* has_app_permission?
|
7
|
+
* has_app_permissions
|
8
|
+
* get_friends
|
9
|
+
* are_friends
|
10
|
+
* expire_session
|
11
|
+
* revoke_authorization
|
12
|
+
* query_ip
|
13
|
+
|
1
14
|
## 0.0.2 (2013-12-06)
|
2
15
|
* **OAuth** 添加 Implicit Grant 授权流程
|
3
16
|
* **OAuth** 添加 Client Credentials 授权流程
|
data/README.md
CHANGED
@@ -149,6 +149,19 @@ get '/auth/callback' do
|
|
149
149
|
end
|
150
150
|
```
|
151
151
|
|
152
|
+
### REST API
|
153
|
+
通过百度开放平台提供的 REST API,第三方应用可以获取到百度用户的用户资料、好友关系等基本信息,以及今后百度开放的其他任何数据,但前提是应用必须获得到百度开放平台和百度用户的授权。
|
154
|
+
|
155
|
+
当前实现了用户信息类接口、好友关系类接口、用户授权类接口和工具类接口。
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
rest_client = Baidu::OAuth::RESTClient.new('my_token...')
|
159
|
+
puts rest_client.get_logged_in_user
|
160
|
+
puts rest_client.get_info
|
161
|
+
puts rest_client.expire_session
|
162
|
+
# puts rest_client.revoke_authorization
|
163
|
+
```
|
164
|
+
|
152
165
|
## PCS
|
153
166
|
要使用 PCS 必须到 [百度开发者中心](http://developer.baidu.com/console) 开启指定应用的 PCS API 权限,参考 [开通PCS API权限](http://developer.baidu.com/wiki/index.php?title=docs/pcs/guide/api_approve),并获取所设置的文件目录。
|
154
167
|
|
data/lib/baidu/oauth.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'baidu/core'
|
2
2
|
require 'baidu/oauth/client'
|
3
|
+
require 'baidu/oauth/rest_client'
|
3
4
|
|
4
5
|
module Baidu
|
5
6
|
module OAuth
|
6
7
|
SITE = 'https://openapi.baidu.com'
|
8
|
+
BASE_PATH = '/rest/2.0'
|
7
9
|
AUTHORIZATION_ENDPOINT = '/oauth/2.0/authorize'
|
8
10
|
TOKEN_ENDPOINT = '/oauth/2.0/token'
|
9
11
|
TOKEN_INFO_ENDPOINT = '/oauth/2.0/tokeninfo'
|
@@ -0,0 +1,334 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module Baidu
|
4
|
+
module OAuth
|
5
|
+
# 通过百度开放平台提供的REST API,第三方应用可以获取到百度用户的用户资料、
|
6
|
+
# 好友关系等基本信息,以及今后百度开放的其他任何数据,
|
7
|
+
# 但前提是应用必须获得到百度开放平台和百度用户的授权。
|
8
|
+
class RESTClient
|
9
|
+
include Baidu::Support::Request
|
10
|
+
|
11
|
+
# 创建一个 REST API Client 实例
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# rest_client = Baidu::OAuth::RESTClient.new('my_token...')
|
15
|
+
#
|
16
|
+
# @overload initialize(access_token)
|
17
|
+
# @param [String] access_token
|
18
|
+
#
|
19
|
+
# @overload initialize(session)
|
20
|
+
# @param [Baidu::Session] session
|
21
|
+
def initialize(access_token_or_session)
|
22
|
+
@access_token = case access_token_or_session
|
23
|
+
when String then access_token_or_session
|
24
|
+
when Baidu::Session then access_token_or_session.access_token
|
25
|
+
else
|
26
|
+
raise ArgumentError, 'need a String or Baidu::Session'
|
27
|
+
end
|
28
|
+
@site = Baidu::OAuth::SITE
|
29
|
+
end
|
30
|
+
|
31
|
+
# 获取当前登录用户的简单信息
|
32
|
+
#
|
33
|
+
# 获取当前登录用户的用户uid、用户名和头像。
|
34
|
+
# @note 如果存储用户信息的话,请大家以百度用户uid为主键
|
35
|
+
#
|
36
|
+
# @example 返回的原始 JSON
|
37
|
+
# {
|
38
|
+
# "uid": "256758493",
|
39
|
+
# "uname": "XiaoLu",
|
40
|
+
# "portrait": "b4994c6849380284916e67af0c"
|
41
|
+
# }
|
42
|
+
#
|
43
|
+
# :uid 当前登录用户的数字ID
|
44
|
+
# :uname 当前登录用户的用户名。只用于显示,可能会发生变化,不保证全局唯一
|
45
|
+
# :portrait 当前登录用户的头像
|
46
|
+
#
|
47
|
+
# 头像的地址具体如下:
|
48
|
+
# small image: http://tb.himg.baidu.com/sys/portraitn/item/{$portrait}
|
49
|
+
# large image: http://tb.himg.baidu.com/sys/portrait/item/{$portrait}
|
50
|
+
#
|
51
|
+
# @return [Hash]
|
52
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E8.8E.B7.E5.8F.96.E5.BD.93.E5.89.8D.E7.99.BB.E5.BD.95.E7.94.A8.E6.88.B7.E7.9A.84.E7.AE.80.E5.8D.95.E4.BF.A1.E6.81.AF 获取当前登录用户的信息
|
53
|
+
def get_logged_in_user
|
54
|
+
api_request '/passport/users/getLoggedInUser'
|
55
|
+
end
|
56
|
+
|
57
|
+
# 返回用户详细资料
|
58
|
+
# @note 如果存储用户信息的话,请大家以百度用户uid为主键
|
59
|
+
#
|
60
|
+
# @example 返回的原始 JSON
|
61
|
+
# {
|
62
|
+
# "userid":"2097322476",
|
63
|
+
# "username":"wl19871011",
|
64
|
+
# "realname":"阳光",
|
65
|
+
# "userdetail":"喜欢自由",
|
66
|
+
# "birthday":"1987-01-01",
|
67
|
+
# "marriage":"恋爱",
|
68
|
+
# "sex":"男",
|
69
|
+
# "blood":"O",
|
70
|
+
# "constellation":"射手",
|
71
|
+
# "figure":"小巧",
|
72
|
+
# "education":"大学/专科",
|
73
|
+
# "trade":"计算机/电子产品",
|
74
|
+
# "job":"未知",
|
75
|
+
# "birthday_year":"1987",
|
76
|
+
# "birthday_month":"01",
|
77
|
+
# "birthday_day":"01"
|
78
|
+
# }
|
79
|
+
#
|
80
|
+
# :userid 当前登录用户的数字ID
|
81
|
+
# :username 当前登录用户的用户名,值可能为空。
|
82
|
+
# :realname 用户真实姓名,可能为空。
|
83
|
+
# :portrait 当前登录用户的头像
|
84
|
+
# :userdetail 自我简介,可能为空。
|
85
|
+
# :birthday 生日,以yyyy-mm-dd格式显示。
|
86
|
+
# :marriage 婚姻状况
|
87
|
+
# :sex 性别
|
88
|
+
# :blood 血型
|
89
|
+
# :figure 体型
|
90
|
+
# :constellation 星座
|
91
|
+
# :education 学历
|
92
|
+
# :trade 当前职业
|
93
|
+
# :job 职位
|
94
|
+
#
|
95
|
+
# 头像的地址具体如下:
|
96
|
+
# small image: http://tb.himg.baidu.com/sys/portraitn/item/{$portrait}
|
97
|
+
# large image: http://tb.himg.baidu.com/sys/portrait/item/{$portrait}
|
98
|
+
#
|
99
|
+
# @return [Hash]
|
100
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E8.BF.94.E5.9B.9E.E7.94.A8.E6.88.B7.E8.AF.A6.E7.BB.86.E8.B5.84.E6.96.99 返回用户详细资料
|
101
|
+
def get_info
|
102
|
+
api_request '/passport/users/getInfo'
|
103
|
+
end
|
104
|
+
|
105
|
+
# 判定当前用户是否已经为应用授权
|
106
|
+
#
|
107
|
+
# @option options [String] :uid 用户uid,为空则默认是当前用户
|
108
|
+
# @option options [String] :appid 应用的appid,为空则默认是当前应用
|
109
|
+
#
|
110
|
+
# @return [Boolean]
|
111
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E5.88.A4.E5.AE.9A.E5.BD.93.E5.89.8D.E7.94.A8.E6.88.B7.E6.98.AF.E5.90.A6.E5.B7.B2.E7.BB.8F.E4.B8.BA.E5.BA.94.E7.94.A8.E6.8E.88.E6.9D.83 判定当前用户是否已经为应用授权
|
112
|
+
def app_user?(options={})
|
113
|
+
rest = api_request '/passport/users/isAppUser', options
|
114
|
+
rest[:result] == '1'
|
115
|
+
end
|
116
|
+
|
117
|
+
# 判断指定用户是否具有某个数据操作权限
|
118
|
+
#
|
119
|
+
# 根据用户id以及在百度的相应的操作权限(单个权限,例如接收email等)来判断用户是否可以进行此操作。
|
120
|
+
#
|
121
|
+
# @param [String] ext_perm 单个权限,例如接收email等,具体权限请查看权限列表
|
122
|
+
# @param [String] uid 用户uid,为空则默认是当前用户
|
123
|
+
#
|
124
|
+
# @return [Boolean]
|
125
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E5.88.A4.E6.96.AD.E6.8C.87.E5.AE.9A.E7.94.A8.E6.88.B7.E6.98.AF.E5.90.A6.E5.85.B7.E6.9C.89.E6.9F.90.E4.B8.AA.E6.95.B0.E6.8D.AE.E6.93.8D.E4.BD.9C.E6.9D.83.E9.99.90 判断指定用户是否具有某个数据操作权限
|
126
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth#.E6.8E.88.E6.9D.83.E6.9D.83.E9.99.90.E5.88.97.E8.A1.A8 权限列表
|
127
|
+
def has_app_permission?(ext_perm, uid=nil)
|
128
|
+
body = { ext_perm: ext_perm, uid: uid }
|
129
|
+
rest = api_request '/passport/users/hasAppPermission', body
|
130
|
+
rest[:result] == '1'
|
131
|
+
end
|
132
|
+
|
133
|
+
# 判断指定用户是否具有某一批数据操作权限
|
134
|
+
#
|
135
|
+
# 根据用户id以及在百度的相应的操作权限(可以是多个权限半角逗号隔开)来判断用户是否可以进行此操作
|
136
|
+
#
|
137
|
+
# @example 返回的原始 JSON
|
138
|
+
# {"basic":"1","email":"0"}
|
139
|
+
#
|
140
|
+
# @overload has_app_permissions(ext_perm, uid=nil)
|
141
|
+
# @param [String] ext_perm 多个权限半角逗号隔开,例如basic,email等,具体权限请查看权限列表
|
142
|
+
# @param [String] uid 用户uid,为空则默认是当前用户
|
143
|
+
#
|
144
|
+
# @overload has_app_permissions(ext_perms, uid=nil)
|
145
|
+
# @param [Array<String>] ext_perms 权限字符串数组,具体权限请查看权限列表
|
146
|
+
# @param [String] uid 用户uid,为空则默认是当前用户
|
147
|
+
#
|
148
|
+
# @return [Hash]
|
149
|
+
# 各个值在原始 JSON 基础之上转换为 true 或者 false
|
150
|
+
# 如:{:basic=>true, :email=>false}
|
151
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E5.88.A4.E6.96.AD.E6.8C.87.E5.AE.9A.E7.94.A8.E6.88.B7.E6.98.AF.E5.90.A6.E5.85.B7.E6.9C.89.E6.9F.90.E4.B8.80.E6.89.B9.E6.95.B0.E6.8D.AE.E6.93.8D.E4.BD.9C.E6.9D.83.E9.99.90 判断指定用户是否具有某一批数据操作权限
|
152
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth#.E6.8E.88.E6.9D.83.E6.9D.83.E9.99.90.E5.88.97.E8.A1.A8 权限列表
|
153
|
+
def has_app_permissions(ext_perms, uid=nil)
|
154
|
+
body = { ext_perms: ext_perms, uid: uid }
|
155
|
+
if ext_perms.is_a? Array
|
156
|
+
body[:ext_perms] = ext_perms.join ','
|
157
|
+
end
|
158
|
+
|
159
|
+
rest = api_request '/passport/users/hasAppPermissions', body
|
160
|
+
rest.each { |k, v| rest[k] = v == '1' }
|
161
|
+
end
|
162
|
+
|
163
|
+
# 返回用户好友资料
|
164
|
+
#
|
165
|
+
# 根据用户id以及在百度的相应的操作权限(可以是多个权限半角逗号隔开)来判断用户是否可以进行此操作
|
166
|
+
# @todo TODO: check return value
|
167
|
+
#
|
168
|
+
# @example 返回的原始 JSON
|
169
|
+
# [
|
170
|
+
# {
|
171
|
+
# "uname": "spacebj009",
|
172
|
+
# "uid": "2013411308",
|
173
|
+
# "portrait": "79787370616365626a3030393306"
|
174
|
+
# },
|
175
|
+
# {
|
176
|
+
# "uname": "spacebj003",
|
177
|
+
# "uid": "2013412844",
|
178
|
+
# "portrait": "73787370616365626a3030333306"
|
179
|
+
# }
|
180
|
+
# ]
|
181
|
+
#
|
182
|
+
# :uname 好友的用户名
|
183
|
+
# :uid 当前登录用户的数字ID
|
184
|
+
# :portrait 好友的用户头像加密串
|
185
|
+
#
|
186
|
+
# @option options [Fixnum] :page_no 用于支持分页的API,0表示第1页,默认为0
|
187
|
+
# @option options [Fixnum] :page_size 用于支持分页的API,表示每页返回多少条数据,默认值500
|
188
|
+
# @option options [String] :sort_type 0: 按照添加时间排序,1:登陆时间排序,默认为0
|
189
|
+
#
|
190
|
+
# @return [Array]
|
191
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E8.BF.94.E5.9B.9E.E7.94.A8.E6.88.B7.E5.A5.BD.E5.8F.8B.E8.B5.84.E6.96.99 返回用户好友资料
|
192
|
+
def get_friends(options={})
|
193
|
+
api_request '/friends/getFriends', options
|
194
|
+
end
|
195
|
+
|
196
|
+
# 获得指定用户之间好友关系
|
197
|
+
#
|
198
|
+
# 获得指定用户之间是否是好友关系。第一个数组指定一半用户,第二个数组指定另外一半,
|
199
|
+
# 两个数组必须同样的个数,一次最多可以查20个
|
200
|
+
#
|
201
|
+
# @example 返回的原始 JSON
|
202
|
+
# [
|
203
|
+
# {
|
204
|
+
# "uid1": "2222",
|
205
|
+
# "uid2": "1111",
|
206
|
+
# "are_friends": "1",
|
207
|
+
# "are_friends_reverse": "0"
|
208
|
+
# },
|
209
|
+
# {
|
210
|
+
# "uid1": "3333",
|
211
|
+
# "uid2": "2222",
|
212
|
+
# "are_friends": "0",
|
213
|
+
# "are_friends_reverse": "1"
|
214
|
+
# }
|
215
|
+
# ]
|
216
|
+
#
|
217
|
+
# :are_friends uid2是否是uid1的好友
|
218
|
+
# :are_friends_reverse uid1是否是uid2的好友
|
219
|
+
#
|
220
|
+
# @overload are_friends(uid1, uid2)
|
221
|
+
# @param [String] uid1
|
222
|
+
# @param [String] uid2
|
223
|
+
#
|
224
|
+
# @overload are_friends(uids1, uids2)
|
225
|
+
# @param [Array<String>] uids1
|
226
|
+
# @param [Array<String>] uids2
|
227
|
+
#
|
228
|
+
# @return [Array]
|
229
|
+
# are_friends 和 are_friends_reverse 值在原始 JSON 基础之上转换为 true 或者 false
|
230
|
+
# 如:[{:uid1=>"22", :uid2=>"33", :are_friends=>false, :are_friends_reverse=>true}]
|
231
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E8.8E.B7.E5.BE.97.E6.8C.87.E5.AE.9A.E7.94.A8.E6.88.B7.E4.B9.8B.E9.97.B4.E5.A5.BD.E5.8F.8B.E5.85.B3.E7.B3.BB 获得指定用户之间好友关系
|
232
|
+
def are_friends(uids1, uids2)
|
233
|
+
body = {}
|
234
|
+
case
|
235
|
+
when uids1.is_a?(String) && uids2.is_a?(String)
|
236
|
+
body[:uids1], body[:uids2] = uids1, uids2
|
237
|
+
when uids1.is_a?(Array) && uids2.is_a?(Array)
|
238
|
+
raise ArgumentError, 'not the same size of array' unless uids1.size == uids2.size
|
239
|
+
body[:uids1], body[:uids2] = uids1.join(','), uids2.join(',')
|
240
|
+
else
|
241
|
+
raise ArgumentError, 'not the same types'
|
242
|
+
end
|
243
|
+
|
244
|
+
rest = api_request '/friends/areFriends', body
|
245
|
+
rest.each do |h|
|
246
|
+
h[:are_friends] = h[:are_friends] == '1'
|
247
|
+
h[:are_friends_reverse] = h[:are_friends_reverse] == '1'
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
# 使access_token,session_key过期
|
252
|
+
#
|
253
|
+
# 使用户授予的access_token和session_key过期
|
254
|
+
#
|
255
|
+
# @return [Boolean] true: 成功 false: 失败
|
256
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E4.BD.BFaccess_token.2Csession_key.E8.BF.87.E6.9C.9F 使access_token,session_key过期
|
257
|
+
def expire_session
|
258
|
+
rest = api_request '/passport/auth/expireSession'
|
259
|
+
rest[:result] == '1'
|
260
|
+
end
|
261
|
+
|
262
|
+
# 撤销用户授予第三方应用的权限
|
263
|
+
#
|
264
|
+
# @param [String] uid 用户的ID,空则默认为当前用户
|
265
|
+
#
|
266
|
+
# @return [Boolean] true: 成功 false: 失败
|
267
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E6.92.A4.E9.94.80.E7.94.A8.E6.88.B7.E6.8E.88.E4.BA.88.E7.AC.AC.E4.B8.89.E6.96.B9.E5.BA.94.E7.94.A8.E7.9A.84.E6.9D.83.E9.99.90 撤销用户授予第三方应用的权限
|
268
|
+
def revoke_authorization(uid=nil)
|
269
|
+
rest = api_request('/passport/auth/revokeAuthorization', {uid: uid})
|
270
|
+
rest[:result] == '1'
|
271
|
+
end
|
272
|
+
|
273
|
+
# 查询IP地址所在地区
|
274
|
+
#
|
275
|
+
# @example
|
276
|
+
# rest_client.query_ip '111.222.111.222', '114.114.114.114'
|
277
|
+
#
|
278
|
+
# @example 返回的原始 JSON
|
279
|
+
# {
|
280
|
+
# "111.222.111.222": {
|
281
|
+
# "province": "\u5e7f\u4e1c",
|
282
|
+
# "city": "\u6df1\u5733"
|
283
|
+
# },
|
284
|
+
# "111.222.111.222": {
|
285
|
+
# "province": ""\u6c5f\u82cf",
|
286
|
+
# "city": ""
|
287
|
+
# }
|
288
|
+
# }
|
289
|
+
#
|
290
|
+
# @param [*String] ips 需要查询的ip地址
|
291
|
+
#
|
292
|
+
# @return [Hash]
|
293
|
+
# @see http://developer.baidu.com/wiki/index.php?title=docs/oauth/rest/file_data_apis_list#.E6.9F.A5.E8.AF.A2IP.E5.9C.B0.E5.9D.80.E6.89.80.E5.9C.A8.E5.9C.B0.E5.8C.BA 查询IP地址所在地区
|
294
|
+
def query_ip(*ips)
|
295
|
+
api_request('/iplib/query', { ip: ips.join(',') }, :get)
|
296
|
+
end
|
297
|
+
|
298
|
+
private
|
299
|
+
|
300
|
+
def base_query
|
301
|
+
{ access_token: @access_token }
|
302
|
+
end
|
303
|
+
|
304
|
+
def api_request(path, params={}, method=:post)
|
305
|
+
body = base_query.update params
|
306
|
+
rest =
|
307
|
+
case method
|
308
|
+
when :post
|
309
|
+
post "#{BASE_PATH}#{path}", nil, body
|
310
|
+
when :get
|
311
|
+
get "#{BASE_PATH}#{path}", body
|
312
|
+
end
|
313
|
+
process_error rest
|
314
|
+
rest
|
315
|
+
end
|
316
|
+
|
317
|
+
# NOTE: Baidu api sucks, it may return error info with 200 status
|
318
|
+
# http://developer.baidu.com/wiki/index.php?title=%E7%99%BE%E5%BA%A6Open_API%E9%94%99%E8%AF%AF%E7%A0%81%E5%AE%9A%E4%B9%89
|
319
|
+
def process_error(data)
|
320
|
+
if data.is_a?(Hash) && data.has_key?(:error_code)
|
321
|
+
code = data[:error_code].to_s.strip
|
322
|
+
msg = data[:error_msg]
|
323
|
+
unless code == '0'
|
324
|
+
if %w[102 110 111 112].include? code
|
325
|
+
raise Baidu::Errors::AuthError.new(msg, code)
|
326
|
+
else
|
327
|
+
raise Baidu::Errors::Error.new(msg, code)
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
data/lib/baidu/version.rb
CHANGED
@@ -82,26 +82,24 @@ describe Baidu::OAuth::Client do
|
|
82
82
|
context '#user_and_device_code' do
|
83
83
|
|
84
84
|
it 'requests user and device code' do
|
85
|
-
stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci', response_type: 'device_code')
|
86
|
-
|
85
|
+
stub = stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci', response_type: 'device_code')
|
86
|
+
stub.to_return(status: 200, body: ft('user_and_device_code.json'))
|
87
87
|
@client.device_flow.user_and_device_code
|
88
|
-
|
89
|
-
should have_been_made
|
88
|
+
stub.should have_been_requested
|
90
89
|
end
|
91
90
|
|
92
91
|
it 'requests user and device code with scope' do
|
93
|
-
stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci',
|
94
|
-
|
95
|
-
|
92
|
+
stub = stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci',
|
93
|
+
response_type: 'device_code',
|
94
|
+
scope: 'basic netdisk')
|
95
|
+
stub.to_return(status: 200, body: ft('user_and_device_code.json'))
|
96
96
|
@client.device_flow.user_and_device_code 'basic netdisk'
|
97
|
-
|
98
|
-
response_type: 'device_code', scope: 'basic netdisk').
|
99
|
-
should have_been_made
|
97
|
+
stub.should have_been_requested
|
100
98
|
end
|
101
99
|
|
102
100
|
it 'responses user and device code' do
|
103
|
-
stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci', response_type: 'device_code')
|
104
|
-
|
101
|
+
stub = stub_get(:oauth, '/oauth/2.0/device/code', client_id: 'ci', response_type: 'device_code')
|
102
|
+
stub.to_return(status: 200, body: ft('user_and_device_code.json'))
|
105
103
|
result = @client.device_flow.user_and_device_code
|
106
104
|
expect(result).to be_instance_of(Hash)
|
107
105
|
expect(result).to have_key(:device_code)
|
@@ -114,29 +112,26 @@ describe Baidu::OAuth::Client do
|
|
114
112
|
end
|
115
113
|
|
116
114
|
context '#get_token' do
|
117
|
-
context 'with code flow' do
|
118
|
-
before do
|
119
|
-
stub_post(:oauth, '/oauth/2.0/token',
|
120
|
-
grant_type: 'authorization_code',
|
121
|
-
code: 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn',
|
122
|
-
client_id: 'ci', client_secret: 'cs',
|
123
|
-
redirect_uri: 'http://www.example.com/oauth_redirect').
|
124
|
-
to_return(status: 200, body: ft('get_token_code.json'))
|
125
|
-
end
|
126
|
-
|
115
|
+
context 'with authorization code flow' do
|
127
116
|
it 'requests access tokey' do
|
117
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'authorization_code',
|
118
|
+
client_id: 'ci', client_secret: 'cs',
|
119
|
+
redirect_uri: 'http://www.example.com/oauth_redirect',
|
120
|
+
code: 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn')
|
121
|
+
stub.to_return(status: 200, body: ft('get_token_code.json'))
|
128
122
|
@client.authorization_code_flow.get_token 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn',
|
129
|
-
|
130
|
-
|
131
|
-
grant_type: 'authorization_code',
|
132
|
-
code: 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn',
|
133
|
-
client_id: 'ci', client_secret: 'cs',
|
134
|
-
redirect_uri: 'http://www.example.com/oauth_redirect').should have_been_made
|
123
|
+
'http://www.example.com/oauth_redirect'
|
124
|
+
stub.should have_been_requested
|
135
125
|
end
|
136
126
|
|
137
127
|
it 'responses access token' do
|
128
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'authorization_code',
|
129
|
+
client_id: 'ci', client_secret: 'cs',
|
130
|
+
redirect_uri: 'http://www.example.com/oauth_redirect',
|
131
|
+
code: 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn')
|
132
|
+
stub.to_return(status: 200, body: ft('get_token_code.json'))
|
138
133
|
result = @client.authorization_code_flow.get_token 'ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn',
|
139
|
-
|
134
|
+
'http://www.example.com/oauth_redirect'
|
140
135
|
expect(result).to be_instance_of(Baidu::Session)
|
141
136
|
expect(result).to respond_to(:access_token)
|
142
137
|
expect(result).to respond_to(:refresh_token)
|
@@ -147,20 +142,20 @@ describe Baidu::OAuth::Client do
|
|
147
142
|
end
|
148
143
|
|
149
144
|
context 'with device flow' do
|
150
|
-
before do
|
151
|
-
stub_post(:oauth, '/oauth/2.0/token', grant_type: 'device_token',
|
152
|
-
code: 'a82hjs723h72h3a82hjs723h72h3vb', client_id: 'ci', client_secret: 'cs').
|
153
|
-
to_return(status: 200, body: ft('get_token_device.json'))
|
154
|
-
end
|
155
|
-
|
156
145
|
it 'requests access token' do
|
146
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'device_token',
|
147
|
+
client_id: 'ci', client_secret: 'cs',
|
148
|
+
code: 'a82hjs723h72h3a82hjs723h72h3vb')
|
149
|
+
stub.to_return(status: 200, body: ft('get_token_device.json'))
|
157
150
|
@client.device_flow.get_token 'a82hjs723h72h3a82hjs723h72h3vb'
|
158
|
-
|
159
|
-
code: 'a82hjs723h72h3a82hjs723h72h3vb',
|
160
|
-
client_id: 'ci', client_secret: 'cs').should have_been_made
|
151
|
+
stub.should have_been_requested
|
161
152
|
end
|
162
153
|
|
163
154
|
it 'responses access token' do
|
155
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'device_token',
|
156
|
+
client_id: 'ci', client_secret: 'cs',
|
157
|
+
code: 'a82hjs723h72h3a82hjs723h72h3vb')
|
158
|
+
stub.to_return(status: 200, body: ft('get_token_device.json'))
|
164
159
|
result = @client.device_flow.get_token 'a82hjs723h72h3a82hjs723h72h3vb'
|
165
160
|
expect(result).to be_instance_of(Baidu::Session)
|
166
161
|
expect(result).to respond_to(:access_token)
|
@@ -184,14 +179,16 @@ describe Baidu::OAuth::Client do
|
|
184
179
|
end
|
185
180
|
|
186
181
|
it 'requests access token with scope' do
|
187
|
-
|
182
|
+
params = { grant_type: 'client_credentials', scope: 'basic hao123',
|
183
|
+
client_id: 'ci', client_secret: 'cs' }
|
184
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', params)
|
188
185
|
@client.client_credentials_flow.get_token('basic hao123')
|
189
186
|
stub.should have_been_requested
|
190
187
|
end
|
191
188
|
|
192
189
|
it 'responses access token' do
|
193
|
-
stub = stub_post(:oauth, '/oauth/2.0/token', base_params)
|
194
|
-
|
190
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', base_params)
|
191
|
+
stub.to_return(status: 200, body: ft('get_token_client_credentials.json'))
|
195
192
|
result = @client.client_credentials_flow.get_token
|
196
193
|
expect(result).to be_instance_of(Baidu::Session)
|
197
194
|
expect(result).to respond_to(:access_token)
|
@@ -203,34 +200,31 @@ describe Baidu::OAuth::Client do
|
|
203
200
|
end
|
204
201
|
|
205
202
|
context '#refresh_token' do
|
206
|
-
before do
|
207
|
-
stub_post(:oauth, '/oauth/2.0/token', grant_type: 'refresh_token',
|
208
|
-
refresh_token: '2.af3d55f8615fdfd9edb7c4b5ebdc3e32.604800.1293440400-2346678-124328',
|
209
|
-
client_id: 'ci', client_secret: 'cs').
|
210
|
-
to_return(status: 200, body: ft('refresh_token.json'))
|
211
|
-
end
|
212
|
-
|
213
203
|
it 'requests access token by refresh token' do
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
204
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'refresh_token',
|
205
|
+
refresh_token: '2.af3d55f',
|
206
|
+
client_id: 'ci', client_secret: 'cs')
|
207
|
+
stub.to_return(status: 200, body: ft('refresh_token.json'))
|
208
|
+
@client.refresh('2.af3d55f')
|
209
|
+
stub.should have_been_requested
|
218
210
|
end
|
219
211
|
|
220
|
-
it 'requests access token by
|
221
|
-
stub_post(:oauth, '/oauth/2.0/token', grant_type: 'refresh_token',
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
refresh_token: '2.af3d55f8615fdfd9edb7c4b5ebdc3e32.604800.1293440400-2346678-124328',
|
229
|
-
scope: 'basic netdisk', client_id: 'ci', client_secret: 'cs').should have_been_made
|
212
|
+
it 'requests access token by refresh token with params' do
|
213
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'refresh_token',
|
214
|
+
refresh_token: '2.af3d55f8',
|
215
|
+
client_id: 'ci', client_secret: 'cs',
|
216
|
+
scope: 'basic netdisk')
|
217
|
+
stub.to_return(status: 200, body: ft('refresh_token.json'))
|
218
|
+
@client.refresh('2.af3d55f8', scope: 'basic netdisk')
|
219
|
+
stub.should have_been_requested
|
230
220
|
end
|
231
221
|
|
232
222
|
it 'responses access token by refresh token' do
|
233
|
-
|
223
|
+
stub = stub_post(:oauth, '/oauth/2.0/token', grant_type: 'refresh_token',
|
224
|
+
refresh_token: '2.af3d55f86',
|
225
|
+
client_id: 'ci', client_secret: 'cs')
|
226
|
+
stub.to_return(status: 200, body: ft('refresh_token.json'))
|
227
|
+
result = @client.refresh('2.af3d55f86')
|
234
228
|
expect(result).to be_instance_of(Baidu::Session)
|
235
229
|
expect(result).to respond_to(:access_token)
|
236
230
|
expect(result).to respond_to(:refresh_token)
|
@@ -0,0 +1,313 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'baidu/oauth'
|
5
|
+
|
6
|
+
module Baidu
|
7
|
+
describe OAuth::RESTClient do
|
8
|
+
let(:base_query) { { access_token: '3.xxx.yyy' } }
|
9
|
+
|
10
|
+
before do
|
11
|
+
@client = OAuth::RESTClient.new(base_query[:access_token])
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#initialize' do
|
15
|
+
it 'inits with access token string' do
|
16
|
+
client = OAuth::RESTClient.new('xyz_at')
|
17
|
+
expect(client).to be_a(OAuth::RESTClient)
|
18
|
+
expect(client.instance_variable_get(:@access_token)).to eq('xyz_at')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'inits with Baidu::Session' do
|
22
|
+
session = Baidu::Session.new
|
23
|
+
session.access_token = 'zyx_at'
|
24
|
+
client = OAuth::RESTClient.new(session)
|
25
|
+
expect(client).to be_a(OAuth::RESTClient)
|
26
|
+
expect(client.instance_variable_get(:@access_token)).to eq('zyx_at')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'provides base uri' do
|
30
|
+
client = OAuth::RESTClient.new('xyz_at')
|
31
|
+
expect(client.instance_variable_get(:@site)).to eq('https://openapi.baidu.com')
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises error with other params' do
|
35
|
+
expect {
|
36
|
+
OAuth::RESTClient.new({})
|
37
|
+
}.to raise_error(ArgumentError, 'need a String or Baidu::Session')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#get_logged_in_user' do
|
42
|
+
it 'requests with params' do
|
43
|
+
stub = stub_post(:oauth_rest, '/passport/users/getLoggedInUser', base_query)
|
44
|
+
@client.get_logged_in_user
|
45
|
+
stub.should have_been_requested
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#get_info' do
|
50
|
+
it 'requests current user info' do
|
51
|
+
stub = stub_post(:oauth_rest, '/passport/users/getInfo', base_query)
|
52
|
+
@client.get_info
|
53
|
+
stub.should have_been_requested
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#app_user?' do
|
58
|
+
it 'requests "isAppUser" api' do
|
59
|
+
stub = stub_post(:oauth_rest, '/passport/users/isAppUser', base_query)
|
60
|
+
@client.app_user?
|
61
|
+
stub.should have_been_requested
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'requests "isAppUser" for specified user' do
|
65
|
+
stub = stub_post(:oauth_rest,
|
66
|
+
'/passport/users/isAppUser',
|
67
|
+
base_query.update({ uid: '456123' }))
|
68
|
+
stub.to_return(body: '{"result":"1"}')
|
69
|
+
rest = @client.app_user?(uid: '456123')
|
70
|
+
stub.should have_been_requested
|
71
|
+
expect(rest).to eq(true)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'requests "isAppUser" for specified appid' do
|
75
|
+
stub = stub_post(:oauth_rest,
|
76
|
+
'/passport/users/isAppUser',
|
77
|
+
base_query.update({ appid: '341256' }))
|
78
|
+
stub.to_return(body: '{"result":"0"}')
|
79
|
+
rest = @client.app_user?(appid: '341256')
|
80
|
+
stub.should have_been_requested
|
81
|
+
expect(rest).to eq(false)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#has_app_permission?' do
|
86
|
+
it 'requests "hasAppPermission" api' do
|
87
|
+
stub = stub_post(:oauth_rest,
|
88
|
+
'/passport/users/hasAppPermission',
|
89
|
+
base_query.update({ ext_perm: 'netdisk' }))
|
90
|
+
stub.to_return(body: '{"result":"1"}')
|
91
|
+
rest = @client.has_app_permission? 'netdisk'
|
92
|
+
stub.should have_been_requested
|
93
|
+
expect(rest).to eq(true)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'requests "hasAppPermission" for specified user' do
|
97
|
+
stub = stub_post(:oauth_rest,
|
98
|
+
'/passport/users/hasAppPermission',
|
99
|
+
base_query.update({ ext_perm: 'super_msg', uid: '456123' }))
|
100
|
+
stub.to_return(body: '{"result":"0"}')
|
101
|
+
rest = @client.has_app_permission?('super_msg', '456123')
|
102
|
+
stub.should have_been_requested
|
103
|
+
expect(rest).to eq(false)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#has_app_permissions' do
|
108
|
+
it 'requests "hasAppPermissions" api' do
|
109
|
+
stub = stub_post(:oauth_rest,
|
110
|
+
'/passport/users/hasAppPermissions',
|
111
|
+
base_query.update({ ext_perms: 'netdisk,basic' }))
|
112
|
+
stub.to_return(body: '{"basic":"1", "netdisk":"0"}')
|
113
|
+
rest = @client.has_app_permissions 'netdisk,basic'
|
114
|
+
stub.should have_been_requested
|
115
|
+
expect(rest[:basic]).to eq(true)
|
116
|
+
expect(rest[:netdisk]).to eq(false)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'requests "hasAppPermissions" api with array of perms' do
|
120
|
+
stub = stub_post(:oauth_rest,
|
121
|
+
'/passport/users/hasAppPermissions',
|
122
|
+
base_query.update({ ext_perms: 'netdisk,basic' }))
|
123
|
+
stub.to_return(body: '{"basic":"1", "netdisk":"0"}')
|
124
|
+
rest = @client.has_app_permissions %w[netdisk basic]
|
125
|
+
stub.should have_been_requested
|
126
|
+
expect(rest[:basic]).to eq(true)
|
127
|
+
expect(rest[:netdisk]).to eq(false)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'requests "hasAppPermissions" for specified user' do
|
131
|
+
stub = stub_post(:oauth_rest,
|
132
|
+
'/passport/users/hasAppPermissions',
|
133
|
+
base_query.update({ ext_perms: 'super_msg', uid: '456123' }))
|
134
|
+
stub.to_return(body: '{"super_msg":"0"}')
|
135
|
+
rest = @client.has_app_permissions('super_msg', '456123')
|
136
|
+
stub.should have_been_requested
|
137
|
+
expect(rest[:super_msg]).to eq(false)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe '#get_friends' do
|
142
|
+
it 'requests with default params' do
|
143
|
+
stub = stub_post(:oauth_rest, '/friends/getFriends', base_query)
|
144
|
+
@client.get_friends
|
145
|
+
stub.should have_been_requested
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'requests with custom params' do
|
149
|
+
stub = stub_post(:oauth_rest,
|
150
|
+
'/friends/getFriends',
|
151
|
+
base_query.update({ page_no: 3, page_size: 10, sort_type: 1 }))
|
152
|
+
@client.get_friends page_no: 3, page_size: 10, sort_type: 1
|
153
|
+
stub.should have_been_requested
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'returns result of an array' do
|
157
|
+
stub = stub_post(:oauth_rest, '/friends/getFriends', base_query.update(page_size: 2))
|
158
|
+
stub.to_return(body: ft('get_friends.json'))
|
159
|
+
rest = @client.get_friends page_size: 2
|
160
|
+
stub.should have_been_requested
|
161
|
+
expect(rest).to be_a Array
|
162
|
+
expect(rest.size).to be(2)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe '#are_friends' do
|
167
|
+
it 'requests with both string params' do
|
168
|
+
stub = stub_post(:oauth_rest,
|
169
|
+
'/friends/areFriends',
|
170
|
+
base_query.update(uids1: '111', uids2: '222'))
|
171
|
+
@client.are_friends '111', '222'
|
172
|
+
stub.should have_been_requested
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'requests with both array params' do
|
176
|
+
stub = stub_post(:oauth_rest,
|
177
|
+
'/friends/areFriends',
|
178
|
+
base_query.update(uids1: '111,333', uids2: '222,444'))
|
179
|
+
@client.are_friends %w[111 333], %w[222 444]
|
180
|
+
stub.should have_been_requested
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'requests with different param type' do
|
184
|
+
expect {
|
185
|
+
@client.are_friends '111', %w[222]
|
186
|
+
}.to raise_error ArgumentError, 'not the same types'
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'requests with different size of array params' do
|
190
|
+
expect {
|
191
|
+
@client.are_friends %w[111], %w[222, 333]
|
192
|
+
}.to raise_error ArgumentError, 'not the same size of array'
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'changes result with true or false' do
|
196
|
+
stub = stub_post(:oauth_rest,
|
197
|
+
'/friends/areFriends',
|
198
|
+
base_query.update(uids1: '111,333', uids2: '222,444'))
|
199
|
+
stub.to_return(body: ft('are_friends.json'))
|
200
|
+
rest = @client.are_friends %w[111 333], %w[222 444]
|
201
|
+
stub.should have_been_requested
|
202
|
+
expect(rest.first[:are_friends]).to eq(true)
|
203
|
+
expect(rest.first[:are_friends_reverse]).to eq(false)
|
204
|
+
expect(rest.last[:are_friends]).to eq(false)
|
205
|
+
expect(rest.last[:are_friends_reverse]).to eq(true)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
describe '#expire_session' do
|
210
|
+
it 'requests "expireSession" api successfully' do
|
211
|
+
stub = stub_post(:oauth_rest,
|
212
|
+
'/passport/auth/expireSession',
|
213
|
+
base_query)
|
214
|
+
stub.to_return(body: '{"result":"1"}')
|
215
|
+
rest = @client.expire_session
|
216
|
+
stub.should have_been_requested
|
217
|
+
expect(rest).to eq(true)
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'requests "expireSession" api unsuccessfully' do
|
221
|
+
stub = stub_post(:oauth_rest,
|
222
|
+
'/passport/auth/expireSession',
|
223
|
+
base_query)
|
224
|
+
stub.to_return(body: '{"result":"0"}')
|
225
|
+
rest = @client.expire_session
|
226
|
+
stub.should have_been_requested
|
227
|
+
expect(rest).to eq(false)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe '#revoke_authorization' do
|
232
|
+
it 'requests "revokeAuthorization" api successfully' do
|
233
|
+
stub = stub_post(:oauth_rest,
|
234
|
+
'/passport/auth/revokeAuthorization',
|
235
|
+
base_query)
|
236
|
+
stub.to_return(body: '{"result":"1"}')
|
237
|
+
rest = @client.revoke_authorization
|
238
|
+
stub.should have_been_requested
|
239
|
+
expect(rest).to eq(true)
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'requests "revokeAuthorization" api with uid successfully' do
|
243
|
+
stub = stub_post(:oauth_rest,
|
244
|
+
'/passport/auth/revokeAuthorization',
|
245
|
+
base_query.update({ uid: 123654 }))
|
246
|
+
stub.to_return(body: '{"result":"1"}')
|
247
|
+
rest = @client.revoke_authorization '123654'
|
248
|
+
stub.should have_been_requested
|
249
|
+
expect(rest).to eq(true)
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'requests "revokeAuthorization" api unsuccessfully' do
|
253
|
+
stub = stub_post(:oauth_rest,
|
254
|
+
'/passport/auth/revokeAuthorization',
|
255
|
+
base_query)
|
256
|
+
stub.to_return(body: '{"result":"0"}')
|
257
|
+
rest = @client.revoke_authorization
|
258
|
+
stub.should have_been_requested
|
259
|
+
expect(rest).to eq(false)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe '#query_ip' do
|
264
|
+
it 'requests single ip' do
|
265
|
+
stub = stub_get(:oauth_rest,
|
266
|
+
'/iplib/query',
|
267
|
+
base_query.update({ ip: '111.222.111.222' }))
|
268
|
+
stub.to_return(body: '{"111.222.111.222":{"province":"\u5e7f\u4e1c","city":"\u6df1\u5733"}}')
|
269
|
+
rest = @client.query_ip '111.222.111.222'
|
270
|
+
stub.should have_been_requested
|
271
|
+
expect(rest).to have_key(:'111.222.111.222')
|
272
|
+
end
|
273
|
+
|
274
|
+
it 'requests multiple ips' do
|
275
|
+
stub = stub_get(:oauth_rest,
|
276
|
+
'/iplib/query',
|
277
|
+
base_query.update({ ip: '111.222.111.222,8.8.8.8' }))
|
278
|
+
@client.query_ip '111.222.111.222', '8.8.8.8'
|
279
|
+
stub.should have_been_requested
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'requests multiple ips as array' do
|
283
|
+
stub = stub_get(:oauth_rest,
|
284
|
+
'/iplib/query',
|
285
|
+
base_query.update({ ip: '111.222.111.222,8.8.8.8,8.8.4.4' }))
|
286
|
+
@client.query_ip ['111.222.111.222', '8.8.8.8', '8.8.4.4']
|
287
|
+
stub.should have_been_requested
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe 'when process error' do
|
292
|
+
it 'does not raise error' do
|
293
|
+
expect {
|
294
|
+
@client.send(:process_error, ['hi'])
|
295
|
+
@client.send(:process_error, {num: 0})
|
296
|
+
@client.send(:process_error, {error_code: 0})
|
297
|
+
}.not_to raise_error
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'raises Baidu::Errors::Error' do
|
301
|
+
expect {
|
302
|
+
@client.send(:process_error, {error_code: 1})
|
303
|
+
}.to raise_error Baidu::Errors::Error
|
304
|
+
end
|
305
|
+
|
306
|
+
it 'raises Baidu::Errors::AuthError' do
|
307
|
+
expect {
|
308
|
+
@client.send(:process_error, {error_code: 110})
|
309
|
+
}.to raise_error Baidu::Errors::AuthError
|
310
|
+
end
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -35,6 +35,7 @@ end
|
|
35
35
|
def webmock_url(mod, path)
|
36
36
|
case mod
|
37
37
|
when :oauth then "https://openapi.baidu.com#{path}"
|
38
|
+
when :oauth_rest then "https://openapi.baidu.com/rest/2.0#{path}"
|
38
39
|
when :pcs then "https://pcs.baidu.com/rest/2.0/pcs#{path}"
|
39
40
|
when :pcs_upload then "https://c.pcs.baidu.com/rest/2.0/pcs#{path}"
|
40
41
|
when :pcs_download then "https://d.pcs.baidu.com/rest/2.0/pcs#{path}"
|
@@ -43,24 +44,35 @@ end
|
|
43
44
|
|
44
45
|
def stub_get(mod, path, params={})
|
45
46
|
req = stub_request :get, webmock_url(mod, path)
|
46
|
-
|
47
|
+
set_req_params :query, req, params
|
47
48
|
req
|
48
49
|
end
|
49
50
|
|
50
51
|
def stub_post(mod, path, params={})
|
51
52
|
req = stub_request :post, webmock_url(mod, path)
|
52
|
-
|
53
|
+
set_req_params :body, req, params
|
53
54
|
req
|
54
55
|
end
|
55
56
|
|
56
57
|
def a_get(mod, path, params={})
|
57
58
|
req = a_request :get, webmock_url(mod, path)
|
58
|
-
|
59
|
+
set_req_params :query, req, params
|
59
60
|
req
|
60
61
|
end
|
61
62
|
|
62
63
|
def a_post(mod, path, params={})
|
63
64
|
req = a_request :post, webmock_url(mod, path)
|
64
|
-
|
65
|
+
set_req_params :body, req, params
|
65
66
|
req
|
66
67
|
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def set_req_params(type, req, params)
|
72
|
+
unless params.empty?
|
73
|
+
if params.is_a?(Hash)
|
74
|
+
params = URI.encode_www_form(params)
|
75
|
+
end
|
76
|
+
req.with(Hash[type, params])
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baidu-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lonre Wang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multipart-post
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- lib/baidu/oauth/flow/client_credentials.rb
|
67
67
|
- lib/baidu/oauth/flow/device.rb
|
68
68
|
- lib/baidu/oauth/flow/implicit_grant.rb
|
69
|
+
- lib/baidu/oauth/rest_client.rb
|
69
70
|
- lib/baidu/pcs.rb
|
70
71
|
- lib/baidu/pcs/client.rb
|
71
72
|
- lib/baidu/session.rb
|
@@ -75,16 +76,19 @@ files:
|
|
75
76
|
- lib/baidu/version.rb
|
76
77
|
- spec/baidu/core_spec.rb
|
77
78
|
- spec/baidu/oauth/client_spec.rb
|
79
|
+
- spec/baidu/oauth/rest_client_spec.rb
|
78
80
|
- spec/baidu/pcs/client_spec.rb
|
79
81
|
- spec/baidu/session_spec.rb
|
80
82
|
- spec/baidu/support/request_spec.rb
|
81
83
|
- spec/baidu/support/util_spec.rb
|
82
84
|
- spec/fixtures/add_task.json
|
85
|
+
- spec/fixtures/are_friends.json
|
83
86
|
- spec/fixtures/cancel_task.json
|
84
87
|
- spec/fixtures/copy.json
|
85
88
|
- spec/fixtures/delete.json
|
86
89
|
- spec/fixtures/diff.json
|
87
90
|
- spec/fixtures/empty.json
|
91
|
+
- spec/fixtures/get_friends.json
|
88
92
|
- spec/fixtures/get_token_client_credentials.json
|
89
93
|
- spec/fixtures/get_token_code.json
|
90
94
|
- spec/fixtures/get_token_device.json
|
@@ -130,23 +134,26 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
134
|
version: '0'
|
131
135
|
requirements: []
|
132
136
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.
|
137
|
+
rubygems_version: 2.2.0
|
134
138
|
signing_key:
|
135
139
|
specification_version: 4
|
136
140
|
summary: Unofficial Baidu REST API SDK for Ruby.
|
137
141
|
test_files:
|
138
142
|
- spec/baidu/core_spec.rb
|
139
143
|
- spec/baidu/oauth/client_spec.rb
|
144
|
+
- spec/baidu/oauth/rest_client_spec.rb
|
140
145
|
- spec/baidu/pcs/client_spec.rb
|
141
146
|
- spec/baidu/session_spec.rb
|
142
147
|
- spec/baidu/support/request_spec.rb
|
143
148
|
- spec/baidu/support/util_spec.rb
|
144
149
|
- spec/fixtures/add_task.json
|
150
|
+
- spec/fixtures/are_friends.json
|
145
151
|
- spec/fixtures/cancel_task.json
|
146
152
|
- spec/fixtures/copy.json
|
147
153
|
- spec/fixtures/delete.json
|
148
154
|
- spec/fixtures/diff.json
|
149
155
|
- spec/fixtures/empty.json
|
156
|
+
- spec/fixtures/get_friends.json
|
150
157
|
- spec/fixtures/get_token_client_credentials.json
|
151
158
|
- spec/fixtures/get_token_code.json
|
152
159
|
- spec/fixtures/get_token_device.json
|