Class: Dhl::Intraship::API

Inherits:
Object
  • Object
show all
Defined in:
lib/dhl-intraship/api.rb

Constant Summary collapse

DEFAULT_NAMESPACES =
{
"xmlns:soapenv" => "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:de" => "http://de.ws.intraship",
"xmlns:is" => "http://dhl.de/webservice/is_base_de",
"xmlns:cis" => "http://dhl.de/webservice/cisbase"}
INTRASHIP_WSDL =
"http://www.intraship.de/ws/1_0/ISService/DE.wsdl"
INTRASHIP_ENDPOINT =
"http://www.intraship.de/ws/1_0/de/ISService"
INTRASHIP_TEST_WSDL =
"http://test-intraship.dhl.com/ws/1_0/ISService/DE.wsdl"
INTRASHIP_TEST_ENDPOINT =
"http://test-intraship.dhl.com/ws/1_0/de/ISService"

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ API

Returns a new instance of API.



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
# File 'lib/dhl-intraship/api.rb', line 18

def initialize(config, options = {})
  raise "User must be specified" if config[:user].nil?
  raise "Signature (password) must be specified" if config[:signature].nil?
  raise "EKP (first part of the DHL account number) must be specified" if config[:ekp].nil?

  if options[:test]
    wsdl_url = INTRASHIP_TEST_WSDL
    endpoint = INTRASHIP_TEST_ENDPOINT
  else
    wsdl_url = INTRASHIP_WSDL
    endpoint = INTRASHIP_ENDPOINT
  end

  @user = config[:user]
  @signature = config[:signature]
  @ekp = config[:ekp]
  @procedure_id = config[:procedure_id] || '01'
  @partner_id = config[:partner_id] || '01'

  @options = options
  @client = ::Savon::Client.new do
    wsdl.document = wsdl_url
    wsdl.endpoint = endpoint
  end
end

Instance Method Details

#bookPickup(booking_information, pickup_address, contact_orderer = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dhl-intraship/api.rb', line 121

def bookPickup(booking_information, pickup_address, contact_orderer = nil)
  warn "DHL does not yet support the book pickup call"

  raise "Booking information must be of type BookingInformation! Is #{booking_information}" unless booking_information.kind_of? BookingInformation
  raise "Pickup_address must be of type Address! Is #{pickup_address.class}" unless pickup_address.kind_of? Address
  raise "Contact orderer must be of type Address! Is #{contact_orderer.class}" unless contact_orderer.nil? or contact_orderer.kind_of? Address

  if booking_information..nil? and [:DDI, :DDN].includes?(booking_information.product_id)
    booking_information. = @ekp
  end
  if booking_information.attendance.nil?
    booking_information.attendance = @partner_id
  end

  begin
    result = @client.request "de:BookPickupRequest" do
      soap.xml do |xml|
        xml.soapenv(:Envelope, DEFAULT_NAMESPACES) do |xml|
          xml.soapenv(:Header) do |xml|
            append_default_header_to_xml(xml)
          end
          xml.soapenv(:Body) do |xml|
            xml.de(:"BookPickupRequest") do |xml|
              add_version_information(xml)
              booking_information.append_to_xml(xml)
              xml.PickupAddress do |xml|
                pickup_address.append_to_xml(xml)
              end
              xml.ContactOrderer do |xml|
                contact_orderer.append_to_xml(xml)
              end unless contact_orderer.nil?
            end
          end
        end
      end
    end
    r = result.to_hash[:book_pickup_response]

    raise "Intraship call failed with code #{r[:status][:status_code]}: #{r[:status][:status_message]}" unless r[:status][:status_code] == '0'

    r[:confirmation_number]
  rescue Savon::Error => error
    raise error
  end
end

#createShipmentDD(shipments) ⇒ Object



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
# File 'lib/dhl-intraship/api.rb', line 44

def createShipmentDD(shipments)
  begin
    shipments = [shipments] unless shipments.respond_to?('each')

    # For some reason the class instance variables are not accessible inside of the request block
    ekp = @ekp
    partner_id = @partner_id

    returnXML = @config && @config[:label_response_type] && @config[:label_response_type] == :xml;
    result = @client.request "de:CreateShipmentDDRequest" do
      soap.xml do |xml|
        xml.soapenv(:Envelope, DEFAULT_NAMESPACES) do |xml|
          xml.soapenv(:Header) do |xml|
            append_default_header_to_xml(xml)
          end
          xml.soapenv(:Body) do |xml|
            xml.de(:"CreateShipmentDDRequest") do |xml|
              add_version_information(xml)
              xml.ShipmentOrder do |xml|
                xml.SequenceNumber('1')
                shipments.each do |shipment|
                  shipment.append_to_xml(ekp, partner_id, xml)
                  xml.LabelResponseType('XML') if returnXML
                end
              end
            end
          end
        end
      end
    end
    r = result.to_hash[:create_shipment_response]
    if r[:status][:status_code] == '0'
      shipment_number = r[:creation_state][:shipment_number][:shipment_number]

      if returnXML
        xml_label = r[:creation_state][:xmllabel]
        {shipment_number: shipment_number, xml_label: xml_label}
      else
        label_url = r[:creation_state][:labelurl]
        {shipment_number: shipment_number, label_url: label_url}
      end

    else
      raise "Intraship call failed with code #{r[:status][:status_code]}: #{r[:status][:status_message]} (Status messages: #{r[:creation_state][:status_message].to_s})"
    end
  rescue Savon::Error => error
    raise error
  end
end

#deleteShipmentDD(shipment_number) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dhl-intraship/api.rb', line 94

def deleteShipmentDD(shipment_number)
  begin
    result = do_simple_shipment_number_only_request('DeleteShipmentDDRequest', shipment_number)
    r = result.to_hash[:delete_shipment_response]

    # Return true if successful
    raise "Intraship call failed with code #{r[:status][:status_code]}: #{r[:status][:status_message]} (Status messages: #{r[:deletion_state][:status].to_s})" unless r[:status][:status_code] == '0'

    true
  rescue Savon::Error => error
    raise error
  end
end

#doManifestDD(shipment_number) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dhl-intraship/api.rb', line 108

def doManifestDD(shipment_number)
  begin
    result = do_simple_shipment_number_only_request('DoManifestDDRequest', shipment_number)
    r = result.to_hash[:do_manifest_response]

    raise "Intraship call failed with code #{r[:status][:status_code]}: #{r[:status][:status_message]} (Status messages: #{r[:manifest_state][:status].to_s})" unless r[:status][:status_code] == '0'

    true
  rescue Savon::Error => error
    raise error
  end
end