##############################################################################
#
# Con-way XML Sample Code for Ruby - Service Center Details 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 the postal code below, if don't pass it in as an argument
# -----------------
# 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 = 'Service Center Details'
requestType = 'ServiceCenterDetailsRequest'
conwayXmlHost = 'www.Con-way.com'
conwayXmlUri = '/webapp/servicecenter_app/ServiceCenterDetailsServlet'
# array of elements you want to query from the XML Response
myElements = ['SIC', 'Name']
# - If you don't pass in your postal code as an argument, hard code it here:
if (ARGV[0] && ARGV[0].size > 0)
postalCode = ARGV[0]
else
postalCode = "19348"
end
today = Time.now.localtime.strftime("%m/%d/%y")
xmlRequest = '' +
'' + postalCode + '' +
''
# 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
for i in 0...myElements.size
puts "The value of element <#{myElements[i]}> is: " +
root.elements[myElements[i]].text
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