Sha256: f50339c0078c3da9fa1b4bcc2d038782b0ba03cbd17ff1cfd66883828d28118e

Contents?: true

Size: 1.73 KB

Versions: 17

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  module Services
    class ShipEngine
      class ParseRateEstimateResponse
        extend Dry::Monads::Result::Mixin

        class << self
          def call(response:, request:, options:)
            parsed_json = JSON.parse(response.body)
            rates = parsed_json.map do |rate|
              carrier = options.carriers.detect { |c| c.id == rate['carrier_id'] }
              next unless carrier

              shipping_method = FriendlyShipping::ShippingMethod.new(
                carrier: carrier,
                service_code: rate['service_code']
              )

              amounts = get_amounts(rate)
              FriendlyShipping::Rate.new(
                shipping_method: shipping_method,
                amounts: amounts,
                remote_service_id: rate['rate_id'],
                delivery_date: Time.parse(rate['estimated_delivery_date']),
                warnings: rate['warning_messages'],
                errors: rate['error_messages']
              )
            end.compact

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

          private

          def get_amounts(rate_hash)
            [:shipping, :other, :insurance, :confirmation].map do |name|
              currency = Money::Currency.new(rate_hash["#{name}_amount"]["currency"])
              amount = rate_hash["#{name}_amount"]["amount"] * currency.subunit_to_unit
              [
                name,
                Money.new(amount, currency)
              ]
            end.to_h
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
friendly_shipping-0.6.4 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.5.2 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.5 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.14 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.13 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.12 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.11 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.10 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.9 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.8 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb
friendly_shipping-0.4.7 lib/friendly_shipping/services/ship_engine/parse_rate_estimate_response.rb