SOAP Web Services in Ruby: A Guide to Savon and Handsoap Libraries

Introduction

SOAP (Simple Object Access Protocol) is an XML-based messaging protocol used for exchanging structured information between web services. Although RESTful APIs have gained popularity in recent years, SOAP remains relevant for many legacy systems and specific use cases. In this article, we will explore two popular Ruby libraries for working with SOAP web services: Savon and Handsoap.

Savon: A Flexible SOAP Client Library

Getting Started with Savon

Savon is a popular and widely-used Ruby library for SOAP web services. It provides an easy-to-use interface for interacting with SOAP services and supports various SOAP versions, including 1.1 and 1.2. To start using Savon, simply install the gem:

gem install savon

After the installation, you can create a new Savon client by providing the WSDL (Web Services Description Language) URL:

require ‘savon’

client = Savon.client(wsdl: ‘http://example.com/service.wsdl’)

Making SOAP Requests with Savon

Savon allows you to make SOAP requests easily. First, you need to determine the available operations for the SOAP service using the operations method:

operations = client.operations

# => [:create_user, :delete_user, :update_user, :get_user]

Once you have the list of operations, you can call a specific operation by using the call method:

response = client.call(:create_user, message: { name: ‘John Doe’, email: ‘john.doe@example.com’ })

Processing SOAP Responses with Savon and Handsoap

Parsing Responses with Savon

The call method returns a Savon::Response object that contains the SOAP response. You can access the response body and parsed data using the body and to_hash methods, respectively:

response_body = response.body

# => { create_user_response: { user_id: 123 } }

response_hash = response.to_hash

# => { create_user_response: { user_id: 123 } }

Extracting Data from Responses with Handsoap

Handsoap provides several helper methods for parsing SOAP responses. The get_inner_text method, for instance, can be used to extract the text content from an XML element:

class UserService < Handsoap::Service

# …

def create_user(name, email)

response = invoke(‘CreateUser’) do |message|

message.add ‘Name’, name

message.add ‘Email’, email

end

return parse_create_user_response(response)

end

private

def parse_create_user_response(response)

user_id = response.get_inner_text(‘//user_id’)

{ user_id: user_id.to_i }

end

end

With the create_user method and response parsing in place, you can now interact with the SOAP service using your custom UserService class:

user_service = UserService.new

result = user_service.create_user(‘John Doe’, ‘john.doe@example.com’)

# => { user_id: 123 }

Choosing Between Savon and Handsoap

Both Savon and Handsoap offer powerful capabilities for working with SOAP web services in Ruby. Your choice between the two libraries may come down to personal preference and project requirements.

Savon is more popular and provides a simple, functional API that is easy to use. It is a good choice if you want to get up and running quickly or if you prefer a more functional approach.

Handsoap, on the other hand, offers an object-oriented approach and provides finer control over SOAP request and response handling. This can be advantageous in complex projects or when working with non-standard SOAP services.

Conclusion

In this article, we have explored two popular Ruby libraries for interacting with SOAP web services: Savon and Handsoap. Both libraries offer powerful features, and your choice between them may depend on personal preference or project requirements. Regardless of the library you choose, Ruby provides an excellent environment for building and consuming SOAP web services.

Leave a Reply

Your email address will not be published. Required fields are marked *