Ruby on Rails
“Ruby on Rails” often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language. If you don’t know what that means, you probably don’t need this page.
There are many shopping carts that have been written in Ruby utilizing the Rails framework. If you are looking for a cart that uses Rails and works well with Shipwire please see our Shopify Order Fulfillment page.
Doug over at Host17 Internet Services was kind enough to look over the Shipwire Fulfillment Web Services API‘s and put together some Rails calls to use the Shipwire API.
This code has NOT been certified or tested by Shipwire.
From Doug, “I am providing sample code for a RoR interface to the Shipwire API. The code is copyrighted (C) but freely distributable. There are no warranties either express or implied. It is hoped that the code will assist others in interfacing to the Shipwire API with RoR. The sample code consists of 2 parts, (1) a sample controller method; and, (2) a sample template.”
click here to download the sample code.
Sample Controller Method
# Modify the following method by substituting appropriate values for
# 'your_email_address' and 'your_password'. Then insert the modified
# method into an appropriate controller class definition. Make
# sure that you have added appropriate SKUs to your Shipwire test
# account.
# Although copyrighted (C), this code is freely distributable.
def shipwire()
require ‘net/https’
require ‘rexml/document’
# If we don’t have a params hash, create it.
if defined? params==nil
params=Hash.new(nil)
end
# – - – - – - – Place your configuration here – - – - – - -
productsArray=[\
{'Code'=>'1000','Description'=>'Programming Ruby'},\
{'Code'=>'2000','Description'=>'Agile Web Development with Rails'},\
{'Code'=>'3000','Description'=>'RailsSpace'}]
# If the params hash is empty, this must be a test. Therefore,
# add some default purchases.
if params.length==0
params['Qty_0']=’1′
params['Qty_1']=’2′
params['Qty_2']=’3′
end
# – - – - – - – - – - – End Configuration – - – - – - – - -
out_hash=Hash.new(0)
# In an actual production environment, it is likely that the entire
# orderList assignment below will be replaced with a feed from a
# database or something similar. However, the following references
# sensible key names for the params hash so that it may actually be
# employed in a production environment using an order form; provided,
# that some limitations are observed such as submitting only a single
# order at a time. The following also associates sensible default
# values with the keys of the params hash so that your action may
# be tested without having to submit any actual key value pairs.
orderList=<<_ALTO
your_email_address
your+password
Test
023YAHOO
#{params['Order_Warehouse'] || ’00′}
#{params['FullName'] || ‘Sheridan Rawlins’}
#{params['Address1'] || ’321 Foobar Lane’}
#{params['Address2'] || ”}
#{params['City'] || ‘Our Town’}
#{params['State'] || ‘CA’}
#{params['Country'] || ‘US’}
#{params['Zip'] || ’12345′}
#{params['Phone'] || ’555.444.3210′}
#{params['Email'] || ‘sheridan@rawlins.com’}
#{params['Shipping'] || ‘UPS Ground’}
_ALTO
params.each do
|key,value|
if key.match(/^Qty_/)
idx=key.gsub(‘Qty_’,”).to_i
orderList=orderList+\
“- \n\
#{productsArray[idx]['Code']}\n \
#{params[value]} \n \
#{productsArray[idx]['Description']} \n \
\n”
end
end
orderList=orderList+<<_ALTO
_ALTO
url=URI.parse(‘https://www.shipwire.com/exec/FulfillmentServices.php’)
req = Net::HTTP::Post.new(url.path)
req.basic_auth(‘doug@host17.net’,'wedontcare’)
req.set_form_data({‘OrderListXML’=>orderList},’;')
http=Net::HTTP.new(url.host,443)
http.use_ssl=(true)
res=http.start {|http| http.request(req)}
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end
@xml_response=res.body
doc=REXML::Document.new(res.body)
@shippingConfirmHash=Hash.new()
@shippingConfirmHash['status']=doc.elements.[](‘SubmitOrderResponse/Status’).text if defined? doc.elements.[](‘SubmitOrderResponse/Status’).text!=nil
@shippingConfirmHash['TotalOrders']=doc.elements.[](‘SubmitOrderResponse/TotalOrders’).text if defined? doc.elements.[](‘SubmitOrderResponse/TotalOrders’).text!=nil
@shippingConfirmHash['TotalItems']=doc.elements.[](‘SubmitOrderResponse/TotalItems’).text if defined? doc.elements.[](‘SubmitOrderResponse/TotalItems’).text!=nil
@shippingConfirmHash['TransactionId']=doc.elements.[](‘SubmitOrderResponse/TransactionId’).text if defined? doc.elements.[](‘SubmitOrderResponse/TransactionId’).text!=nil
doc.elements.each(‘SubmitOrderResponse/OrderInformation/Order’) {|element| @shippingConfirmHash['OrderInformation_number']=element.attributes['number']}
doc.elements.each(‘SubmitorderResponse/OrderInformation/Order’) {|element| @shippingConfirmHash['OrderInformation_id']=element.attributes['id']}
doc.elements.each(‘SubmitOrderResponse/OrderInformation/Order’) {|element| @shippingConfirmHash['OrderInformation_status']=element.attributes['status']}
doc.elements.each(‘SubmitOrderResponse/OrderInformation/Order/Exception’) {|element| @shippingConfirmHash['OrderInformation_Order_Exception']=element}
end
Sample Template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Although copyrighted (C), this code is freely distributable -->
<html>
<head><title>Shipwire Fulfillment</title></head>
<body>
<p style="font-weight: bold;">
Raw Hash Data:<br />
</p>
<p>
<%= @shippingConfirmHash %>
</p>
<p>
<span style="font-weight:bold;">
Tabular Data:
</span>
<table>
<% data_out=String.new('')
@shippingConfirmHash.each {|key,value| data_out=data_out+"<tr><td>#{key}</td><td>#{value}</td></tr>\n"} %>
<%= data_out %>
</table>
</p>
<p>
<span style="font-weight:bold;">
Raw XML:
</span>
<pre>
<%= @xml_response.gsub!('<','<').gsub!('>','>') %>
</pre>
</p>
</body>
</html>