Sha256: 46ce57961b8874abdcf9cfce081fb41df748bc939f4133822e04a1e1c93eece7

Contents?: true

Size: 1.98 KB

Versions: 18

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

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

module FriendlyShipping
  module Services
    class UpsFreight
      class ParseFreightRateResponse
        class << self
          def call(request:, response:)
            parsed_json = ParseJSONResponse.call(response.body, 'FreightRateResponse')

            parsed_json.fmap do |json|
              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
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
friendly_shipping-0.5.2 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.5 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.14 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.13 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.12 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.11 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.10 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.9 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.8 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.7 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.6 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.5 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.4 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.3 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.2 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.1 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb
friendly_shipping-0.4.0 lib/friendly_shipping/services/ups_freight/parse_freight_rate_response.rb