Class: FriendlyShipping::Services::Ups::SerializeTimeInTransitRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups/serialize_time_in_transit_request.rb

Constant Summary collapse

MAX_SHIPMENT_WEIGHT =
Measured::Weight.new(150, :pounds)

Class Method Summary collapse

Class Method Details

.call(shipment:, options:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/friendly_shipping/services/ups/serialize_time_in_transit_request.rb', line 9

def self.call(shipment:, options:)
  xml_builder = Nokogiri::XML::Builder.new do |xml|
    xml.TimeInTransitRequest do
      xml.Request do
        if options.customer_context
          xml.TransactionReference do
            xml.CustomerContext(options.customer_context)
          end
        end
        xml.RequestAction('TimeInTransit')
      end
      xml.TransitFrom do
        xml.AddressArtifactFormat do
          xml.PoliticalDivision2(shipment.origin.city)
          xml.PoliticalDivision1(shipment.origin.region.code)
          xml.CountryCode(shipment.origin.country.code)
          xml.PostcodePrimaryLow(shipment.origin.zip)
        end
      end
      xml.TransitTo do
        xml.AddressArtifactFormat do
          xml.PoliticalDivision2(shipment.destination.city)
          xml.PoliticalDivision1(shipment.destination.region.code)
          xml.CountryCode(shipment.destination.country.code)
          xml.PostcodePrimaryLow(shipment.destination.zip)
        end
      end
      xml.ShipmentWeight do
        xml.UnitOfMeasurement do
          xml.Code('LBS')
        end
        shipment_weight = shipment.packages.map(&:weight).inject(Measured::Weight(0, :pounds), :+)
        weight_for_ups = [shipment_weight, MAX_SHIPMENT_WEIGHT].min
        xml.Weight(weight_for_ups.convert_to(:pounds).value.to_f.round(3))
      end
      xml.InvoiceLineTotal do
        xml.CurrencyCode(options.invoice_total.currency.iso_code)
        xml.MonetaryValue(options.invoice_total.to_f.round(2))
      end
      xml.PickupDate(options.pickup.strftime('%Y%m%d'))
    end
  end
  xml_builder.to_xml
end