class DhlExpressGlobal::Request::Base

Constants

PAYMENT_INFO_CODES

List of Payment Info codes

PRODUCTION_URL

DHL Express Global Production URL

TEST_URL

DHL Express Global Test URL

Attributes

debug[RW]

Public Class Methods

new(credentials, options = {}) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 29
def initialize(credentials, options = {})
  requires!(options, :shipper, :recipient, :packages)
  @credentials = credentials
  @shipper, @recipient, @packages, @service_type, @debug = options[:shipper], options[:recipient], options[:packages], options[:service_type], options[:debug]
  @debug = ENV['DEBUG'] == 'true'
  @shipping_options = options[:shipping_options] ||= {}
  @payment_options = options[:payment_options] ||= {}
end

Public Instance Methods

add_address_street_lines(xml, address) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 121
def add_address_street_lines(xml, address)
  Array(address).take(3).each_with_index do |address_line, i|
    case i
    when 0
      xml.StreetLines address_line
    when 1
      xml.StreetLines2 address_line
    when 2
      xml.streetLines3 address_line
    end
  end
end
add_recipient(xml) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 104
def add_recipient(xml)
  xml.Recipient {
    xml.Contact {
      xml.PersonName @recipient[:name]
      xml.CompanyName @recipient[:company]
      xml.PhoneNumber @recipient[:phone_number]
    }
    xml.Address {
      add_address_street_lines(xml, @recipient[:address])
      xml.City @recipient[:city]
      xml.PostalCode @recipient[:postal_code]
      xml.StateOrProvinceCode @recipient[:state] if @recipient[:state]
      xml.CountryCode @recipient[:country_code]
    }
  }
end
add_requested_packages(xml) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 134
def add_requested_packages(xml)
  @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]
      }
    }
  end
  xml.ShipTimestamp (Time.now + 10*60).strftime("%Y-%m-%dT%H:%M:%SGMT%:z")
  xml.UnitOfMeasurement @packages.first[:weight][:units] == 'KG' ? 'SI' : 'SU'
  xml.Content @shipping_options[:package_contents] ||= "NON_DOCUMENTS"
end
add_shipment_info(xml) click to toggle source

Add information for shipments

# File lib/dhl_express_global/request/base.rb, line 151
def add_shipment_info(xml)
  xml.ShipmentInfo {
    xml.DropOffType @shipping_options[:drop_off_type] ||= "REGULAR_PICKUP"
    xml.ServiceType @shipping_options[:service_type]
    xml.RequestValueAddedServices @shipping_options[:request_value_added_services] ||= "N"
    xml.NextBusinessDay @shipping_options[:next_business_day] ||= "N"
  }
end
add_shipper(xml) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 87
def add_shipper(xml)
  xml.Shipper {
    xml.Contact {
      xml.PersonName @shipper[:name]
      xml.CompanyName @shipper[:company]
      xml.PhoneNumber @shipper[:phone_number]
    }
    xml.Address {
      add_address_street_lines(xml, @shipper[:address])
      xml.City @shipper[:city]
      xml.PostalCode @shipper[:postal_code]
      xml.StateOrProvinceCode @shipper[:state] if @shipper[:state]
      xml.CountryCode @shipper[:country_code]
    }
  }
end
add_ws_authentication_header(xml) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 74
def add_ws_authentication_header(xml)
  xml[:soapenv].Header {
    xml[:wsse].Security('soapenv:mustUnderstand' => "1"  , 
                        'xmlns:wsse' => 'http://schemas.xmlsoap.org/ws/2003/06/secext', 
                        'xmlns:wsu' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd') {
      xml[:wsse].UsernameToken('wsu:Id' => 'UsernameToken') {
        xml[:wsse].Username @credentials.username
        xml[:wsse].Password(@credentials.password, 'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')
      }
    }
  }
end
api_action() click to toggle source
# File lib/dhl_express_global/request/base.rb, line 50
def api_action
  # default action, overwrite in subclass
  "expressRateBook"
end
api_url() click to toggle source
# File lib/dhl_express_global/request/base.rb, line 42
def api_url
  if @credentials.mode == 'production'
    "#{PRODUCTION_URL}#{api_action}"
  else
    "#{TEST_URL}#{api_action}?WSDL"
  end
end
build_xml() click to toggle source
# File lib/dhl_express_global/request/base.rb, line 55
def build_xml
  raise NotImplementedError, 'Override #build_xml in subclass'
end
headers() click to toggle source
# File lib/dhl_express_global/request/base.rb, line 160
def headers
  {"Content-Type"=>"text/xml; charset=utf-8"}
end
parse_response(response) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 59
def parse_response(response)
  response = Hash.from_xml( response.parsed_response.gsub("\n", "") ) if response.parsed_response.is_a? String
  response = sanitize_response_keys(response)
end
process_request() click to toggle source
# File lib/dhl_express_global/request/base.rb, line 38
def process_request
  raise NotImplementedError, 'Override #process_request in subclass'
end
sanitize_response_keys(response) click to toggle source
# File lib/dhl_express_global/request/base.rb, line 64
def sanitize_response_keys(response)
  if response.is_a? Hash
    response.inject({}) { |result, (key, value)| result[underscorize(key).to_sym] = sanitize_response_keys(value); result }
  elsif response.is_a? Array
    response.collect { |result| sanitize_response_keys(result) }
  else
    response
  end
end