Sha256: abfaa30c2721c60e2a5195dbce6d470c87b5f770d3cdd21b21e48c9332ea2d7e

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class ShipEngine
      # Serializes a shipment and options for the rate estimates API request.
      class SerializeRateEstimateRequest
        class << self
          # @param shipment [Physical::Shipment] the shipment to serialize
          # @param options [LabelOptions] the options to serialize
          # @return [Hash] the serialized request
          def call(shipment:, options:)
            {
              carrier_ids: options.carrier_ids,
              from_country_code: shipment.origin.country.alpha_2_code,
              from_postal_code: shipment.origin.zip,
              to_country_code: shipment.destination.country.alpha_2_code,
              to_postal_code: shipment.destination.zip,
              to_city_locality: shipment.destination.city,
              to_state_province: shipment.destination.region.code,
              weight: {
                value: shipment.packages.map { |p| p.weight.convert_to(:pound).value.to_f }.sum.round(2),
                unit: 'pound'
              },
              dimensions: dimensions(shipment.packages),
              confirmation: 'none',
              ship_date: options.ship_date.strftime('%Y-%m-%d'),
            }.merge(SerializeAddressResidentialIndicator.call(shipment.destination)).compact_blank
          end

          private

          # @param packages [Array<Physical::Package>]
          # @return [Hash]
          def dimensions(packages)
            length = packages.map { |p| p.length.convert_to(:inch).value.to_f }.sum.round(2)
            width = packages.map { |p| p.width.convert_to(:inch).value.to_f }.sum.round(2)
            height = packages.map { |p| p.height.convert_to(:inch).value.to_f }.sum.round(2)
            return {} if length == 0 && width == 0 && height == 0

            {
              unit: 'inch',
              length: length,
              width: width,
              height: height
            }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/ship_engine/serialize_rate_estimate_request.rb