Sha256: 1aaef97a3a909112ab0520942a08b33f78d597e677dec8c33e9ce44ac5b1c3a3

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'json'
require 'friendly_shipping/services/rl/shipment_document'

module FriendlyShipping
  module Services
    class RL
      # Parses the response from the R+L API when printing a Bill of Lading (BOL).
      class ParsePrintBOLResponse
        extend Dry::Monads::Result::Mixin

        class << self
          # @param request [Request] the request to attach to the API result
          # @param response [Response] the response to parse
          # @return [Result<ApiResult<ShipmentDocument>>] shipment document containing the BOL
          def call(request:, response:)
            parsed_json = JSON.parse(response.body)
            bol_document = ShipmentDocument.new(
              format: :pdf,
              document_type: :rl_bol,
              binary: Base64.decode64(parsed_json['BolDocument'])
            )
            if bol_document.valid?
              Success(
                ApiResult.new(
                  bol_document,
                  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
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/rl/parse_print_bol_response.rb