Sha256: 303192c7bd51209962e5a8eee3ad4a9d73a1b9a24bba00ac9ec41647b46e2a6e

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class UpsJson
      class GenerateTimingsPayload
        def self.call(shipment:, options:)
          {
            originCountryCode: shipment.origin.country.code,
            originStateProvince: region_name_or_code(shipment.origin, shipment.origin.region),
            originCityName: shipment.origin.city,
            originPostalCode: shipment.origin.zip,
            destinationCountryCode: shipment.destination.country.code,
            destinationStateProvince: region_name_or_code(shipment.destination, shipment.destination.region),
            destinationCityName: shipment.destination.city,
            destinationPostalCode: shipment.destination.zip,
            residentialIndicator: shipment.destination.commercial? ? '02' : '01',
            shipDate: options.pickup.strftime('%Y-%m-%d'),
            shipmentContentsCurrencyCode: 'USD',
            shipmentContentsValue: shipment_contents_value(shipment),
            weight: shipment.packages.sum(&:weight).value.to_s,
            weightUnitOfMeasure: 'LBS',
            billType: '02',
            numberOfPackages: shipment.packages.size.to_s
          }
        end

        def self.shipment_contents_value(shipment)
          shipment.packages.map do |package|
            package.items.sum { |item| item.cost || 0 }
          end.sum.to_d
        end

        def self.region_name_or_code(location, region)
          if location.country == "US"
            region.name
          else
            region.code
          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/ups_json/generate_timings_payload.rb