############################################################################## # # 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 = "<BOLrequest testmode=\"Y\">" + "<RequesterUserId shipcode=\"S\">#{username}</RequesterUserId>" + "<ChargeCode>P</ChargeCode>" + "<PRONmbr>#{pronumber}</PRONmbr>" + "<CustRefNmbrs>" + "<PurchaseOrderNmbr>3338889</PurchaseOrderNmbr>" + "<PurchaseOrderNmbr>3338890</PurchaseOrderNmbr>" + "<OtherRefNmbr refcode=\"SKU\" refdesc=\"SKU Number\">3213A</OtherRefNmbr>" + "<OtherRefNmbr refcode=\"UPC\" refdesc=\"UPC number\">789283</OtherRefNmbr>" + "</CustRefNmbrs>" + "<Shipper>" + "<ShipperName>Alan Shipley</ShipperName>" + "<ShipperAddr>1234 NE Main</ShipperAddr>" + "<ShipperCity>Portland</ShipperCity>" + "<ShipperState>OR</ShipperState>" + "<ShipperZip country=\"US\">#{origzip}</ShipperZip>" + "<ShipperPhone extension=\"6055\">503.450.6055</ShipperPhone>" + "<ShipperEmail>web.support@Con-way.com</ShipperEmail>" + "</Shipper>" + "<Consignee>" + "<ConsigneeCustNmbr>883885</ConsigneeCustNmbr>" + "<ConsigneePhone extension=\"6666\">503.450.6436</ConsigneePhone>" + "<ConsigneeEmail>web.support@Con-way.com</ConsigneeEmail>" + "</Consignee>" for i in 0...itemArray.size # Add commodity items xmlRequest << '<Item>' + "<Quantity pkgtype=\"#{itemArray[i]['pkgtype']}\">#{itemArray[i]['qty']}</Quantity>" + "<Weight unit=\"lbs\">#{itemArray[i]['weight']}</Weight>" + "<Description>#{itemArray[i]['desc']}</Description>" + "<CmdtyClass>#{itemArray[i]['cmdtyClass']}</CmdtyClass>" + "<NMFClass>#{itemArray[i]['nmfc']}</NMFClass>" + "<HazMat>#{itemArray[i]['hazmat']}</HazMat>" + "</Item>" end for i in 0...accArray.size # Add accessorials to the XML Request xmlRequest << "<Accessorial chargecode=\"#{accArray[i]['chgcode']}\">#{accArray[i]['acccode']}</Accessorial>" end xmlRequest << "<ShippingRemarks>TEST TEST TEST</ShippingRemarks>" + "<EmergencyContact></EmergencyContact>" + "<PickupRequest>" + "<PickupDate>#{today}</PickupDate>" + "<PickupReadyTime>4:00 pm</PickupReadyTime>" + "<DockCloseTime>7:00 pm</DockCloseTime>" + "<ContactName>Frank</ContactName>" + "<ContactCompany>Franklin Arms</ContactCompany>" + "<ContactPhone>(333)444-4321</ContactPhone>" + "</PickupRequest>" + "<SendBOLemail/>" xmlRequest << "</BOLrequest>" # 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