Ruby Web Services
* * *
## What is SOAP?
Simple Object Access Protocol (SOAP) is a protocol specification for exchanging data.
SOAP is a simple XML-based protocol that allows applications to exchange information over HTTP.
Simple Object Access Protocol is a protocol specification for exchanging data. It is a lightweight, simple, XML-based protocol (a subset of the Standard Generalized Markup Language) designed for exchanging structured and formatted information on the web.
For more SOAP tutorials, please visit: [http://www.w3cschool.cc/soap/soap-tutorial.html](#).
* * *
## SOAP4R Installation
SOAP4R was developed by Hiroshi Nakamura for SOAP applications in Ruby.
SOAP4R download address: [http://raa.ruby-lang.org/project/soap4r/](http://raa.ruby-lang.org/project/soap4r/).
**Note:** Your Ruby environment might already have this component installed.
In a Linux environment, you can also use gem to install the component with the following command:
gem install soap4r --include-dependencies
If you are developing in a Windows environment, you need to download the zip archive and install it by executing install.rb.
* * *
## SOAP4R Services
SOAP4R supports two different service types:
* CGI/FastCGI-based services (SOAP::RPC::CGIStub)
* Standalone services (SOAP::RPC::StandaloneServer)
This tutorial will introduce how to build a standalone SOAP service. The steps are as follows:
### Step 1 - Inherit from SOAP::RPC::StandaloneServer
To implement your own standalone server, you need to write a new class that is a subclass of SOAP::RPC::StandaloneServer:
class MyServer<SOAP::RPC::StandaloneServer ............... end
**Note:** If you want to write a FastCGI-based server, you need to inherit from the SOAP::RPC::CGIStub class. The rest of the program will remain the same.
### Step 2 - Define Handler Methods
Next, we define the Web Service methods. Here we define two methods: one for adding two numbers and one for dividing two numbers:
class MyServer<SOAP::RPC::StandaloneServer ............... def add(a, b)return a + b end def div(a, b)return a / b end end
### Step 3 - Publish the Handler Methods
Next, we add the methods defined on the server. The initialize method is public and is used for external connections:
class MyServer<SOAP::RPC::StandaloneServer def initialize(*args)add_method(receiver, methodName, *paramArg)end end
Here is the description of each parameter:
| Parameter | Description |
| --- | --- |
| **receiver** | The object containing the method. If you define the service methods in the same class, this parameter is _self_. |
| **methodName** | The method name to be called by the RPC request. |
| **paramArg** | Parameter names and parameter modes |
To understand _inout_ and _out_ parameters, consider the following service method, which requires two input parameters: inParam and inoutParam. After execution, it returns three values: retVal, inoutParam, and outParam:
def aMeth(inParam, inoutParam)retVal = inParam + inoutParam outParam = inParam . inoutParam inoutParam = inParam * inoutParam return retVal, inoutParam, outParam end
The public method call is as follows:
add_method(self, 'aMeth', [%w(in inParam), %w(inout inoutParam), %w(out outParam), %w(retval return)])
### Step 4 - Start the Service
Finally, we start the service by instantiating the derived class and calling the start method:
myServer = MyServer.new('ServerName', 'urn:ruby:ServiceName', hostname, port)myServer.start
Here is the description of the request parameters:
| Parameter | Description |
| --- | --- |
| **ServerName** | The service name, you can choose any name you like. |
| **urn:ruby:ServiceName** | Here _urn:ruby_ is fixed, but you can choose a unique _ServiceName_ for your service. |
| **hostname** | Specifies the hostname. |
| **port** | The web service port. |
### Example
Next, we will create a standalone service using the steps above:
## Example
require"soap/rpc/standaloneserver"begin class MyServer err puts err.message end
After executing the above program, a local service listening on port 8080 is started, exposing two methods: add and div.
You can run the service in the background:
$ ruby MyServer.rb &
* * *
## SOAP4R Client
In Ruby, the SOAP::RPC::Driver class is used to develop SOAP clients. Let's take a detailed look at how to use the SOAP::RPC::Driver class.
Calling a SOAP service requires the following information:
* SOAP Service URL (SOAP Endpoint URL)
* Method Namespace URI
* Service method name and parameter information
Next, we will create a SOAP client step by step to call the SOAP methods mentioned above: add and div:
### Step 1 - Create a SOAP Driver Instance
We can call the new method by instantiating the SOAP::RPC::Driver class, as shown below:
SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)
Here is the description of the parameters:
| Parameter | Description |
| --- | --- |
| **endPoint** | The URL address to connect to the SOAP service. |
| **nameSpace** | The namespace for all RPCs of the SOAP::RPC::Driver object. |
| **soapAction** | The value for the SOAPAction field in the HTTP header. If the string is "", it defaults to _nil_. |
### Step 2 - Add Service Methods
To add SOAP service methods to the SOAP::RPC::Driver, we can call the following method on the SOAP::RPC::Driver instance:
driver.add_method(name, *paramArg)
Here is the description of the parameters:
| Parameter | Description |
| --- | --- |
| **name** | The method name of the remote web service. |
| **paramArg** | Specifies the parameters for the remote procedure. |
### Step 3 - Call the SOAP Service
Finally, we can use the SOAP::RPC::Driver instance to call the SOAP service:
result = driver.serviceMethod(paramArg...)
serviceMethod is the actual method name of the SOAP service, and paramArg is the list of method parameters.
### Example
Based on the steps above, we can write the following SOAP client:
## Example
require'soap/rpc/driver'NAMESPACE = 'urn:ruby:calculation'URL = 'http://localhost:8080/'begin driver = SOAP::RPC::Driver.new(URL, NAMESPACE)driver.add_method('add', 'a', 'b')puts driver.add(20, 30)rescue =>err puts err.message end
Above, we briefly introduced Ruby's Web Service. If you want to learn more, you can check the official documentation: (http://www.ruby-doc.org/stdlib/libdoc/soap/rdoc/index.html "Web Service with Ruby")
YouTip