Class: FriendlyShipping::Services::ShipEngineLTL::ParseCarrierResponse

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb

Overview

Parses the carriers API response.

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ Success<ApiResult<Array<Carrier>>>, Failure<ApiResult<Array<String>>>

Returns the parsed carriers or errors.

Parameters:

  • request (Request)

    the request to attach to the API result

  • response (Response)

    the response to parse

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/friendly_shipping/services/ship_engine_ltl/parse_carrier_response.rb', line 14

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