Class: FriendlyShipping::Services::ShipEngine::SerializeLabelShipment

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb

Overview

Serializes a shipment and options for the label API request.

Class Method Summary collapse

Class Method Details

.call(shipment:, options:, test:) ⇒ Hash

Returns the serialized request.

Parameters:

  • shipment (Physical::Shipment)

    the shipment to serialize

  • options (LabelOptions)

    the options to serialize

  • test (Boolean)

    whether to use the test API

Returns:

  • (Hash)

    the serialized request



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
# File 'lib/friendly_shipping/services/ship_engine/serialize_label_shipment.rb', line 13

def call(shipment:, options:, test:)
  shipment_hash = {
    label_format: options.label_format,
    label_download_type: options.label_download_type,
    shipment: {
      service_code: options.shipping_method.service_code,
      ship_to: serialize_address(shipment.destination),
      ship_from: serialize_address(shipment.origin),
      packages: serialize_packages(shipment.packages, options)
    }
  }
  # A carrier might not be necessary if the service code is unique within ShipEngine.
  if options.shipping_method.carrier
    shipment_hash[:shipment][:carrier_id] = options.shipping_method.carrier.id
  end

  if international?(shipment)
    shipment_hash[:shipment][:customs] = serialize_customs(shipment.packages, options)
  end

  # Not all carriers support test labels
  if test
    shipment_hash[:test_label] = true
  end

  if options.label_image_id
    shipment_hash[:label_image_id] = options.label_image_id
  end

  shipment_hash
end