13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/friendly_shipping/services/rl/parse_invoice_response.rb', line 13
def call(request:, response:)
parsed_json = JSON.parse(response.body)
data = (parsed_json['Documents'].find { _1["Type"] == "Invoice" } || {})["Data"]
document = ShipmentDocument.new(binary: data, format: :pdf, document_type: :invoice)
if document.valid?
Success(
ApiResult.new(
document,
original_request: request,
original_response: response
)
)
else
errors = if parsed_json["Messages"].present?
parsed_json["Messages"]
else
parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }]).
map { _1['ErrorMessage'] }
end
Failure(
ApiResult.new(
errors,
original_request: request,
original_response: response
)
)
end
end
|