############################################################################## # # Con-way XML Sample Code for Ruby - Image Retrieval 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 PRO number(s) below, if you don't pass them in as arguments # ----------------- # Send questions to Con-way XML Support at xmlsupport@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' # TODO: replace the 'c:/imgs/' directory location with your preferred location to save image files # On Windows, like this: imgDir = 'C:/Program Files/Apache Group/Apache2/htdocs/conwayImages' # or on Mac, like this: # imgDir = '/Library/WebServer/Documents/conwayImages/' requestType = 'ImageRequest' conwayXmlHost = 'www.Con-way.com' conwayXmlUri = '/webapp/imaging_app/XmlImageRetrieverServlet' # XML Data setup # In actual use, you would probably populate the Image Retrieval Request parameters # from data submitted via an on-line form or database. # For this sample we will just hard code some dummy data. reqArray = Array.new # TODO: Repace PRONUMBER with your own 9 digit PRO Numbers # Type may be BL, DR, LOA, WI. Format may be pdf, jpeg, png, tiff reqArray.push('pro'=>'PRONUMBER', 'type'=>'BL', 'format'=>'pdf') reqArray.push('pro'=>'PRONUMBER', 'type'=>'BL', 'format'=>'tiff') xmlRequest = "<#{requestType}>\n" for i in 0...reqArray.size xmlRequest << "<ImageKey>\n" + ' <PRONumber>' + reqArray[i]['pro'] + "</PRONumber>\n" + ' <ImageType>' + reqArray[i]['type'] + "</ImageType>\n" + ' <ImageFormat>' + reqArray[i]['format'] + "</ImageFormat>\n" + "</ImageKey>\n" end xmlRequest << "</#{requestType}>" # puts xmlRequest # Testing - uncomment to display XML Request document xmlRequest = CGI::escape(xmlRequest) # URL Encode the XML Request 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 xmlResponseDoc = Document.new xmlResponse.body # Create the REXML XML document root = xmlResponseDoc.root # process all <Image> elements in the XML Response document, get the image(s) and save to disk # create unique number from current time for use in filenames nowString = Time.now.localtime.strftime("%Y%m%d_%H%M%S") root.elements.each('Image') do |imgEl| pro = imgEl.elements.to_a('PRONumber')[0].text # PRO number type = imgEl.elements.to_a('ImageType')[0].text.upcase # Document type format = imgEl.elements.to_a('ImageFormat')[0].text.upcase # Image format # Check for errors: if found, report. if imgEl.elements.to_a('Error')[0] # Error element error = imgEl.elements.to_a('Error')[0].text puts "Doctype: #{type}, PRO: #{pro}, Error: #{error}\n" # If no errors, process image data else imgDataEls = imgEl.elements.to_a('ImageData') # Array of binary images for i in 0...imgDataEls.size # set up page string for filename if multiple pages if imgDataEls.size > 1 page = "_#{i+1}" else page = '' end filename = File.join(imgDir, "#{type}-#{pro}-#{nowString}#{page}.#{format}") File.open(filename, "wb") do |file| # Open file for binary write file << Base64.decode64(imgDataEls[i].text) puts "Doctype: #{type}, PRO: #{pro}, Result: image written to #{filename}.\n" end end end end # Testing: uncomment to pretty print the entire XML Response # puts "\n" + "This is the XML Response output for #{title}:" # xmlResponseDoc.write($stdout, 2) # 2nd argument specifies indentation