Sha256: 1cf2cc0311687aafa19fd7b40b74072bbed7829dd47453ec9073acb65d7323e8

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'json'
require 'friendly_shipping/services/rl/shipment_information'

module FriendlyShipping
  module Services
    class RL
      # Parses the response from the R+L API when creating a Bill of Lading (BOL).
      class ParseCreateBOLResponse
        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<ShipmentInformation>>] shipment info with the BOL document attached
          def call(request:, response:)
            parsed_json = JSON.parse(response.body)
            shipment_info = ShipmentInformation.new(
              pro_number: parsed_json['ProNumber'],
              pickup_request_number: parsed_json['PickupRequestNumber']
            )
            if shipment_info.valid?
              Success(
                ApiResult.new(
                  shipment_info,
                  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_create_bol_response.rb