blood_contracts-core 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85f39b273547e389e3938ab33f8182857ef7c0d8f573e5664cb877c40c4d991a
4
- data.tar.gz: a8e6d07a8311096fdb4a83d7c30a510807b6713a29ad620b4e0e174c09448346
3
+ metadata.gz: 51f95387de14e2c9ceea2d10fbb5260d3e6718ebce3e0a41d9754bd3b1bbe99c
4
+ data.tar.gz: 93d8a80f9e28f1b9e625f5ed522704e4f2c56cb7d76f4b313661d9a1fe46a5c9
5
5
  SHA512:
6
- metadata.gz: 726747a300fa76dab9a86ac8efe2f79aae54bfadee6c12989a377833754cf5f912d585b214c23370e4b6bf75eecba05684b5f816ac6eb66a3b208505935ccf9d
7
- data.tar.gz: dd2d045b398fc3d8a82da25226a2b9f8cddc92e9b451111aa20b8c5ca4e44706d197d0b2eef665e776ef8d4d004ac57409d2c3c260bd650b624bb4da2ce9884d
6
+ metadata.gz: 28c699654881ec643f3418378ba91602f153076b2825ab73e23d77c354f08dea226c64d3f986a586d8094dd60787508772c81f481be04a3928f215ec33b2a71d
7
+ data.tar.gz: e766de77fc878b6e8db5cac215f0921acf43e8b68af5b6ff499734aaff246cda74ce3d63da1c1f9af1d6a861be781811776c746798b2e7e87fad651fc6d736c9
@@ -4,20 +4,23 @@ require 'blood_contracts/core'
4
4
  require "pry"
5
5
 
6
6
  module Types
7
- class JSON < BC::Refined
8
- def match
9
- super do
10
- begin
11
- context[:parsed] = ::JSON.parse(unpack_refined(@value))
12
- self
13
- rescue StandardError => error
14
- failure(error)
15
- end
16
- end
7
+ class ExceptionCaught < BC::ContractFailure; end
8
+ class Base < BC::Refined
9
+ def exception(ex, context: @context)
10
+ ExceptionCaught.new({ exception: ex }, context: context)
11
+ end
12
+ end
13
+
14
+ class JSON < Base
15
+ def _match
16
+ context[:parsed] = ::JSON.parse(unpack_refined(@value))
17
+ self
18
+ rescue StandardError => error
19
+ exception(error)
17
20
  end
18
21
 
19
- def unpack
20
- super { |match| match.context[:parsed] }
22
+ def _unpack(match)
23
+ match.context[:parsed]
21
24
  end
22
25
  end
23
26
  end
@@ -34,21 +37,14 @@ module RussianPost
34
37
  end
35
38
 
36
39
  class InputValidationFailure < BC::ContractFailure; end
37
- class ExceptionCaught < BC::ContractFailure; end
38
-
39
- class BaseType < BC::Refined
40
- def exception(ex, context: @context)
41
- ExceptionCaught.new({ exception: ex }, context: context)
42
- end
43
- end
44
40
 
45
- class DomesticParcel < BaseType
41
+ class DomesticParcel < Types::Base
46
42
  self.failure_klass = InputValidationFailure
47
43
 
48
44
  alias :parcel :value
49
45
  def _match
50
46
  return failure(key: :undef_weight, field: :weight) unless parcel.weight
51
- return self if domestic?
47
+ return if domestic?
52
48
  failure(non_domestic_error)
53
49
  rescue StandardError => error
54
50
  exception(error)
@@ -84,7 +80,7 @@ module RussianPost
84
80
  end
85
81
  end
86
82
 
87
- class InternationalParcel < BaseType
83
+ class InternationalParcel < Types::Base
88
84
  self.failure_klass = InputValidationFailure
89
85
 
90
86
  alias :parcel :value
@@ -125,17 +121,13 @@ module RussianPost
125
121
  end
126
122
  end
127
123
 
128
- class RecoverableInputError < BC::Refined
124
+ class RecoverableInputError < Types::Base
129
125
  alias :parsed_response :value
130
- def match
131
- super do
132
- begin
133
- next self if [error_code, error_message].all?
134
- failure(not_a_recoverable_error)
135
- rescue StandardError => error
136
- failure(error)
137
- end
138
- end
126
+ def _match
127
+ return if [error_code, error_message].all?
128
+ failure(key: :not_a_recoverable_error)
129
+ rescue StandardError => error
130
+ exception(error)
139
131
  end
140
132
 
141
133
  def error_message
@@ -145,26 +137,18 @@ module RussianPost
145
137
 
146
138
  private
147
139
 
148
- def not_a_recoverable_error
149
- { key: :not_a_recoverable_error }
150
- end
151
-
152
140
  def error_code
153
141
  parsed_response.values_at("code", "error-code").compact.first
154
142
  end
155
143
  end
156
144
 
157
- class OtherError < BC::Refined
145
+ class OtherError < Types::Base
158
146
  alias :parsed_response :value
159
- def match
160
- super do
161
- begin
162
- next failure({key: :not_a_known_error}) if error_code.nil?
163
- self
164
- rescue StandardError => error
165
- failure(error)
166
- end
167
- end
147
+ def _match
148
+ return failure(key: :not_a_known_error) if error_code.nil?
149
+ self
150
+ rescue StandardError => error
151
+ exception(error)
168
152
  end
169
153
 
170
154
  private
@@ -174,18 +158,14 @@ module RussianPost
174
158
  end
175
159
  end
176
160
 
177
- class DomesticTariff < BC::Refined
161
+ class DomesticTariff < Types::Base
178
162
  alias :parsed_response :value
179
- def match
180
- super do
181
- begin
182
- next self if is_a_domestic_tariff?
183
- context[:raw_response] = parsed_response
184
- failure({key: :not_a_domestic_tariff})
185
- rescue StandardError => error
186
- failure(error)
187
- end
188
- end
163
+ def _match
164
+ return if is_a_domestic_tariff?
165
+ context[:raw_response] = parsed_response
166
+ failure(key: :not_a_domestic_tariff)
167
+ rescue StandardError => error
168
+ exception(error)
189
169
  end
190
170
 
191
171
  def cost
@@ -207,18 +187,14 @@ module RussianPost
207
187
  end
208
188
  end
209
189
 
210
- class InternationalTariff < BC::Refined
190
+ class InternationalTariff < Types::Base
211
191
  alias :parsed_response :value
212
- def match
213
- super do
214
- begin
215
- next self if is_an_international_tariff?
216
- context[:raw_response] = parsed_response
217
- failure({key: :not_an_international_tariff})
218
- rescue StandardError => error
219
- failure(error)
220
- end
221
- end
192
+ def _match
193
+ return if is_an_international_tariff?
194
+ context[:raw_response] = parsed_response
195
+ failure(key: :not_an_international_tariff)
196
+ rescue StandardError => error
197
+ exception(error)
222
198
  end
223
199
 
224
200
  def cost
@@ -245,9 +221,9 @@ module RussianPost
245
221
  KnownError = RecoverableInputError | OtherError
246
222
 
247
223
  DomesticResponse =
248
- (Types::JSON > (DomesticTariff | KnownError)).set(names: %i(parsed mapped))
224
+ (Types::JSON.and_then(DomesticTariff | KnownError)).set(names: %i(parsed mapped))
249
225
  InternationalResponse =
250
- (Types::JSON > (InternationalTariff | KnownError)).set(names: %i(parsed mapped))
226
+ (Types::JSON.and_then(InternationalTariff | KnownError)).set(names: %i(parsed mapped))
251
227
 
252
228
  TariffRequestContract = ::BC::Contract.new(
253
229
  DomesticParcel => DomesticResponse,
@@ -263,7 +239,7 @@ end
263
239
 
264
240
  def match_response(response)
265
241
  case response
266
- when RussianPost::ExceptionCaught
242
+ when Types::ExceptionCaught
267
243
  puts "Honeybadger.notify #{response.errors_h[:exception]}"
268
244
  when RussianPost::InputValidationFailure
269
245
  # работаем с тарифом
@@ -321,6 +297,7 @@ PARCELS = [
321
297
  RESPONSES = [
322
298
  '{"total-cost": 10000, "delivery-till": "2019-12-12"}',
323
299
  '{"total-rate": 100000, "total-vat": 1800}',
300
+ '{"total-rate": "some", "total-vat": "text"}',
324
301
  '{"code": 1010, "desc": "Too long address"}',
325
302
  '{"error-code": 2020, "error-details": ["Too heavy parcel"]}',
326
303
  ]
@@ -59,6 +59,10 @@ module BloodContracts
59
59
  end
60
60
  end
61
61
 
62
+ def errors
63
+ match.errors
64
+ end
65
+
62
66
  private def step_name(index)
63
67
  self.class.names[index] || index
64
68
  end
@@ -51,8 +51,8 @@ module BloodContracts
51
51
 
52
52
  def match
53
53
  return @match if defined? @match
54
- return @match = yield if block_given?
55
- return @match = _match if respond_to?(:_match)
54
+ return @match = (yield || self) if block_given?
55
+ return @match = (_match || self) if respond_to?(:_match)
56
56
  self
57
57
  end
58
58
  alias :call :match
@@ -50,6 +50,10 @@ module BloodContracts
50
50
  end
51
51
  end
52
52
 
53
+ def errors
54
+ match.errors
55
+ end
56
+
53
57
  def inspect
54
58
  "#<sum #{self.class.name} is #{self.class.sum_of.to_a.join(' or ')} (value=#{@value})>"
55
59
  end
@@ -1,5 +1,5 @@
1
1
  module BloodContracts
2
2
  module Core
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blood_contracts-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Dolganov