##############################################################################
#
# Con-way XML Sample Code for Ruby - Shipment Status Tracking 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
# Also add tracking number(s) below, if don't pass in as arguments
# -----------------
# 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 = 'Shipment Status Tracking'
requestType = 'ShipmentStatusRequest'
conwayXmlHost = 'www.Con-way.com'
conwayXmlUri = '/XMLj/X-ShipmentStatus'
# array of elements you want to query from the XML Response
myElements = ['PRONmbr', 'StatusMessage', ]
# - If you don't pass in your tracking numbers as arguments, hard code here:
tracknums = Array.new
if (ARGV[0] && ARGV[0].size > 0)
tracknums = ARGV
else
tracknums.push("292200204") # add more as needed
end
today = Time.now.localtime.strftime("%m/%d/%y")
xmlRequest = ''
for i in 0...tracknums.size
xmlRequest += "#{tracknums[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 "Occurrence #{i+1} of <#{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