Sha256: 0225251e08f7ad6cae60d01fd1bdf8d2ebcf431adee53ace52aa0bdf87185bfd

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  module Services
    class ShipEngineLTL
      class ParseCarrierResponse
        extend Dry::Monads::Result::Mixin

        class << self
          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.8.1 lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb