sale 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 26197a9b6a2de725d188563cbc5b19b9eef9d912a2d6f8df55bd98a8b988d816
4
- data.tar.gz: 62aa4db013989e0b6603db3b76ddc00789e338b4a2f89337e7f56f94d2d532f4
3
+ metadata.gz: 91c880bc22e32f5e06d49b7127c3841608760ae3a00222021bf30cdfb5b8570e
4
+ data.tar.gz: c389190b9f04ffbbf47d116db1173cf326b67bd5026181272f462eafdf138e40
5
5
  SHA512:
6
- metadata.gz: c205f5243b593c21d882659774e47ae8731feda68af6ede4d5db68bd34a6c1173649123aae6f85d93ceee5ed19ef9a80e0f19b6fd50fd365c7e52c2768092c45
7
- data.tar.gz: f56c55cd423b928450e5593a16bee23811b7268daa405b6249faa1727c053446bc541873f946214254dbdb3040e7099c18353dac282309bc939f443182d98a6f
6
+ metadata.gz: 0666d3d2de7f3f0508b9780b35787e5625124712d9e4af295b7657b41bd47f9bb9cb9970dd18fa3bd4d39ba004ad4d976e7fed1e0e7dbb3401a2a502159aa228
7
+ data.tar.gz: 1afc01b9ef4bc598558aa5cfe68893602e4a68b27852821b7e37a680117bba7e4f11f3955a303e6fc9a9afab3d72799ddab6872366b6abc0f38b39364d34221d
data/lib/sale/buyers.rb CHANGED
@@ -1,15 +1,14 @@
1
1
  require_relative './base'
2
2
 
3
3
  class Bsale::Buyers
4
-
5
- def initialize(root)
6
- @client = root
7
- end
8
-
9
4
  class BuyerNotFoundError < Bsale::InvalidResponseError; end
10
5
 
11
6
  EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
12
7
 
8
+ def initialize(root)
9
+ @root = root
10
+ end
11
+
13
12
  def find_by_email_and_code(email, code = nil)
14
13
  if list = find_all_by_email(email) and list.any?
15
14
  with_code = list.select { |b| code.nil? ? (b.code || '').size > 0 : (code.to_s == b.code.to_s) }
@@ -27,16 +26,29 @@ class Bsale::Buyers
27
26
  get_buyers_with(email: email)
28
27
  end
29
28
 
30
- def find_all_by_email(code)
29
+ def find_all_by_code(code)
31
30
  get_buyers_with(code: code)
32
31
  end
33
32
 
34
- def create(firstName:, lastName:, address:, email:, phone:, code:, note:, activity:, municipality:, city:)
33
+ def find_or_create(data)
34
+ matches = if data[:code].present? # has rut, either from order contact info or company record
35
+ find_all_by_code(buyer.code)
36
+ else
37
+ find_all_by_code(FakeRUT.for(data[:code]))
38
+ end
39
+
40
+ matches.any ? matches.first : create(data)
41
+ end
42
+
43
+ def create(firstName:, lastName:, email:, phone:, address:, municipality:, city:, code: nil, note: nil, activity: nil)
44
+ raise "Invalid email: #{email}" unless email.match(EMAIL_REGEX)
45
+ # is_foreign = buyer.code.to_s.gsub(/[^0-9]/, '').to_i == 55_555_555
46
+
35
47
  data = {
36
48
  accumulatePoints: 1,
37
49
  firstName: firstName,
38
50
  lastName: lastName,
39
- code: code,
51
+ code: code && code != '' ? code : FakeRUT.for(email),
40
52
  address: address,
41
53
  email: email,
42
54
  phone: phone,
@@ -46,8 +58,7 @@ class Bsale::Buyers
46
58
  activity: activity # date of birth
47
59
  }
48
60
 
49
- return nil unless email.match(EMAIL_REGEX)
50
- client.post("/clients.json", data)
61
+ root.post("/clients.json", data)
51
62
  end
52
63
 
53
64
  def update(id, data)
@@ -56,7 +67,7 @@ class Bsale::Buyers
56
67
  end
57
68
 
58
69
  # puts "Updating client #{id} with data: #{data.inspect}"
59
- client.put("/clients/#{id}.json", data.merge(id: id))
70
+ root.put("/clients/#{id}.json", data.merge(id: id))
60
71
  end
61
72
 
62
73
  def update_points(buyer, difference, order_code)
@@ -70,27 +81,16 @@ class Bsale::Buyers
70
81
  orderId: order_code
71
82
  }
72
83
 
73
- resp = client.request(:put, "/clients/points.json", data)
74
- if resp.code == 200
75
- return parse_json(resp.body)['points']
76
- else
77
- raise InvalidResponseError.new("#{resp.code}: #{resp.body}")
78
- end
84
+ resp = root.put('/clients/points.json', data)
85
+ # resp.points
79
86
  end
80
87
 
81
88
  private
82
- attr_reader :client
89
+ attr_reader :root
83
90
 
84
91
  def get_buyers_with(attrs)
85
- if data = get_buyers(attrs.merge(state: 0)) and data['items']
86
- data['items'].map { |item| OpenStruct.new(item) }
87
- else
88
- []
89
- end
90
- end
91
-
92
- def get_buyers(query)
93
- client.clients(query)
92
+ # root.clients(attrs.merge(state: 0))
93
+ root.clients(attrs)
94
94
  end
95
95
 
96
96
  end
data/lib/sale/entity.rb CHANGED
@@ -22,6 +22,12 @@ class Entity
22
22
  data[key.to_s] = obj
23
23
  end
24
24
 
25
+ def size
26
+ (to_a || []).size
27
+ end
28
+
29
+ alias_method :count, :size
30
+
25
31
  def each(&block)
26
32
  to_a.each(&block)
27
33
  end
@@ -0,0 +1,22 @@
1
+ module FakeRUT
2
+
3
+ def self.for(str)
4
+ num = Digest::SHA1.hexdigest(str)[0..6].to_i(16)
5
+ "#{num}-#{get_rut_digit(num)}"
6
+ end
7
+
8
+ private
9
+
10
+ def self.get_digit(num)
11
+ m, s, t = 0, 1, num.to_i
12
+
13
+ while t > 0
14
+ s = (s + t % 10 * (9 - m % 6)) % 11
15
+ t = (t / 10).floor
16
+ m += 1
17
+ end
18
+
19
+ v = (s > 0) ? (s-1).to_s : 'K'
20
+ end
21
+
22
+ end
data/sale.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "sale"
6
- s.version = '0.1.4'
6
+ s.version = '0.1.5'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ['Tomás Pollak']
9
9
  s.email = ['tomas@bootic.net']
data/spec/buyers_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'bundler/setup'
2
- require_relative '../lib/bsale'
2
+ require_relative '../lib/sale'
3
3
 
4
4
  RSpec.configure do |config|
5
5
  config.color = true
@@ -10,24 +10,24 @@ describe 'Bsale' do
10
10
  describe 'Buyers' do
11
11
 
12
12
  let(:client) {
13
- Bsale::Buyers.new('foo')
13
+ Bsale::Root.new('foo').buyers
14
14
  }
15
15
 
16
16
  let(:fake_response) {
17
- double('Response', code: 200, body: '{"count": 0}')
17
+ double('Response', code: 200, body: '{"count": 0}', success?: true)
18
18
  }
19
19
 
20
20
  before do
21
21
  allow(Dagger).to receive(:request).and_return(fake_response)
22
22
  end
23
23
 
24
- describe 'get_buyer_with_email_and_code' do
24
+ describe 'find_by_email_and_code' do
25
25
 
26
26
  describe 'when email is invalid' do
27
27
 
28
28
  it 'returns nil, without making a request' do
29
29
  expect(client).not_to receive(:request)
30
- client.get_buyer_with_email_and_code('aaa')
30
+ client.find_by_email_and_code('aaa')
31
31
  end
32
32
 
33
33
  end
@@ -39,11 +39,11 @@ describe 'Bsale' do
39
39
  end
40
40
 
41
41
  it 'returns nil without code' do
42
- client.get_buyer_with_email_and_code('aaa@foo.com')
42
+ client.find_by_email_and_code('aaa@foo.com')
43
43
  end
44
44
 
45
45
  it 'returns nil with code' do
46
- client.get_buyer_with_email_and_code('aaa@foo.com', '123')
46
+ client.find_by_email_and_code('aaa@foo.com', '123')
47
47
  end
48
48
 
49
49
  end
@@ -55,18 +55,18 @@ describe 'Bsale' do
55
55
  end
56
56
 
57
57
  it 'returns result, without code' do
58
- res = client.get_buyer_with_email_and_code('aaa@foo.com')
58
+ res = client.find_by_email_and_code('aaa@foo.com')
59
59
  expect(res).to eql(OpenStruct.new({ code: 123, accumulatePoints: '1' }))
60
60
  end
61
61
 
62
62
  it 'returns result, with matching code' do
63
- res = client.get_buyer_with_email_and_code('aaa@foo.com', '123')
63
+ res = client.find_by_email_and_code('aaa@foo.com', '123')
64
64
  expect(res).to eql(OpenStruct.new({ code: 123, accumulatePoints: '1' }))
65
65
  end
66
66
 
67
67
  it 'returns nil, with non-matching code' do
68
68
  expect do
69
- client.get_buyer_with_email_and_code('aaa@foo.com', '234')
69
+ client.find_by_email_and_code('aaa@foo.com', '234')
70
70
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
71
71
  end
72
72
 
@@ -80,18 +80,18 @@ describe 'Bsale' do
80
80
  end
81
81
 
82
82
  it 'returns result, without code' do
83
- res = client.get_buyer_with_email_and_code('aaa@foo.com')
83
+ res = client.find_by_email_and_code('aaa@foo.com')
84
84
  expect(res).to eql(OpenStruct.new({ code: 123, accumulatePoints: '1' }))
85
85
  end
86
86
 
87
87
  it 'returns result, with matching code' do
88
- res = client.get_buyer_with_email_and_code('aaa@foo.com', '123')
88
+ res = client.find_by_email_and_code('aaa@foo.com', '123')
89
89
  expect(res).to eql(OpenStruct.new({ code: 123, accumulatePoints: '1' }))
90
90
  end
91
91
 
92
92
  it 'returns nil, with non-matching code' do
93
93
  expect do
94
- client.get_buyer_with_email_and_code('aaa@foo.com', '234')
94
+ client.find_by_email_and_code('aaa@foo.com', '234')
95
95
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
96
96
  end
97
97
 
@@ -105,19 +105,19 @@ describe 'Bsale' do
105
105
 
106
106
  it 'returns nil, without code' do
107
107
  expect do
108
- client.get_buyer_with_email_and_code('aaa@foo.com')
108
+ client.find_by_email_and_code('aaa@foo.com')
109
109
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
110
110
  end
111
111
 
112
112
  it 'returns nil, with matching code' do
113
113
  expect do
114
- client.get_buyer_with_email_and_code('aaa@foo.com', '123')
114
+ client.find_by_email_and_code('aaa@foo.com', '123')
115
115
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
116
116
  end
117
117
 
118
118
  it 'returns nil, with non-matching code' do
119
119
  expect do
120
- client.get_buyer_with_email_and_code('aaa@foo.com', '234')
120
+ client.find_by_email_and_code('aaa@foo.com', '234')
121
121
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
122
122
  end
123
123
 
@@ -132,18 +132,18 @@ describe 'Bsale' do
132
132
 
133
133
  it 'returns nil, without code' do
134
134
  expect do
135
- client.get_buyer_with_email_and_code('aaa@foo.com')
135
+ client.find_by_email_and_code('aaa@foo.com')
136
136
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
137
137
  end
138
138
 
139
139
  it 'returns result, when code matches one' do
140
- res = client.get_buyer_with_email_and_code('aaa@foo.com', '123')
140
+ res = client.find_by_email_and_code('aaa@foo.com', '123')
141
141
  expect(res).to eql(OpenStruct.new({ code: 123, accumulatePoints: '1' }))
142
142
  end
143
143
 
144
144
  it 'returns nil, when code doesnt match any' do
145
145
  expect do
146
- client.get_buyer_with_email_and_code('aaa@foo.com', '234')
146
+ client.find_by_email_and_code('aaa@foo.com', '234')
147
147
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
148
148
  end
149
149
 
@@ -157,13 +157,13 @@ describe 'Bsale' do
157
157
 
158
158
  it 'returns nil, without code' do
159
159
  expect do
160
- client.get_buyer_with_email_and_code('aaa@foo.com')
160
+ client.find_by_email_and_code('aaa@foo.com')
161
161
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
162
162
  end
163
163
 
164
164
  it 'returns nil, with code' do
165
165
  expect do
166
- client.get_buyer_with_email_and_code('aaa@foo.com', '123')
166
+ client.find_by_email_and_code('aaa@foo.com', '123')
167
167
  end.to raise_error(Bsale::Buyers::BuyerNotFoundError)
168
168
  end
169
169
 
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.1.4
4
+ version: 0.1.5
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-22 00:00:00.000000000 Z
11
+ date: 2019-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,6 +93,7 @@ files:
93
93
  - lib/sale/base.rb
94
94
  - lib/sale/buyers.rb
95
95
  - lib/sale/entity.rb
96
+ - lib/sale/fake_rut.rb
96
97
  - lib/sale/invoices.rb
97
98
  - lib/sale/root.rb
98
99
  - sale.gemspec