Sha256: f3f56e94630aa13f9ada5230df4dc64a7a5dc69cacb235522b61d40a99da9386

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

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

2 entries across 2 versions & 1 rubygems

Version Path
friendly_shipping-0.10.1 lib/friendly_shipping/services/rl/parse_print_bol_response.rb
friendly_shipping-0.10.0 lib/friendly_shipping/services/rl/parse_print_bol_response.rb