Ruby on Rails and Web Services

Service Objects provides alternative connection methods for applications that don't support the SOAP protocol

Building upon the success of Web service interoperability with enterprise and client infrastructures, Ruby on Rails (RoR) has taken steps forward to optimize speed and simplicity in consuming Web services. In fact, Rails has become the de-facto standard for rapid Web application development.

With the release of Rails 2.0, Rails made the transition to support REST- based services rather than the more traditional SOAP-based Web services This change was brought about by the removal of ActionWebService from the default installation package and was replaced by ActiveResource. ActiveResource was built as an extension of ActiveModel to easily handle mapping of REST-based services.

Although Ruby on Rails has undergone many improvements since its initial release in 2005, its transition from supporting SOAP to REST-based Web services has created issues for enterprise clients that continue to utilize SOAP in their services.

In order to provide continued support for SOAP-based services, developers from the Ruby community have provided their own solutions to supplement the Ruby library. Ruby gems, such as soap4r, savon and handsoap, are but a few of the libraries developed to support SOAP-based services. However, some of these libraries have compatibility issues with the newest versions of Ruby 1.9.2 and Rails 2.3.8 (and above) when communicating with .NET services.

Alternative connection methods

As Ruby on Rails versions progress, the complexity of maintaining compatibility between Rails and SOAP libraries with other frameworks such as Java and .NET will only increase. At Service Objects we strive to maintain flexibility within our Web services by offering multiple connection methods to provide the best possible solutions for our customers’ requests. While most clients consume our services using SOAP protocol, we also provide connection methods for applications which do not support this protocol.

Recently, we received a request from a client who was experiencing issues using the latest versions of Ruby and Rails while using the soap4r library. In order to avert complications using soap4r with the newer Rails technology, we opted to develop a sample application which consumes our Address Validation US service using a simple HTTP GET request which doesn’t rely on a third-party SOAP library.

Here is an excerpt from the GET request sample code implementation:

To download the complete sample application, click here.
# Load required libraries at head of document
require 'net/http'
require 'uri'

# Using get request to call Address Validation US Service using variables from form submit
url = 'http://trial.serviceobjects.com/av/AddressValidate.asmx/ValidateAddressWithDPV?Address=' + @address + '&Address2=' + @address2 + '&City=' + @city + '&State=' + @state + '&PostalCode=' + @zip + '&LicenseKey=' + @key

resp = Net::HTTP.get_response(URI.parse(url))
output = resp.body
data = Hash.from_xml(output)
result = data['DPVAddress']
Making a transition from a SOAP request to a GET request is just a few more lines of code, but overall is easy to implement.

Follow these three steps:

  1. Include the required libraries at the head of the document. In this case you will need ‘net/http’ and ‘uri’ .
  2. Build the query string using supplied parameters.
  3. Finally, you will need to parse the result by retrieving the data from the body of the response object and converting into a convenient-to-use object using the Hash.from_xml() method.