Sha256: fa957d4948464ad032ddd501be5454a0a2c56f17605ff16c7f493205b97031b2

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  module Services
    class ShipEngineLTL
      # Parses the carriers API response.
      class ParseCarrierResponse
        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 [Success<ApiResult<Array<Carrier>>>, Failure<ApiFailure<Array<String>>>] the parsed carriers or errors
          def call(request:, response:)
            parsed_json = JSON.parse(response.body)
            carriers = parsed_json.fetch('carriers', []).map do |carrier_data|
              FriendlyShipping::Carrier.new(
                id: carrier_data['carrier_id'],
                name: carrier_data['name'],
                data: {
                  countries: carrier_data['countries'],
                  features: carrier_data['features'],
                  scac: carrier_data['scac']
                }
              )
            end

            if carriers.any?
              Success(
                ApiResult.new(
                  carriers,
                  original_request: request,
                  original_response: response
                )
              )
            else
              errors = parsed_json.fetch('errors', [{ 'message' => 'Unknown error' }])
              Failure(
                ApiResult.new(
                  errors.map { |e| e['message'] },
                  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/ship_engine_ltl/parse_carrier_response.rb