Class: FriendlyShipping::Services::UpsFreight::ParseFreightRateResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ Object



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
37
38
39
40
41
42
43
44
45
# File 'lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb', line 12

def call(request:, response:)
  json = JSON.parse(response.body)

  service_code = json.dig("FreightRateResponse", "Service", "Code")
  shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }
  total_shipment_charge = json.dig("FreightRateResponse", "TotalShipmentCharge")
  currency = Money::Currency.new(total_shipment_charge['CurrencyCode'])
  amount = total_shipment_charge['MonetaryValue'].to_f
  total_money = Money.new(amount * currency.subunit_to_unit, currency)
  data = {
    customer_context: json.dig("FreightRateResponse", "TransactionReference", "TransactionIdentifier"),
    commodities: Array.wrap(json.dig("FreightRateResponse", "Commodity")),
    response_body: json
  }

  days_in_transit = json.dig("FreightRateResponse", "TimeInTransit", "DaysInTransit")
  if days_in_transit
    data[:days_in_transit] = days_in_transit.to_i
  end

  FriendlyShipping::ApiResult.new(
    [
      FriendlyShipping::Rate.new(
        amounts: {
          total: total_money
        },
        shipping_method: shipping_method,
        data: data
      )
    ],
    original_request: request,
    original_response: response
  )
end