sale 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sale/base.rb +9 -1
- data/lib/sale/buyers.rb +2 -0
- data/lib/sale/entity.rb +33 -35
- data/lib/sale/fake_rut.rb +2 -0
- data/sale.gemspec +1 -1
- data/spec/base_spec.rb +21 -0
- data/spec/buyers_spec.rb +10 -5
- data/spec/entity_spec.rb +47 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2bdb714aece35dd7f04de403c5f36e973572a390938d0da5af1368b3d20563f
|
4
|
+
data.tar.gz: 61c118538568e2566b1f75a1a579f36cb0504b98a30404441a3967154188a476
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2616e683c8f148bdb90a6ada8d151104fb092d38232fad25e428aee84360702b51c3cf4347aca41e1447e923b4d7e575ee31e104fbac5e81b0c37b1309829fa9
|
7
|
+
data.tar.gz: bf78adc18c95bc390289302ebfeafe9002e14f2e8e55ec6b0da1d0eeabd2d2215ab82387fa1661b7d254ec46905fc6865c322be6798cfecb1b172e6cea5a5cc7
|
data/lib/sale/base.rb
CHANGED
@@ -8,6 +8,7 @@ module Bsale
|
|
8
8
|
class APIBase
|
9
9
|
ENDPOINT = 'https://api.bsale.cl'.freeze
|
10
10
|
VERSION = 'v1'.freeze
|
11
|
+
MAX_RETRIES = 5
|
11
12
|
|
12
13
|
def initialize(token)
|
13
14
|
@token = token or raise 'Token not set!'
|
@@ -34,7 +35,7 @@ module Bsale
|
|
34
35
|
wrap_response(resp)
|
35
36
|
end
|
36
37
|
|
37
|
-
def request(method, path, body = nil)
|
38
|
+
def request(method, path, body = nil, attempt = 1)
|
38
39
|
headers = { 'access_token' => @token }
|
39
40
|
url = path['://'] ? path : [ ENDPOINT, VERSION, path ].join('/')
|
40
41
|
|
@@ -43,6 +44,13 @@ module Bsale
|
|
43
44
|
else
|
44
45
|
Dagger.request(method, url, body, { json: true, follow: 3, headers: headers })
|
45
46
|
end
|
47
|
+
rescue InvalidResponseError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EINVAL, Timeout::Error, \
|
48
|
+
SocketError, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, OpenSSL::SSL::SSLError => e
|
49
|
+
|
50
|
+
raise if attempt > MAX_RETRIES
|
51
|
+
puts "Connection refused. Retrying in a few secs... (attempt #{attempt})"
|
52
|
+
sleep(attempt * 5) # 5 secs, 10 secs, 15 secs, 20 secs
|
53
|
+
request(method, path, body, attempt + 1)
|
46
54
|
end
|
47
55
|
|
48
56
|
def connect
|
data/lib/sale/buyers.rb
CHANGED
data/lib/sale/entity.rb
CHANGED
@@ -1,19 +1,16 @@
|
|
1
1
|
class Entity
|
2
|
+
include Enumerable
|
2
3
|
|
3
4
|
attr_reader :data
|
4
5
|
|
5
6
|
def initialize(data, client, original_params = nil)
|
6
|
-
@data, @client, @original_params = data, client, original_params
|
7
|
+
@data, @client, @original_params = maybe_stringify(data), client, original_params
|
7
8
|
end
|
8
9
|
|
9
10
|
def to_s
|
10
11
|
@data.inspect
|
11
12
|
end
|
12
13
|
|
13
|
-
def to_a
|
14
|
-
data['items'] ? items : nil
|
15
|
-
end
|
16
|
-
|
17
14
|
def [](key)
|
18
15
|
data[key.to_s]
|
19
16
|
end
|
@@ -22,26 +19,10 @@ class Entity
|
|
22
19
|
data[key.to_s] = obj
|
23
20
|
end
|
24
21
|
|
25
|
-
def
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
alias_method :count, :size
|
30
|
-
|
31
|
-
def each(&block)
|
32
|
-
to_a.each(&block)
|
33
|
-
end
|
34
|
-
|
35
|
-
def each_with_index(&block)
|
36
|
-
to_a.each_with_index &block
|
37
|
-
end
|
38
|
-
|
39
|
-
def map(&block)
|
40
|
-
to_a.map(&block)
|
22
|
+
def to_a
|
23
|
+
data['items'] ? items : []
|
41
24
|
end
|
42
25
|
|
43
|
-
alias_method :collect, :map
|
44
|
-
|
45
26
|
def load(params = nil)
|
46
27
|
client.get(data['href'], params)
|
47
28
|
end
|
@@ -51,6 +32,16 @@ class Entity
|
|
51
32
|
load # just return a new instance
|
52
33
|
end
|
53
34
|
|
35
|
+
def each(&block)
|
36
|
+
to_a.each(&block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def count
|
40
|
+
to_a.size
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :size, :count
|
44
|
+
|
54
45
|
def items
|
55
46
|
return if data['items'].nil?
|
56
47
|
@items ||= data['items'].map { |obj| Entity.new(obj, client) }
|
@@ -80,20 +71,15 @@ class Entity
|
|
80
71
|
data['next'] ? follow_link(data['next'], @original_params) : nil
|
81
72
|
end
|
82
73
|
|
83
|
-
def method_missing(name)
|
74
|
+
def method_missing(name, *args)
|
84
75
|
name = name.to_s
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
if is_hash?(data[name])
|
90
|
-
Entity.new(data[name], client)
|
91
|
-
else
|
92
|
-
data[name]
|
93
|
-
end
|
76
|
+
if is_hash?(data[name])
|
77
|
+
Entity.new(data[name], client)
|
78
|
+
elsif data[name]
|
79
|
+
data[name]
|
94
80
|
else
|
95
|
-
|
96
|
-
#
|
81
|
+
nil
|
82
|
+
# raise "Not found: #{name} in #{data}"
|
97
83
|
end
|
98
84
|
end
|
99
85
|
|
@@ -104,10 +90,22 @@ class Entity
|
|
104
90
|
# is_hash?(obj) && obj['href'] && obj.keys == ['href']
|
105
91
|
# end
|
106
92
|
|
93
|
+
# def is_integer?(val)
|
94
|
+
# Integer(val)
|
95
|
+
# true
|
96
|
+
# rescue
|
97
|
+
# false
|
98
|
+
# end
|
99
|
+
|
107
100
|
def is_hash?(obj)
|
108
101
|
obj.is_a?(Hash)
|
109
102
|
end
|
110
103
|
|
104
|
+
def maybe_stringify(obj)
|
105
|
+
return obj unless is_hash?(obj)
|
106
|
+
obj.inject({}) { |memo,(k,v) | memo[k.to_s] = v; memo }
|
107
|
+
end
|
108
|
+
|
111
109
|
def follow_link(href, params = nil)
|
112
110
|
client.get(href, params)
|
113
111
|
end
|
data/lib/sale/fake_rut.rb
CHANGED
data/sale.gemspec
CHANGED
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require_relative '../lib/sale'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.color = true
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'APIBase' do
|
9
|
+
|
10
|
+
let(:subject) { Bsale::APIBase.new(token) }
|
11
|
+
let(:token) { '123456' }
|
12
|
+
|
13
|
+
describe '#request' do
|
14
|
+
it 'explodes with 401 error if invalid token' do
|
15
|
+
expect do
|
16
|
+
resp = subject.get('foo')
|
17
|
+
end.to raise_error(Bsale::InvalidResponseError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/buyers_spec.rb
CHANGED
@@ -56,12 +56,14 @@ describe 'Bsale' do
|
|
56
56
|
|
57
57
|
it 'returns result, without code' do
|
58
58
|
res = client.find_by_email_and_code('aaa@foo.com')
|
59
|
-
expect(res).to
|
59
|
+
expect(res).to be_a(Entity) # (OpenStruct.new({ code: 123, accumulatePoints: '1' }))
|
60
|
+
expect(res.code).to eq(123)
|
60
61
|
end
|
61
62
|
|
62
63
|
it 'returns result, with matching code' do
|
63
64
|
res = client.find_by_email_and_code('aaa@foo.com', '123')
|
64
|
-
expect(res).to
|
65
|
+
expect(res).to be_a(Entity)
|
66
|
+
expect(res.code).to eq(123)
|
65
67
|
end
|
66
68
|
|
67
69
|
it 'returns nil, with non-matching code' do
|
@@ -81,12 +83,14 @@ describe 'Bsale' do
|
|
81
83
|
|
82
84
|
it 'returns result, without code' do
|
83
85
|
res = client.find_by_email_and_code('aaa@foo.com')
|
84
|
-
expect(res).to
|
86
|
+
expect(res).to be_a(Entity)
|
87
|
+
expect(res.code).to eq(123)
|
85
88
|
end
|
86
89
|
|
87
90
|
it 'returns result, with matching code' do
|
88
91
|
res = client.find_by_email_and_code('aaa@foo.com', '123')
|
89
|
-
expect(res).to
|
92
|
+
expect(res).to be_a(Entity)
|
93
|
+
expect(res.code).to eq(123)
|
90
94
|
end
|
91
95
|
|
92
96
|
it 'returns nil, with non-matching code' do
|
@@ -138,7 +142,8 @@ describe 'Bsale' do
|
|
138
142
|
|
139
143
|
it 'returns result, when code matches one' do
|
140
144
|
res = client.find_by_email_and_code('aaa@foo.com', '123')
|
141
|
-
expect(res).to
|
145
|
+
expect(res).to be_a(Entity)
|
146
|
+
expect(res.code).to eq(123)
|
142
147
|
end
|
143
148
|
|
144
149
|
it 'returns nil, when code doesnt match any' do
|
data/spec/entity_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require_relative '../lib/sale'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.color = true
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'Entity' do
|
9
|
+
|
10
|
+
describe '#to_a' do
|
11
|
+
it 'returns empty array if no items' do
|
12
|
+
obj = Entity.new({ foo: 'bar '}, nil)
|
13
|
+
expect(obj.to_a).to eq([])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#count' do
|
18
|
+
it 'returns 0 array if no items' do
|
19
|
+
obj = Entity.new({ foo: 'bar '}, nil)
|
20
|
+
expect(obj.count).to eq(0)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns number if items' do
|
24
|
+
obj = Entity.new({ items: [1] }, nil)
|
25
|
+
expect(obj.count).to eq(1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#each_with_index' do
|
30
|
+
it 'works beautifully' do
|
31
|
+
obj = Entity.new({ foo: 'bar', items: [1, 2, 3] }, nil)
|
32
|
+
res = []
|
33
|
+
obj.each_with_index { |el, i| res.push([el.data, i]) }
|
34
|
+
expect(res).to eq([[1,0], [2,1], [3,2]])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#select' do
|
39
|
+
it 'works beautifully' do
|
40
|
+
obj = Entity.new({ foo: 'bar', items: [1, 2, 3] }, nil)
|
41
|
+
res = obj.select { |el| el.data % 2 == 0 }
|
42
|
+
expect(res.map(&:data)).to eq([2])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,9 @@ files:
|
|
97
97
|
- lib/sale/invoices.rb
|
98
98
|
- lib/sale/root.rb
|
99
99
|
- sale.gemspec
|
100
|
+
- spec/base_spec.rb
|
100
101
|
- spec/buyers_spec.rb
|
102
|
+
- spec/entity_spec.rb
|
101
103
|
homepage: https://github.com/github/bsale-api-wrapper
|
102
104
|
licenses: []
|
103
105
|
metadata: {}
|