##############################################################################
#
# Con-way XML Sample Code for Ruby - Bill of Lading interface
#
# Required: This example uses the Ruby Electric XML (REXML) libraries for processing XML in Ruby.
# You can get REXML at:
# http://www.germane-software.com/software/rexml/
# REXML API documentation is available here:
# http://www.germane-software.com/software/rexml/doc/index.html
# and a good introductory tutorial here:
# http://www.germane-software.com/software/rexml/docs/tutorial.html
# -----------------
# TODO: replace the USERNAME and PASSWORD String values below
# with your Con-way username and password
# -----------------
# Send questions to Con-way XML Support at web.support@Con-way.com
#
##############################################################################
require "net/http"
require "CGI" # to URL Encode XML Request
require "Base64" # to encode authentication string
require "rexml/document" # for processing XML
include REXML # so that we don't have to prefix everything with REXML::
# Replace the USERNAME and PASSWORD string values with your Con-way username and password
username = 'USERNAME'
password = 'PASSWORD'
title = 'Bill of Lading'
requestType = 'BOLrequest'
conwayXmlHost = 'www.Con-way.com'
conwayXmlUri = '/XMLj/X-BOL'
# array of elements you want to query from the XML Response
myElements = ['Shipment', 'Status']
# XML Data setup
# In actual use, you would probably populate the BOL Request parameters
# (Weights, Classes, Zip Codes, etc.) from data submitted via an on-line order form or database.
# For this sample we will just hard code some dummy data.
pronumber = ""
origzip = "97006"
destxip = "33179"
today = Time.now.localtime.strftime("%m/%d/%y")
itemArray = Array.new #Your commodity items, maximum of 4 - add as needed
itemArray.push('qty'=>'44', 'pkgtype'=>'PLT', 'weight'=>'644', 'desc'=>'widget-arms',
'cmdtyClass'=>'775', 'nmfc'=>'', 'hazmat'=>'N')
itemArray.push('qty'=>'66', 'pkgtype'=>'PCS', 'weight'=>'1223', 'desc'=>'cam-shafts',
'cmdtyClass'=>'100', 'nmfc'=>'', 'hazmat'=>'N')
accArray = Array.new # Your accessorial services - add as needed
accArray.push('chgcode' => 'P', 'acccode' => 'GUR')
accArray.push('chgcode' => 'P', 'acccode' => 'DID')
accArray.push('chgcode' => 'P', 'acccode' => 'DST')
today = Time.now.localtime.strftime("%m/%d/%y")
# Build the XML Request
xmlRequest = "" +
"#{username}" +
"P" +
"#{pronumber}" +
"" +
"3338889" +
"3338890" +
"3213A" +
"789283" +
"" +
"" +
"Alan Shipley" +
"1234 NE Main" +
"Portland" +
"OR" +
"#{origzip}" +
"503.450.6055" +
"web.support@Con-way.com" +
"" +
"" +
"883885" +
"503.450.6436" +
"web.support@Con-way.com" +
""
for i in 0...itemArray.size # Add commodity items
xmlRequest << '- ' +
"#{itemArray[i]['qty']}" +
"#{itemArray[i]['weight']}" +
"#{itemArray[i]['desc']}" +
"#{itemArray[i]['cmdtyClass']}" +
"#{itemArray[i]['nmfc']}" +
"#{itemArray[i]['hazmat']}" +
"
"
end
for i in 0...accArray.size # Add accessorials to the XML Request
xmlRequest << "#{accArray[i]['acccode']}"
end
xmlRequest << "TEST TEST TEST" +
"" +
"" +
"#{today}" +
"4:00 pm" +
"7:00 pm" +
"Frank" +
"Franklin Arms" +
"(333)444-4321" +
"" +
""
xmlRequest << ""
# Testing
# xmlDoc = Document.new xmlRequest
# xmlDoc.write($stdout, 2)
# exit
# URL Encode the XML Request
xmlRequest = CGI::escape(xmlRequest)
begin
mySession = Net::HTTP.start(conwayXmlHost)
headers = {}
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['authorization'] = 'Basic ' + Base64.encode64(username + ':' +password)
xmlResponse = mySession.request_post(conwayXmlUri, requestType + '=' + xmlRequest, headers)
rescue => err
puts "Error: #{err}"
exit
end
# Create the REXML XML document
xmlResponseDoc = Document.new xmlResponse.body
# Extract your element values from the XML Response
root = xmlResponseDoc.root
# The outer loop iterates the total number of occurrences of your first element
for i in 0...root.elements.to_a("//#{myElements[0]}").size
# The inner loop iterates all of your elements, for each occurrence of the first
for j in 0...myElements.size
myEls = root.elements.to_a("//#{myElements[j]}")
puts "<#{myElements[j]}> is: " + myEls[i].text
end
puts "\n"
end
# Pretty print the entire XML Response
puts "\n" + "This is the XML Response output for #{title}:"
xmlResponseDoc.write($stdout, 2) # 2nd argument specifies indentation