Sha256: 35f6551792a852023f29241ecf2920b12bd43424b8433f8a244fb2b267fce15a

Contents?: true

Size: 1.76 KB

Versions: 14

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'friendly_shipping/rate'
require 'friendly_shipping/api_result'
require 'friendly_shipping/services/ups_freight/shipping_methods'

module FriendlyShipping
  module Services
    class UpsFreight
      class ParseFreightRateResponse
        class << self
          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
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.8.1 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb