base-api-client 0.1.beta → 0.2.1.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39d75f174be753de6e385803877029bdcb18e082
4
- data.tar.gz: 5d70306b9d7689c2fd63107fd3254e802f159805
3
+ metadata.gz: 103ef6001cec3b96f9f486c5189972ea14b66eb0
4
+ data.tar.gz: 6d4b73796e59847cc1e697f7e9e7ddb842b3a195
5
5
  SHA512:
6
- metadata.gz: 46b44a2014cafdfa7918095e1ff0d1510ff5e34dff51a7d258ee769018505e70d49c0dcdd07fbdf41fac3625d0b470a5d6b63b1f7679ceba4b7c5812c284ea19
7
- data.tar.gz: a4f317556b86c5ea7eebef236a1c6e6e03fec83b97b4607cf25871767695eee645224e9f1bdbaa119942ec18e9e24e2a208c3be04adeab08c67628f60407d146
6
+ metadata.gz: e07780ea63e9722d309c679e612b53941d8de8e0f16fe2c3ff7670f889ab29baa0a5ede2a0dad1a326102753280c9f535de1296831d9e1cf58bcc5310672b2e1
7
+ data.tar.gz: 66039dc96c386efced4a658df4cbd84b7e8c496c889366c6c235d74548a05a1a32af56e922d27c1ff071a4736261ee3426f9c05c72361f1ed344f8e5960b43f6
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'base-api-client'
3
- spec.version = '0.1.beta'
3
+ spec.version = '0.2.1.beta'
4
4
  spec.author = 'ysksn'
5
5
  spec.email = ['bluewhale1982@gmail.com']
6
6
  spec.summary = 'Client for accessing BASE APIs'
@@ -1,10 +1,10 @@
1
1
  {
2
- "client_id": "",
3
- "client_secret": "",
2
+ "client_id": "e0920d3fba627a5da4eaab4e8e9d43fc",
3
+ "client_secret": "ad493e7ada7eb96a6f71d1ccb100966b",
4
4
  "code": "",
5
- "redirect_uri": "",
5
+ "redirect_uri": "http://localhost.local",
6
6
  "search": {
7
- "client_id": "",
8
- "client_secret": ""
7
+ "client_id": "623f75000821c16163ec570ca6469539",
8
+ "client_secret": "214b4134022df8bf0230f5a077c772d9"
9
9
  }
10
10
  }
@@ -11,6 +11,8 @@ module Base
11
11
  :client_id,
12
12
  :client_secret,
13
13
  :code,
14
+ :expires_at,
15
+ :grant_type,
14
16
  :redirect_uri,
15
17
  :refresh_token,
16
18
  :search_client_id,
@@ -25,7 +27,6 @@ module Base
25
27
  @redirect_uri ||= app_info['redirect_uri']
26
28
  @search_client_id ||= app_info['search']['client_id']
27
29
  @search_client_secret ||= app_info['search']['client_secret']
28
- set_tokens!
29
30
  end
30
31
 
31
32
  def update!
@@ -69,6 +70,19 @@ module Base
69
70
  use_ssl: uri.scheme == 'https') { |http| http.request(request) }
70
71
  end
71
72
 
73
+ def generate_code
74
+ system(command, [AUTH_URI, generate_authorize_parameters].join)
75
+ end
76
+
77
+ def set_tokens!
78
+ response = fetch_token
79
+ @access_token = response['access_token']
80
+ @refresh_token = response['refresh_token']
81
+ @code = nil
82
+ @expires_at = to_expires_at(response['expires_in'])
83
+ to_hash
84
+ end
85
+
72
86
  private
73
87
 
74
88
  def load_file
@@ -80,7 +94,9 @@ module Base
80
94
  { client_id: client_id,
81
95
  client_secret: client_secret,
82
96
  code: code,
97
+ expires_at: expires_at,
83
98
  redirect_uri: redirect_uri,
99
+ access_token: access_token,
84
100
  refresh_token: refresh_token,
85
101
  search_client_id: search_client_id,
86
102
  search_client_secret: search_client_secret }
@@ -98,13 +114,9 @@ module Base
98
114
  ['state', ''],
99
115
  %w(response_type code),
100
116
  ['scope', SCOPES]]
101
- .inject([]) do |memo, arr|
102
- memo << arr.join('=')
103
- end.join('&')
104
- end
105
-
106
- def generate_code
107
- system(command, [AUTH_URI, generate_authorize_parameters].join)
117
+ .inject([]) do |memo, arr|
118
+ memo << arr.join('=')
119
+ end.join('&')
108
120
  end
109
121
 
110
122
  def command
@@ -115,8 +127,16 @@ module Base
115
127
  end
116
128
  end
117
129
 
118
- def fetch_token(refresh: false)
119
- grant_type = refresh ? 'refresh_token' : 'authorization_code'
130
+ def grant_type
131
+ @grant_type =
132
+ if code && redirect_uri
133
+ 'authorization_code'
134
+ elsif refresh_token
135
+ 'refresh_token'
136
+ end
137
+ end
138
+
139
+ def fetch_token
120
140
  request_parameters = to_hash.merge(grant_type: grant_type)
121
141
  uri = URI.parse(TOKEN_URI.to_s)
122
142
  response = Net::HTTP.post_form(uri, request_parameters)
@@ -124,10 +144,8 @@ module Base
124
144
  JSON.parse response.body
125
145
  end
126
146
 
127
- def set_tokens!(refresh: false)
128
- response = refresh ? fetch_token(refresh: refresh) : fetch_token
129
- @access_token = response['access_token']
130
- @refresh_token = response['refresh_token']
147
+ def to_expires_at(expires_in)
148
+ Time.now + expires_in
131
149
  end
132
150
  end
133
151
  end
@@ -6,10 +6,12 @@ RSpec.describe Base::APIClient::ClientSecret do
6
6
  let(:data) { MultiJson.load(File.open(secret_file, 'r').read) }
7
7
 
8
8
  before(:each) do
9
+ allow(Time).to receive(:now) { Time.new(2016) }
9
10
  allow_any_instance_of(Base::APIClient::ClientSecret)
10
11
  .to receive(:fetch_token) do
11
12
  { access_token: 'fake_access_token',
12
- refresh_token: 'fake_refresh_token' }
13
+ refresh_token: 'fake_refresh_token',
14
+ expires_in: 3600 }
13
15
  end
14
16
  end
15
17
 
@@ -35,6 +37,10 @@ RSpec.describe Base::APIClient::ClientSecret do
35
37
  expect(subject).to respond_to(:code)
36
38
  end
37
39
 
40
+ it 'has @expires_at' do
41
+ expect(subject).to respond_to(:expires_at)
42
+ end
43
+
38
44
  it 'has @redirect_uri' do
39
45
  expect(subject).to respond_to(:redirect_uri)
40
46
  end
@@ -119,8 +125,10 @@ RSpec.describe Base::APIClient::ClientSecret do
119
125
  client_id: 'fake_client_id',
120
126
  client_secret: 'fake_client_secret',
121
127
  code: 'fake_code',
128
+ expires_at: nil,
122
129
  redirect_uri: 'http://fake_redirect_uri.com',
123
- refresh_token: nil,
130
+ access_token: nil,
131
+ refresh_token: nil,
124
132
  search_client_id: 'fake_search_client_id',
125
133
  search_client_secret: 'fake_search_client_secret')
126
134
  end
@@ -129,7 +137,7 @@ RSpec.describe Base::APIClient::ClientSecret do
129
137
  describe '#to_json' do
130
138
  it 'returns values of instance variables in json' do
131
139
  expect(subject.send(:to_json)).to eq \
132
- <<"EOS""{\"client_id\":\"fake_client_id\",\"client_secret\":\"fake_client_secret\",\"code\":\"fake_code\",\"redirect_uri\":\"http://fake_redirect_uri.com\",\"refresh_token\":null,\"search_client_id\":\"fake_search_client_id\",\"search_client_secret\":\"fake_search_client_secret\"}"
140
+ <<"EOS""{\"client_id\":\"fake_client_id\",\"client_secret\":\"fake_client_secret\",\"code\":\"fake_code\",\"expires_at\":null,\"redirect_uri\":\"http://fake_redirect_uri.com\",\"access_token\":null,\"refresh_token\":null,\"search_client_id\":\"fake_search_client_id\",\"search_client_secret\":\"fake_search_client_secret\"}"
133
141
  EOS
134
142
  end
135
143
  end
@@ -199,10 +207,42 @@ EOS
199
207
  end
200
208
  end
201
209
 
202
- describe '#fetch_token' do
203
- context 'passed arg "refresh: false"' do
204
- let(:arg) { { refresh: false } }
210
+ describe '#grant_type' do
211
+ context '@grant_type is not nil' do
212
+ it 'returns @grant_type value authorization_code' do
213
+ subject.instance_variable_set(:@grant_type, 'authorization_code')
214
+ subject.send(:grant_type)
215
+ expect(subject.instance_variable_get(:@grant_type)).to eq \
216
+ 'authorization_code'
217
+ end
218
+ end
205
219
 
220
+ context '@code and @redirect_uri exist' do
221
+ it 'sets @grant_type "authorization_code"' do
222
+ subject.instance_variable_set(:@grant_type, nil)
223
+ subject.instance_variable_set(:@code, true)
224
+ subject.instance_variable_set(:@redirect_uri, true)
225
+ subject.send(:grant_type)
226
+ expect(subject.instance_variable_get(:@grant_type)).to eq \
227
+ 'authorization_code'
228
+ end
229
+ end
230
+
231
+ context 'only @refresh_token exists' do
232
+ it 'sets @grant_type "authorization_code"' do
233
+ subject.instance_variable_set(:@grant_type, nil)
234
+ subject.instance_variable_set(:@code, nil)
235
+ subject.instance_variable_set(:@redirect_uri, nil)
236
+ subject.instance_variable_set(:@refresh_token, true)
237
+ subject.send(:grant_type)
238
+ expect(subject.instance_variable_get(:@grant_type)).to eq \
239
+ 'refresh_token'
240
+ end
241
+ end
242
+ end
243
+
244
+ describe '#fetch_token' do
245
+ context 'grant_type is "authorization_code"' do
206
246
  it 'returns access and refresh tokens' do
207
247
  stub_request(
208
248
  :post,
@@ -212,15 +252,14 @@ EOS
212
252
  .send(:to_hash)
213
253
  .merge(grant_type: 'authorization_code'))
214
254
 
215
- expect(subject.send(:fetch_token, arg)).to eq(
255
+ expect(subject.send(:fetch_token)).to eq(
216
256
  access_token: 'fake_access_token',
217
- refresh_token: 'fake_refresh_token')
257
+ refresh_token: 'fake_refresh_token',
258
+ expires_in: 3600)
218
259
  end
219
260
  end
220
261
 
221
- context 'passed arg "refresh: true"' do
222
- let(:arg) { { refresh: true } }
223
-
262
+ context 'grant_type is "refresh_token"' do
224
263
  it 'returns access and refresh tokens' do
225
264
  stub_request(
226
265
  :post,
@@ -230,41 +269,90 @@ EOS
230
269
  .send(:to_hash)
231
270
  .merge(grant_type: 'refresh_token'))
232
271
 
233
- expect(subject.send(:fetch_token, arg)).to eq(
272
+ expect(subject.send(:fetch_token)).to eq(
234
273
  access_token: 'fake_access_token',
235
- refresh_token: 'fake_refresh_token')
274
+ refresh_token: 'fake_refresh_token',
275
+ expires_in: 3600)
236
276
  end
237
277
  end
238
278
  end
239
279
 
240
280
  describe '#set_tokens!' do
241
- context 'passed arg "refresh: false"' do
242
- let(:arg) { { refresh: false } }
281
+ let(:expected_expires_at) do
282
+ Date.parse(Time.new(2016).to_s).to_time + 3600
283
+ end
284
+
285
+ before do
286
+ allow(subject).to receive(:fetch_token) do
287
+ { 'access_token' => 'stubbed_access_token',
288
+ 'refresh_token' => 'stubbed_refresh_token',
289
+ 'expires_in' => 3600 }
290
+ end
291
+ end
292
+
293
+ context 'fetch_token called with grant_type "authorization_code"' do
294
+ context 'grant_type is authorization_code' do
295
+ before do
296
+ allow(subject).to receive(:grant_type) do
297
+ 'authorization_code'
298
+ end
299
+ end
243
300
 
244
- it 'set access and refresh tokens' do
245
- allow(subject).to receive(:fetch_token) do
246
- { 'access_token' => 'stubbed_access_token',
247
- 'refresh_token' => 'stubbed_refresh_token' }
301
+ it 'set access and refresh tokens' do
302
+ subject.send(:set_tokens!)
303
+ expect(subject.access_token).to eq 'stubbed_access_token'
304
+ expect(subject.refresh_token).to eq 'stubbed_refresh_token'
305
+ expect(subject.expires_at).to eq expected_expires_at
248
306
  end
249
307
 
250
- subject.send(:set_tokens!, arg)
251
- expect(subject.access_token).to eq 'stubbed_access_token'
252
- expect(subject.refresh_token).to eq 'stubbed_refresh_token'
308
+ it 'returns hased properties' do
309
+ expect(subject.send(:set_tokens!)).to eq(
310
+ access_token: 'stubbed_access_token',
311
+ client_id: 'fake_client_id',
312
+ client_secret: 'fake_client_secret',
313
+ code: nil,
314
+ expires_at: expected_expires_at,
315
+ redirect_uri: 'http://fake_redirect_uri.com',
316
+ refresh_token: 'stubbed_refresh_token',
317
+ search_client_id: 'fake_search_client_id',
318
+ search_client_secret: 'fake_search_client_secret')
319
+ end
253
320
  end
254
321
  end
255
322
 
256
- context 'passed arg "refresh: true"' do
257
- let(:arg) { { refresh: true } }
323
+ context 'fetch_token called with grant_type "refresh_token"' do
324
+ context 'grant_type is refresh_token' do
325
+ before do
326
+ allow(subject).to receive(:grant_type) { 'refresh_token' }
327
+ end
328
+
329
+ it 'set access and refresh tokens' do
330
+ subject.send(:set_tokens!)
331
+ expect(subject.access_token).to eq 'stubbed_access_token'
332
+ expect(subject.refresh_token).to eq 'stubbed_refresh_token'
333
+ expect(subject.expires_at).to eq expected_expires_at
334
+ end
258
335
 
259
- it 'set access and refresh tokens' do
260
- allow(subject).to receive(:fetch_token).with(arg) do
261
- { 'access_token' => 'stubbed_access_token',
262
- 'refresh_token' => 'stubbed_refresh_token' }
336
+ it 'returns hased properties' do
337
+ expect(subject.send(:set_tokens!)).to eq(
338
+ access_token: 'stubbed_access_token',
339
+ client_id: 'fake_client_id',
340
+ client_secret: 'fake_client_secret',
341
+ code: nil,
342
+ expires_at: expected_expires_at,
343
+ redirect_uri: 'http://fake_redirect_uri.com',
344
+ refresh_token: 'stubbed_refresh_token',
345
+ search_client_id: 'fake_search_client_id',
346
+ search_client_secret: 'fake_search_client_secret')
263
347
  end
348
+ end
349
+ end
264
350
 
265
- subject.send(:set_tokens!, arg)
266
- expect(subject.access_token).to eq 'stubbed_access_token'
267
- expect(subject.refresh_token).to eq 'stubbed_refresh_token'
351
+ describe '#to_expires_at' do
352
+ it 'returns expires_at' do
353
+ expires_at = subject.send(:to_expires_at, 3600)
354
+ expect(expires_at).to eq \
355
+ Date.parse(Time.new(2016).to_s).to_time + 3600
268
356
  end
269
357
  end
270
358
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.beta
4
+ version: 0.2.1.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - ysksn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-14 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: os