Class: FriendlyShipping::Services::RL::ParsePrintShippingLabelsResponse

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb

Overview

Parses the response from the R+L API when printing shipping labels.

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ Result<ApiResult<ShipmentDocument>>

Returns shipping document containing the labels.

Parameters:

  • request (Request)

    the request to attach to the API result

  • response (Response)

    the response to parse

Returns:



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
# File 'lib/friendly_shipping/services/rl/parse_print_shipping_labels_response.rb', line 14

def call(request:, response:)
  parsed_json = JSON.parse(response.body)
  label_doc = ShipmentDocument.new(
    format: :pdf,
    document_type: :label,
    binary: Base64.decode64(parsed_json['ShippingLabelsFile'])
  )
  if label_doc.valid?
    Success(
      ApiResult.new(
        label_doc,
        original_request: request,
        original_response: response
      )
    )
  else
    errors = parsed_json.fetch('Errors', [{ 'ErrorMessage' => 'Unknown error' }])
    Failure(
      ApiResult.new(
        errors.map { |e| e['ErrorMessage'] },
        original_request: request,
        original_response: response
      )
    )
  end
end