##############################################################################
#
# Con-way XML Sample Code for Ruby - Rating 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
# * to get customer-specific discounts, replace CUSTNMBR String value
# below with your Con-way customer number
#
# -----------------
# 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'
# to get a customer-specific discount, replace the CUSTNMBR string below with
# your Con-way customer number, then uncomment the line below.
custNmbr = 'CUSTNMBR';
title = 'Rating'
requestType = 'RateRequest'
conwayXmlHost = 'www.Con-way.com'
conwayXmlUri = '/XMLj/X-Rate'
# array of elements you want to query from the XML Response
myElements = ['TotalCharge', 'Discount', 'NetCharge', 'EffectiveDate' ]
# XML Data setup
# In actual use, you would probably populate the Rating 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.
origZip = "97006"
destZip = "33179"
today = Time.now.localtime.strftime("%m/%d/%y")
itemArray = Array.new #Your commodity items, maximum of 4 - add as needed
itemArray.push('class'=>'775', 'weight'=>'667')
itemArray.push('class'=>'100', 'weight'=>'555')
accArray = Array.new # Your accessorial services - add as needed
accArray.push("SSC")
accArray.push("DNC")
accArray.push("GUR")
today = Time.now.localtime.strftime("%m/%d/%y")
xmlRequest = '' +
'' + origZip + '' +
'' + destZip + ''
# To get customer specific discount, uncomment this line:
# xmlRequest << '' + custNmbr + '';
xmlRequest << 'P' +
'100' +
"#{today}"
# Add commodity items
for i in 0...itemArray.size
xmlRequest << '- ' +
'' + itemArray[i]['class'] + '' +
'' + itemArray[i]['weight'] + '' +
'
'
end
# Add accessorials
for i in 0...accArray.size # Add accessorials to the XML Request
xmlRequest << "#{accArray[i]}"
end
xmlRequest << ""
# 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