Class: Deliveries::Couriers::Ups::Shipments::Create

Inherits:
Object
  • Object
show all
Includes:
JsonRequest, LabelUtils
Defined in:
lib/deliveries/couriers/ups/shipments/create.rb

Constant Summary collapse

API_VERSION =
'v1807'.freeze
TYPES =
%i[forward return].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LabelUtils

image2pdf, merge_pdfs

Constructor Details

#initialize(shipper:, consignee:, parcels:, reference_code:, type: :forward, collection_point: nil, language: nil) ⇒ Create

Returns a new instance of Create.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 14

def initialize(shipper:, consignee:, parcels:, reference_code:, type: :forward, collection_point: nil, language: nil)
  raise ArgumentError, 'Invalid value for type' unless TYPES.include?(type)

  self.shipper = shipper
  self.consignee = consignee
  self.parcels = parcels
  self.reference_code = reference_code
  self.collection_point = collection_point
  self.type = type
  self.language = language&.to_sym&.downcase || :en
end

Instance Attribute Details

#collection_pointObject

Returns the value of attribute collection_point.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def collection_point
  @collection_point
end

#consigneeObject

Returns the value of attribute consignee.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def consignee
  @consignee
end

#languageObject

Returns the value of attribute language.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def language
  @language
end

#parcelsObject

Returns the value of attribute parcels.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def parcels
  @parcels
end

#reference_codeObject

Returns the value of attribute reference_code.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def reference_code
  @reference_code
end

#shipperObject

Returns the value of attribute shipper.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def shipper
  @shipper
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 12

def type
  @type
end

Instance Method Details

#executeObject



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/deliveries/couriers/ups/shipments/create.rb', line 26

def execute
  request = {
    ShipmentRequest: {
      Request: {
        RequestOption: 'nonvalidate'
      },
      Shipment: {
        Description: Ups.config('default_product.description', default: 'International Goods'),

        Shipper: shipper_address(with_number: true),

        PaymentInformation: {
          ShipmentCharge: {
            Type: '01',
            BillShipper: {
              AccountNumber: 
            }
          }
        },

        ReferenceNumber: {
          Code: 'ON',
          Value: reference_code
        },

        Service: {
          Code: '11',
          Description: 'Standard'
        },

        Package: package_data,

        LabelSpecification: {
          LabelImageFormat: {
            Code: 'GIF'
          }
        }
      }
    }
  }

  if type == :return
    request[:ShipmentRequest][:Shipment][:ReturnService] = { Code: '9' }
    request[:ShipmentRequest][:Shipment][:ShipFrom] = consignee_address
    request[:ShipmentRequest][:Shipment][:ShipTo] = shipper_address
  else # forward
    request[:ShipmentRequest][:Shipment][:ShipFrom] = shipper_address
    request[:ShipmentRequest][:Shipment][:ShipTo] = consignee_address

    if collection_point?
      request[:ShipmentRequest][:Shipment][:ShipmentIndicationType] = {
        Code: '01',
        Description: 'DirectToRetail'
      }
      request[:ShipmentRequest][:Shipment][:AlternateDeliveryAddress] = collection_point_address
      request[:ShipmentRequest][:Shipment][:ShipmentServiceOptions] = {
        Notification: {
          NotificationCode: '012',
          EMail: {
            EMailAddress: consignee.email
          },
          Locale: {
            Language: locale_language,
            Dialect: locale_dialect
          }
        }
      }
    end
  end

  response = call request

  tracking_code = response.dig(:ShipmentResponse, :ShipmentResults, :ShipmentIdentificationNumber)
  labels = [response.dig(:ShipmentResponse, :ShipmentResults, :PackageResults)].flatten.map do |p|
    encoded_gif = p.dig(:ShippingLabel, :GraphicImage)
    image2pdf Base64.decode64(encoded_gif).force_encoding('binary'), height: 4
  end

  {
    tracking_code: tracking_code,
    label: labels.one? ? labels.first : merge_pdfs(labels)
  }
end