6.43.1. What is SOAP? #
Simple object access Protocol (SOAP) is a protocol specification for exchanging data.
SOAP is a simple XML-based protocol that enables applications to exchange information through HTTP.
Simple object access protocol is a protocol specification for exchanging data, which is lightweight, simple and based on XML (a subset of the standard general markup language). It is designed to exchange structured andsolidified information on WEB.
6.43.2. SOAP4R installation #
SOAP4R is developed and implemented by Hiroshi Nakamura and is used in SOAP applications of Ruby.
SOAP4R download address: http://raa.ruby-lang.org/project/soap4r/ .
Note: this component may already be installed in your ruby environment.
You can also use it in Linux environment.
gem
to install the component, use the following command:
geminstallsoap4r--include-dependencies
If you are developing under the window environment, you need to download thezip file and execute the
install.rb
to install.
6.43.3. SOAP4R service #
SOAP4R supports two different types of services:
Based on CGI/FastCGI Services (SOAP::RPC::CGIStub)
Stand-alone Service (SOAP::RPC:StandaloneServer)
This tutorial will show you how to build a stand-alone SOAP service. The steps are as follows:
6.43.4. Step 1-inherit SOAP::RPC::StandaloneServer #
To implement your own stand-alone server, you need to write a new class, which is subclasses of
SOAP::RPC::StandaloneServer
:
classMyServer<SOAP::RPC::StandaloneServer...............end
Note: if you are writing a FastCGI-based server, you need to inherit
SOAP::RPC::CGIStub
class, the rest of the program will remain the same.
6.43.5. Step 2-define the processing method #
Next, we define the method of Web Service. As follows, we define two methods, one is the addition of two numbers, and the other is the division of two numbers:
classMyServer<SOAP::RPC::StandaloneServer...............#processing method defadd(a,b)returna+benddefdiv(a,b)returna/b
end end
6.43.6. Step 3-publish the processing method #
Next, add the method we defined on the server.
initialize
method is exposed for external connections:
classMyServer<SOAP::RPC::StandaloneServerdefinitialize(*args)add_method(receiver,methodName,
*paramArg)endend
The following is a description of the parameters:
Parameters. | Description |
|---|---|
| The object of the method containing the method name. If you define a servicemethod in the same class, this parameter is? self. |
| The name of the method that calls the RPC request. |
| Parameter name and parameter mode |
In order to understand
inout
and
out
Parameters, considering the following service methods, you need to enter two parameters:
inParam
and
inoutParam
, three values are returned after the function has been executed
retVal
、
inoutParam
、
outParam
:
defaMeth(inParam,inoutParam)retVal=inParam+inoutParamoutParam=inParam.inout
ParaminoutParam=inParam*inoutParamreturnretVal,inoutParam,outParamend
The exposed calling methods are as follows:
add_method(self,'aMeth',[%w(in inParam),%w(inout inoutParam),%w(out
outParam),%w(retval return)])
6.43.7. Step 4-start the service #
Finally, we instantiate the derived class and call the
start
method to start the service:
myServer=MyServer.new('ServerName','urn:ruby:ServiceName',hostname,port)myServer.start
The following is a description of the request parameters:
Parameters. | Description |
|---|---|
| Service name. You can take whatever you like. |
| Here?urn:ruby? It’s fixed, but you can get a unique ServiceName for your service. |
| Specify hostname |
| Web service port |
6.43.8. Example #
Next, let’s create a stand-alone service by following the steps above: After executing the above program, a local service listening on port 8080 isstarted, and two methods are exposed: You can perform the above services at the backend:Example #
require"soap/rpc/standaloneserver"beginclassMyServer<SOAP::RPC::StandaloneServer#Expose
our
servicedefinitialize(*args)add_method(self,'add','a','b')add_method(self,'div','a','b')end#Handler
methodsdefadd(a,b)returna+benddefdiv(a,b)returna/b end end server =
MyServer.new("MyServer", 'urn:ruby:calculation', 'localhost', 8080)
trap('INT){ server.shutdown } server.start rescue => err puts
err.message end
add
and
div
.$ ruby MyServer.rb &
6.43.9. SOAP4R client #
Used in ruby
SOAP::RPC::Driver
class to develop SOAP clients. Next, let’s take a closer look at the use of the
SOAP::RPC::Driver
.
Call
SOAP
service requires the following information:
SOAP Service URL address
Namespace of the service method
Service method name and parameter information
Next, we will create a SOAP client step by step to call the above SOAP method:
add
,
div
:
6.43.10. Step 1-create a SOAP Driver instance #
We can instantiate
SOAP::RPC::Driver
class to call its new method, as follows
SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)
The following is a description of the parameters:
Parameters. | Description |
|---|---|
| URL address to connect to the SOAP service |
| The namespace is used for all RPC of the SOAP::RPC::Driver object. |
| The SOAPAction field value used for the HTTP header. If the string is “”, the default is nil |
6.43.11. Step 2-add a service method #
For
SOAP::RPC::Driver
to add a SOAP service method, we can use the instance
SOAP::RPC::Driver
to call the following methods:
driver.add_method(name, *paramArg)
The following is a description of the parameters:
Parameters. | Description |
|---|---|
| The method name of the remote web service |
| Specify the parameters of the remote program |
6.43.12. Step 3-invoke the SOAP service #
Finally, we can use the
SOAP::RPC::Driver
instance to invoke the SOAP service:
result = driver.serviceMethod(paramArg...)
The actual method name of
serviceMethod
SOAP
service
paramArg
is a list of parameters for the method.
6.43.13. Example #
Based on the above steps, we can write the following Above we just briefly introduce Ruby’s Web Service. If you want to know more, you can check the official document: Ruby’s Web Service
SOAP
client:Example #
#!/usr/bin/ruby
-wrequire'soap/rpc/driver'NAMESPACE='urn:ruby:calculation'URL='
http://localhost:8080/'begindriver=SOAP::RPC::Driver.new(URL,NAMESPACE)#Add
remote sevice methodsdriver.add_method('add','a','b')#Call remote
service methodsputsdriver.add(20,30)rescue=>errputserr.messageend