Class: FriendlyShipping::Services::TForceFreight::ParseRatesResponse
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::TForceFreight::ParseRatesResponse
- Defined in:
- lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb
Overview
Parses a rates response into an ApiResult.
Class Method Summary collapse
-
.call(request:, response:) ⇒ ApiResult<Array<Rate>>
The parsed result.
Class Method Details
.call(request:, response:) ⇒ ApiResult<Array<Rate>>
Returns the parsed result.
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 47 48 49 50 51 52 53 |
# File 'lib/friendly_shipping/services/tforce_freight/parse_rates_response.rb', line 16 def call(request:, response:) json = JSON.parse(response.body) transaction_id = json.dig("summary", "transactionReference", "transactionId") rates = json['detail'].map do |detail| service_code = detail.dig("service", "code") shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code } total_amount = detail.dig("shipmentCharges", "total", "value") total_currency = detail.dig("shipmentCharges", "total", "currency") total = Money.new(total_amount.to_f * 100, total_currency) rates = detail['rate'] data = rates.each_with_object({}) do |rate, result| result[rate['code'].downcase.to_sym] = rate['value'].to_f end data.merge( customer_context: transaction_id, commodities: Array.wrap(json['commodities']) ) days_in_transit = detail.dig("timeInTransit", "timeInTransit") data[:days_in_transit] = days_in_transit.to_i if days_in_transit FriendlyShipping::Rate.new( amounts: { total: total }, shipping_method: shipping_method, data: data ) end FriendlyShipping::ApiResult.new( rates, original_request: request, original_response: response ) end |