class DhlExpressGlobal::Request::Shipment

Public Class Methods

new(credentials, options={}) click to toggle source
Calls superclass method DhlExpressGlobal::Request::Base::new
# File lib/dhl_express_global/request/shipment.rb, line 7
def initialize(credentials, options={})
  super
  requires!(options, :service_type, :payment_info, :international_detail)
  @international_detail = options[:international_detail]
  requires!(@international_detail, :commodities)
  @commodities = @international_detail[:commodities]
  requires!(@commodities, :description, :customs_value)
  @payment_info = options[:payment_info]
  @label_specification = {
    :image_type => 'PDF',
    :label_template => 'ECOM26_84_001'
  }

  @label_specification.merge! options[:label_specification] if options[:label_specification]
end

Public Instance Methods

api_response() click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 33
def api_response
  @response ||= self.class.post(URI(api_url),
    body:    build_xml,
    headers: headers,
    verify: true
  )
end
process_request() click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 23
def process_request
  puts api_response if @debug
  response = parse_response(api_response)
  if success?(response)
    success_response(response)
  else
    failure_response(response)
  end
end

Private Instance Methods

add_international_detail(xml) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 79
def add_international_detail(xml)
  xml.InternationalDetail {
    xml.Commodities {
      xml.NumberOfPieces @commodities[:number_of_pieces] if @commodities[:number_of_pieces]
      xml.Description @commodities[:description]
      xml.CountryOfManufacture @commodities[:country_of_manufacture] if @commodities[:country_of_manufacture]
      xml.Quantity @commodities[:quantity] if @commodities[:quantity]
      xml.UnitPrice @commodities[:unit_price] if @commodities[:unit_price]
      xml.CustomsValue @commodities[:customs_value]
    }
    xml.Content @international_detail[:content] if @international_detail[:content]
  }
end
add_requested_packages(xml) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 93
def add_requested_packages(xml)
  xml.Packages {
    @packages.each_with_index do |package, i|
      xml.RequestedPackages('number' => i + 1) {
        xml.Weight package[:weight][:value]
        xml.Dimensions {
          xml.Length package[:dimensions][:length]
          xml.Width package[:dimensions][:width]
          xml.Height package[:dimensions][:height]
        }
        xml.CustomerReferences @shipping_options[:customer_references] ||= "#{rand(10**10)}"
      }
    end
  }
end
add_shipment_info(xml) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 69
def add_shipment_info(xml)
  xml.ShipmentInfo {
    xml.DropOffType @shipping_options[:drop_off_type] ||= "REGULAR_PICKUP"
    xml.ServiceType @service_type
    xml.Account @credentials.account_number
    xml.Currency @shipping_options[:currency].upcase
    xml.UnitOfMeasurement @shipping_options[:unit_of_measurement]
  }
end
build_xml() click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 43
def build_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml[:soapenv].Envelope( 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 
                            'xmlns:ship' => "http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/ShipmentMsgRequest") {
      add_ws_authentication_header(xml)
      xml[:soapenv].Body {
        xml[:ship].ShipmentRequest {
          xml.RequestedShipment {
            xml.parent.namespace = nil
            add_shipment_info(xml)
            xml.ShipTimestamp @shipping_options[:ship_timestamp] ||= (Time.now + 10*60).strftime("%Y-%m-%dT%H:%M:%SGMT%:z")
            xml.PaymentInfo @payment_info
            add_international_detail(xml)
            xml.Ship {
              add_shipper(xml)
              add_recipient(xml)
            }
            add_requested_packages(xml)
          }
        }
      }
    }
  end
  builder.doc.root.to_xml
end
error_response(response) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 123
def error_response(response)
  response[:envelope][:body][:shipment_response][:notification][:message]
rescue
  response[:envelope][:body][:shipment_response][:notification].first[:message]
end
failure_response(response) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 110
def failure_response(response)
  error_message = error_response(response)
  raise RateError, error_message
end
headers() click to toggle source
Calls superclass method DhlExpressGlobal::Request::Base#headers
# File lib/dhl_express_global/request/shipment.rb, line 129
def headers
  super.merge!("SOAPAction" => "euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_createShipmentRequest")
end
success?(response) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 119
def success?(response)
  response[:envelope][:body][:shipment_response] && !error_response(response)
end
success_response(response) click to toggle source
# File lib/dhl_express_global/request/shipment.rb, line 115
def success_response(response)
  @response_details = response[:envelope][:body][:shipment_response]
end