Class: Dhl::Bcs::V2::Shipment

Inherits:
Object
  • Object
show all
Defined in:
lib/dhl/bcs/v2/shipment.rb

Constant Summary collapse

PRODUCT_PROCEDURE_NUMBERS =
{
  'V01PAK' => '01', # DHL Paket
  'V01PRIO' => '01', # DHL Paket Prio
  'V06PAK' => '06', # DHL Paket Taggleich
  'V53WPAK' => '53', # DHL Paket International
  'V54EPAK' => '54', # DHL Europaket
  'V55PAK' => '55', # DHL Paket Connect
  'V06TG' => '01', # DHL Kurier Taggleich
  'V06WZ' => '01', # DHL Kurier Wunschzeit
  'V86PARCEL' => '86', # DHL Paket Austria
  'V87PARCEL' => '87', # DHL PAKET Connect
  'V82PARCEL' => '82' # DHL PAKET International
}.freeze
PRODUCTS =

DHL PAKET International

PRODUCT_PROCEDURE_NUMBERS.keys.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ Shipment

Returns a new instance of Shipment.



33
34
35
36
37
38
39
40
41
# File 'lib/dhl/bcs/v2/shipment.rb', line 33

def initialize(**attributes)
  assign_attributes(
    {
      product: 'V01PAK',
      shipment_date: Date.today,
      services: []
    }.merge(attributes)
  )
end

Instance Attribute Details

#bank_dataObject

Returns the value of attribute bank_data.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def bank_data
  @bank_data
end

#customer_referenceObject

Returns the value of attribute customer_reference.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def customer_reference
  @customer_reference
end

#export_documentObject

Returns the value of attribute export_document.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def export_document
  @export_document
end

#heightObject

Returns the value of attribute height.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def height
  @height
end

#lengthObject

Returns the value of attribute length.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def length
  @length
end

#notification_emailObject

Returns the value of attribute notification_email.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def notification_email
  @notification_email
end

#productObject

Returns the value of attribute product.



7
8
9
# File 'lib/dhl/bcs/v2/shipment.rb', line 7

def product
  @product
end

#receiverObject

Returns the value of attribute receiver.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def receiver
  @receiver
end

#servicesObject

Returns the value of attribute services.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def services
  @services
end

#shipment_dateObject

Returns the value of attribute shipment_date.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def shipment_date
  @shipment_date
end

#shipperObject

Returns the value of attribute shipper.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def shipper
  @shipper
end

#weightObject

Returns the value of attribute weight.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def weight
  @weight
end

#widthObject

Returns the value of attribute width.



4
5
6
# File 'lib/dhl/bcs/v2/shipment.rb', line 4

def width
  @width
end

Class Method Details

.build(shipper:, receiver:, bank_data: nil, export_document: nil, **shipment_attributes) ⇒ Object

build a shipment from hash data



25
26
27
28
29
30
31
# File 'lib/dhl/bcs/v2/shipment.rb', line 25

def self.build(shipper:, receiver:, bank_data: nil, export_document: nil, **shipment_attributes)
  shipper = Shipper.build(**shipper) if shipper.is_a?(Hash)
  receiver = Receiver.build(**receiver) if receiver.is_a?(Hash)
  bank_data = BankData.build(**bank_data) if bank_data.is_a?(Hash)
  export_document = ExportDocument.build(export_document.delete(:export_doc_positions), **export_document) if export_document.is_a?(Hash)
  new(**{ shipper: shipper, receiver: receiver, bank_data: bank_data, export_document: export_document }.merge(shipment_attributes))
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



43
44
45
46
47
48
# File 'lib/dhl/bcs/v2/shipment.rb', line 43

def assign_attributes(attributes)
  attributes.each do |key, value|
    setter = :"#{key}="
    send(setter, value) if respond_to?(setter)
  end
end

#to_soap_hash(ekp, participation_number) ⇒ Object

Raises:



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
# File 'lib/dhl/bcs/v2/shipment.rb', line 55

def to_soap_hash(ekp, participation_number)
  raise Dhl::Bcs::Error, 'Packing weight in kilo must be set!' unless weight
  raise Dhl::Bcs::Error, 'Sender address must be set!' unless shipper
  raise Dhl::Bcs::Error, 'Receiver address must be set!' unless receiver
  raise Dhl::Bcs::Error, 'Product must be set!' unless product
  raise Dhl::Bcs::Error, 'In order to do an international shipment --product:V53WPAK--, :export_document muse be set!' unless !((product == 'V53WPAK') ^ export_document  )

   = "#{ekp}#{PRODUCT_PROCEDURE_NUMBERS[product]}#{participation_number}"
  raise Dhl::Bcs::Error, 'Need a 14 character long account number. Check EKP and participation_number' if .size != 14
  {
    'ShipmentDetails' => {}.tap { |h|
      h['product'] = product
      h['shipmentDate'] = shipment_date.strftime("%Y-%m-%d")
      h['cis:accountNumber'] = 
      h['customerReference'] = customer_reference if !customer_reference.blank?

      # just one ShipmentItem possible
      h['ShipmentItem'] = { 'weightInKG' => weight }.tap { |si|
        si['lengthInCM'] = length if length
        si['widthInCM'] = width if width
        si['heightInCM'] = height if height
      }
      h['Service'] = services.map(&:to_soap_hash) unless services.empty?
      h['Notification'] = { 'recipientEmailAddress' => notification_email } if notification_email
      h['BankData'] = bank_data.to_soap_hash if bank_data
    },
    # Shipper information
    'Shipper' => shipper.to_soap_hash,
    # Receiver information
    'Receiver' => receiver.to_soap_hash
  }.tap{|h|
      #ExportDocuemnt information
    h['ExportDocument'] = export_document.to_soap_hash if export_document
  }
end