base-api-client 0.2.3.beta → 0.2.4.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: ffaf6e1e894e5abe3cce6ad140c14e911cdc7758
4
- data.tar.gz: a8d9955e61c1621a6f9acd6c57154dcd15e9c3ce
3
+ metadata.gz: 2049e5a041b193725cedb8ba838866bb0f5b893c
4
+ data.tar.gz: 4bc4c3c911f59ca70117c8c6c0a784b5996e7b6e
5
5
  SHA512:
6
- metadata.gz: d0b03d2976a86f5558edf0fa32ec03b38472e9cab582db58dc2ecd4d3ce167bd04c25d885c427e59f7d28d053b17c572f9ef77c795e049492551d8d6a44f8eb5
7
- data.tar.gz: 7563f4afe8ad6929f2e089a888bb36f4c55bde0a47d5cd2b05a5a283ee61c044f5bfcdab5b9c43b036250209ddf7b52a5b211114b70c39d386adaca1f1a3a030
6
+ metadata.gz: d26d8081069b011a9429e7bce75c842f0fe5670cbe1f0a15204c45263eef680795e554b1f80f6971103c192a74e9e0a8fee8e22f5a54dc4f76363ace617d4564
7
+ data.tar.gz: fdf932804c2514f6d4e112902dd4e7c0cc62d44d8711b358f6858c489edc4e56e4bcd6fbb83f52ecf0a904e2a0a4eaf3eddf56d34918a2d7d728464c2d846acd
data/Gemfile CHANGED
@@ -3,7 +3,6 @@ source 'https://rubygems.org'
3
3
  gem 'bundler', '~> 1.11.2'
4
4
  gem 'guard-rspec'
5
5
  gem 'json_spec', '~> 1.1.4'
6
- gem 'os', '~> 0.9.6'
7
6
  gem 'pry-byebug'
8
7
  gem 'pry-doc'
9
8
  gem 'rake', '~> 10.4.2'
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'base-api-client'
3
- spec.version = '0.2.3.beta'
3
+ spec.version = '0.2.4.beta'
4
4
  spec.author = 'ysksn'
5
5
  spec.email = ['bluewhale1982@gmail.com']
6
6
  spec.summary = 'Client for accessing BASE APIs'
@@ -13,6 +13,4 @@ Gem::Specification.new do |spec|
13
13
  spec.bindir = 'bin'
14
14
  spec.post_install_message = 'base-api-client gem installed :)'
15
15
  spec.required_ruby_version = ['> 2.2.2']
16
-
17
- spec.add_runtime_dependency 'os', '~> 0.9.6'
18
16
  end
@@ -1,8 +1,8 @@
1
1
  {
2
- "client_id": "e0920d3fba627a5da4eaab4e8e9d43fc",
3
- "client_secret": "ad493e7ada7eb96a6f71d1ccb100966b",
2
+ "client_id": "",
3
+ "client_secret": "",
4
4
  "code": "",
5
- "redirect_uri": "http://localhost.local",
6
- "search_client_id": "623f75000821c16163ec570ca6469539",
7
- "search_client_secret": "214b4134022df8bf0230f5a077c772d9"
5
+ "redirect_uri": "",
6
+ "search_client_id": "",
7
+ "search_client_secret": ""
8
8
  }
@@ -23,21 +23,25 @@ module Base
23
23
  @client_id ||= opts['client_id']
24
24
  @client_secret ||= opts['client_secret']
25
25
  @code ||= opts['code']
26
+ @expires_at ||= opts['expires_at']
26
27
  @redirect_uri ||= opts['redirect_uri']
28
+ @access_token ||= opts['access_token']
29
+ @refresh_token ||= opts['refresh_token']
27
30
  @search_client_id ||= opts['search_client_id']
28
31
  @search_client_secret ||= opts['search_client_secret']
29
32
  end
30
33
 
31
34
  def update!(args)
32
- self.client_id = args[:client_id] if args[:client_id]
33
- self.client_secret = args[:client_secret] if args[:client_secret]
34
- self.code = args[:code] if args[:code]
35
- self.expires_at = args[:expires_at] if args[:expires_at]
36
- self.redirect_uri = args[:redirect_uri] if args[:redirect_uri]
37
- self.access_token = args[:access_token] if args[:access_token]
38
- self.refresh_token = args[:refresh_token] if args[:refresh_token]
39
- self.search_client_id = args[:search_client_id] if args[:search_client_id]
40
- self.search_client_secret = args[:search_client_secret] if args[:search_client_secret]
35
+ opts = Base::Normalization.recursive_hash_normalize_keys(args)
36
+ self.client_id = opts[:client_id] if opts[:client_id]
37
+ self.client_secret = opts[:client_secret] if opts[:client_secret]
38
+ self.code = opts[:code] if opts[:code]
39
+ self.expires_at = opts[:expires_at] if opts[:expires_at]
40
+ self.redirect_uri = opts[:redirect_uri] if opts[:redirect_uri]
41
+ self.access_token = opts[:access_token] if opts[:access_token]
42
+ self.refresh_token = opts[:refresh_token] if opts[:refresh_token]
43
+ self.search_client_id = opts[:search_client_id] if opts[:search_client_id]
44
+ self.search_client_secret = opts[:search_client_secret] if opts[:search_client_secret]
41
45
  end
42
46
 
43
47
  def to_json
@@ -81,8 +85,8 @@ module Base
81
85
  use_ssl: uri.scheme == 'https') { |http| http.request(request) }
82
86
  end
83
87
 
84
- def generate_code
85
- system(command, [AUTH_URI, generate_authorize_parameters].join)
88
+ def generate_code_uri
89
+ [AUTH_URI, generate_authorize_parameters].join
86
90
  end
87
91
 
88
92
  def set_tokens!
@@ -90,7 +94,7 @@ module Base
90
94
  @access_token = response['access_token']
91
95
  @refresh_token = response['refresh_token']
92
96
  @code = nil
93
- @expires_at = to_expires_at(response['expires_in'])
97
+ send :expires_at=
94
98
  to_hash
95
99
  end
96
100
 
@@ -126,14 +130,6 @@ module Base
126
130
  end.join('&')
127
131
  end
128
132
 
129
- def command
130
- if OS.linux?
131
- 'sensible-browser'
132
- elsif OS.mac?
133
- 'open'
134
- end
135
- end
136
-
137
133
  def grant_type
138
134
  @grant_type =
139
135
  if code && redirect_uri
@@ -151,8 +147,8 @@ module Base
151
147
  JSON.parse response.body
152
148
  end
153
149
 
154
- def to_expires_at(expires_in)
155
- Time.now + expires_in
150
+ def expires_at=(expires_at = nil)
151
+ @expires_at = expires_at || Time.now + 3600
156
152
  end
157
153
  end
158
154
  end
@@ -0,0 +1,19 @@
1
+ module Base
2
+ class Normalization
3
+ class << self
4
+ def recursive_hash_normalize_keys(val)
5
+ (val.is_a? Hash) ? deep_hash_normalize(val) : val
6
+ end
7
+
8
+ def deep_hash_normalize(old_hash)
9
+ sym_hash = {}
10
+
11
+ old_hash.each do |k, v|
12
+ sym_hash[k.to_sym] = recursive_hash_normalize_keys(v)
13
+ end
14
+
15
+ sym_hash
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/base/apis.rb CHANGED
@@ -2,7 +2,6 @@ require 'net/http'
2
2
  require 'open-uri'
3
3
  require 'pathname'
4
4
  require 'multi_json'
5
- require 'os'
6
5
 
7
6
  module Base
8
7
  module Apis
@@ -61,7 +61,7 @@ RSpec.describe Base::APIClient::ClientSecret do
61
61
  expect(subject.client_secret).to eq \
62
62
  'fake_client_secret'
63
63
  expect(subject.code).to eq \
64
- 'fake_code'
64
+ nil
65
65
  expect(subject.redirect_uri).to eq \
66
66
  'http://fake_redirect_uri.com'
67
67
  expect(subject.search_client_id).to eq \
@@ -131,7 +131,7 @@ RSpec.describe Base::APIClient::ClientSecret do
131
131
  expect(subject.send(:to_hash)).to eq(
132
132
  client_id: 'fake_client_id',
133
133
  client_secret: 'fake_client_secret',
134
- code: 'fake_code',
134
+ code: nil,
135
135
  expires_at: nil,
136
136
  redirect_uri: 'http://fake_redirect_uri.com',
137
137
  access_token: nil,
@@ -144,7 +144,7 @@ RSpec.describe Base::APIClient::ClientSecret do
144
144
  describe '#to_json' do
145
145
  it 'returns values of instance variables in json' do
146
146
  expect(subject.send(:to_json)).to eq \
147
- <<"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\"}"
147
+ <<"EOS""{\"client_id\":\"fake_client_id\",\"client_secret\":\"fake_client_secret\",\"code\":null,\"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\"}"
148
148
  EOS
149
149
  end
150
150
  end
@@ -157,67 +157,18 @@ EOS
157
157
  end
158
158
  end
159
159
 
160
- describe '#command' do
161
- context 'OS is Linux' do
162
- it 'returns "sensible-browser"' do
163
- allow(OS).to receive(:linux?) { true }
164
- expect(subject.send(:command)).to eq 'sensible-browser'
165
- end
166
- end
167
-
168
- context 'OS is OSX' do
169
- it 'returns "open"' do
170
- allow(OS).to receive(:linux?) { false }
171
- allow(OS).to receive(:mac?) { true }
172
- expect(subject.send(:command)).to eq 'open'
173
- end
174
- end
175
- end
176
-
177
- describe '#generate_code' do
178
- context 'OS is Linux' do
179
- before(:each) do
180
- allow(subject).to receive(:command) { 'sensible-browser' }
181
- end
182
-
183
- it 'call sensible-browser' do
184
- expect(subject).to receive(:system).with(
185
- 'sensible-browser',
186
- 'https://api.thebase.in/1/oauth/authorize?client_id=fake_client_id&redirect_uri=http://fake_redirect_uri.com&state=&response_type=code&scope=read_items read_orders read_savings read_users read_users_mail write_items write_orders')
187
-
188
- subject.send(:generate_code)
189
- end
190
-
191
- it 'returns ture' do
192
- allow(subject).to receive(:generate_code) { true }
193
- expect(subject.send(:generate_code)).to eq true
194
- end
195
- end
196
-
197
- context 'OS is OSX' do
198
- before(:each) do
199
- allow(subject).to receive(:command) { 'open' }
200
- end
201
-
202
- it 'call open' do
203
- expect(subject).to receive(:system).with(
204
- 'open',
205
- 'https://api.thebase.in/1/oauth/authorize?client_id=fake_client_id&redirect_uri=http://fake_redirect_uri.com&state=&response_type=code&scope=read_items read_orders read_savings read_users read_users_mail write_items write_orders')
206
-
207
- subject.send(:generate_code)
208
- end
209
-
210
- it 'returns ture' do
211
- allow(subject).to receive(:generate_code) { true }
212
- expect(subject.send(:generate_code)).to eq true
213
- end
160
+ describe '#generate_code_uri' do
161
+ it 'generates URI for fetching code' do
162
+ expect(subject.generate_code_uri).to eq \
163
+ 'https://api.thebase.in/1/oauth/authorize?client_id=fake_client_id&redirect_uri=http://fake_redirect_uri.com&state=&response_type=code&scope=read_items read_orders read_savings read_users read_users_mail write_items write_orders'
214
164
  end
215
165
  end
216
166
 
217
167
  describe '#grant_type' do
218
168
  context '@grant_type is not nil' do
219
169
  it 'returns @grant_type value authorization_code' do
220
- subject.instance_variable_set(:@grant_type, 'authorization_code')
170
+ subject.instance_variable_set(:@grant_type, nil)
171
+ subject.instance_variable_set(:@code, true)
221
172
  subject.send(:grant_type)
222
173
  expect(subject.instance_variable_get(:@grant_type)).to eq \
223
174
  'authorization_code'
@@ -355,11 +306,25 @@ EOS
355
306
  end
356
307
  end
357
308
 
358
- describe '#to_expires_at' do
359
- it 'returns expires_at' do
360
- expires_at = subject.send(:to_expires_at, 3600)
361
- expect(expires_at).to eq \
362
- Date.parse(Time.new(2016).to_s).to_time + 3600
309
+ describe '#expires_at=' do
310
+ let(:time) { Date.parse(Time.new(2016).to_s).to_time + 3600 }
311
+
312
+ context 'an arg given' do
313
+ it 'returns expires_at' do
314
+ subject.send(:expires_at=, time)
315
+ expect(subject.expires_at).to eq time
316
+ end
317
+ end
318
+
319
+ context 'none is given' do
320
+ it 'returns expires_at' do
321
+ allow(Time).to receive(:now) do
322
+ Date.parse(Time.new(2016).to_s).to_time
323
+ end
324
+
325
+ subject.send(:expires_at=)
326
+ expect(subject.expires_at).to eq time
327
+ end
363
328
  end
364
329
  end
365
330
  end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Base::Normalization do
4
+ describe '::deep_hash_normalize' do
5
+ context 'given arg is not a hash' do
6
+ let(:arg) { [] }
7
+
8
+ it 'returns arg' do
9
+ expect(Base::Normalization.recursive_hash_normalize_keys(arg)).to eq []
10
+ end
11
+ end
12
+
13
+ context 'given arg is a hash' do
14
+ let(:arg) { { 'a' => 1, 'b' => 2, 'c' => 3 } }
15
+
16
+ it 'calles Base::Normalization.deep_hash_normalize' do
17
+ allow(Base::Normalization).to receive(:deep_hash_normalize).with(arg) do
18
+ { a: 1,
19
+ b: 2,
20
+ c: 3 }
21
+ end
22
+
23
+ Base::Normalization.recursive_hash_normalize_keys(arg)
24
+ end
25
+
26
+ it 'returns given hash with symbol keys' do
27
+ expect(Base::Normalization.recursive_hash_normalize_keys(arg)).to eq(
28
+ a: 1,
29
+ b: 2,
30
+ c: 3)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '::recursive_hash_normalize_keys' do
36
+ context 'given arg is a shallow hash' do
37
+ context 'given hash with string keys' do
38
+ let(:arg) { { 'a' => 1, 'b' => 2, 'c' => 3 } }
39
+
40
+ it 'returns given hash with symbol keys' do
41
+ expect(Base::Normalization.deep_hash_normalize(arg)).to eq(
42
+ a: 1,
43
+ b: 2,
44
+ c: 3)
45
+ end
46
+ end
47
+
48
+ context 'given hash with symbol keys' do
49
+ let(:arg) { { a: 1, b: 2, c: 3 } }
50
+
51
+ it 'returns given hash with symbol keys' do
52
+ expect(Base::Normalization.deep_hash_normalize(arg)).to eq(
53
+ a: 1,
54
+ b: 2,
55
+ c: 3)
56
+ end
57
+ end
58
+ end
59
+
60
+ context 'given arg is a deep hash' do
61
+ let(:arg) do
62
+ {
63
+ 'a' => 1,
64
+ b: {
65
+ 'c' => [2, 3, 4],
66
+ 'd' => { 'e' => 'hello' } } }
67
+ end
68
+
69
+ it 'returns given hash with symbol keys' do
70
+ expect(Base::Normalization.deep_hash_normalize(arg)).to eq(
71
+ a: 1,
72
+ b: {
73
+ c: [2, 3, 4],
74
+ d: { e: 'hello' } })
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "client_id": "fake_client_id",
3
3
  "client_secret": "fake_client_secret",
4
- "code": "fake_code",
5
4
  "redirect_uri": "http://fake_redirect_uri.com",
6
5
  "search_client_id": "fake_search_client_id",
7
6
  "search_client_secret": "fake_search_client_secret"
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: base-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.beta
4
+ version: 0.2.4.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-22 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: os
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.9.6
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.9.6
11
+ date: 2016-02-23 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description:
28
14
  email:
29
15
  - bluewhale1982@gmail.com
@@ -44,6 +30,7 @@ files:
44
30
  - lib/base/apis/category.rb
45
31
  - lib/base/apis/delivery_company.rb
46
32
  - lib/base/apis/item_category/detail.rb
33
+ - lib/base/apis/normalization.rb
47
34
  - lib/base/apis/order.rb
48
35
  - lib/base/apis/saving.rb
49
36
  - lib/base/apis/search.rb
@@ -51,6 +38,7 @@ files:
51
38
  - spec/base/apis/category_spec.rb
52
39
  - spec/base/apis/delivery_company_spec.rb
53
40
  - spec/base/apis/item_category/detail_spec.rb
41
+ - spec/base/apis/normalization_spec.rb
54
42
  - spec/base/apis/order_spec.rb
55
43
  - spec/base/apis/saving_spec.rb
56
44
  - spec/base/apis/search_spec.rb
@@ -94,6 +82,7 @@ test_files:
94
82
  - spec/base/apis/category_spec.rb
95
83
  - spec/base/apis/delivery_company_spec.rb
96
84
  - spec/base/apis/item_category/detail_spec.rb
85
+ - spec/base/apis/normalization_spec.rb
97
86
  - spec/base/apis/order_spec.rb
98
87
  - spec/base/apis/saving_spec.rb
99
88
  - spec/base/apis/search_spec.rb