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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51f95387de14e2c9ceea2d10fbb5260d3e6718ebce3e0a41d9754bd3b1bbe99c
|
4
|
+
data.tar.gz: 93d8a80f9e28f1b9e625f5ed522704e4f2c56cb7d76f4b313661d9a1fe46a5c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28c699654881ec643f3418378ba91602f153076b2825ab73e23d77c354f08dea226c64d3f986a586d8094dd60787508772c81f481be04a3928f215ec33b2a71d
|
7
|
+
data.tar.gz: e766de77fc878b6e8db5cac215f0921acf43e8b68af5b6ff499734aaff246cda74ce3d63da1c1f9af1d6a861be781811776c746798b2e7e87fad651fc6d736c9
|
data/examples/tariff_contract.rb
CHANGED
@@ -4,20 +4,23 @@ require 'blood_contracts/core'
|
|
4
4
|
require "pry"
|
5
5
|
|
6
6
|
module Types
|
7
|
-
class
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
20
|
-
|
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 <
|
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
|
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 <
|
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 <
|
124
|
+
class RecoverableInputError < Types::Base
|
129
125
|
alias :parsed_response :value
|
130
|
-
def
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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 <
|
145
|
+
class OtherError < Types::Base
|
158
146
|
alias :parsed_response :value
|
159
|
-
def
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
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 <
|
161
|
+
class DomesticTariff < Types::Base
|
178
162
|
alias :parsed_response :value
|
179
|
-
def
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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 <
|
190
|
+
class InternationalTariff < Types::Base
|
211
191
|
alias :parsed_response :value
|
212
|
-
def
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
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
|
224
|
+
(Types::JSON.and_then(DomesticTariff | KnownError)).set(names: %i(parsed mapped))
|
249
225
|
InternationalResponse =
|
250
|
-
(Types::JSON
|
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
|
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
|
]
|
@@ -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
|