Class: FriendlyShipping::Services::ShipEngine::ParseCarrierResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb

Overview

Parses the carriers API response.

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ ApiResult<Array<Carrier>] the parsed carriers

Returns ApiResult] the parsed carriers.

Parameters:

  • request (Request)

    the request to attach to the API result

  • response (Response)

    the response to parse

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/friendly_shipping/services/ship_engine/parse_carrier_response.rb', line 12

def call(request:, response:)
  parsed_json = JSON.parse(response.body)
  carriers = parsed_json['carriers'].map do |carrier_data|
    carrier = FriendlyShipping::Carrier.new(
      id: carrier_data['carrier_id'],
      name: carrier_data['friendly_name'],
      code: carrier_data['carrier_code'],
      balance: carrier_data['balance'],
      data: carrier_data
    )

    carrier_data['services'].each do |method_hash|
      shipping_method = parse_shipping_method(carrier, method_hash)
      carrier.shipping_methods << shipping_method
    end

    carrier
  end

  ApiResult.new(
    carriers,
    original_request: request,
    original_response: response
  )
end