May 12, 2021 Ruby
Simple Object Access Protocol (SOAP, written entirely as Simple Object Access Protocol) is a protocol specification for exchanging data.
SOAP is a simple XML-based protocol that enables applications to exchange information over HTTP.
A simple object access protocol is a protocol specification for exchanging data, a lightweight, simple, XML-based protocol (a subset of the standard common tag language) designed to exchange structured and cured information on the WEB.
For more SOAP tutorials, check out: // www.w3cschool.cn/soap/soap-intro.html.
SOAP4R was developed by Hiroshi Nakamura for Ruby's SOAP applications.
SOAP4R Download Address: http://raa.ruby-lang.org/soap4r/.
Note: The component may already be installed in your ruby environment.
You can also use gems to install this component in a Linux environment, with the following commands:
$ gem install soap4r --include-dependencies
If you are developing in a window environment, you need to download the zip compression file and install it by executing install.rb.
SOAP4R supports two different service types:
This tutorial will show you how to build a stand-alone SOAP service. Here are the steps:
To implement your own stand-alone server, you need to write a new class, which is a sub-class of SOAP::RPC::StandaloneServer:
class MyServer < SOAP::RPC::StandaloneServer ............... end
Note: If you are writing a FastCGI-based server, you need to inherit the SOAP::RPC::CGIStub class, and the rest of the program will remain the same.
Next we define the method of Web Services, as follows we define two methods, one is two numbers plus, one is two numbers divided:
class MyServer < SOAP::RPC::StandaloneServer
...............
# 处理方法
def add(a, b)
return a + b
end
def div(a, b)
return a / b
end
end
Next, add the method we defined on the server, the initialize method, which is exposed for external connections:
class MyServer < SOAP::RPC::StandaloneServer
def initialize(*args)
add_method(receiver, methodName, *paramArg)
end
end
The following is a description of the parameters:
Parameters | Describe |
---|---|
receiver | The object of the method that contains the method name. If you define a service method in the same class, the parameter is self. |
methodName | The name of the method that called the RPC request. |
paramArg | The parameter name and parameter pattern |
In order to understand the inout and out parameters, consider the following service methods, you need to enter two parameters: inParam and inout Param, and return three values when the function is executed: retVal, inout Param, outParam:
def aMeth(inParam, inoutParam)
retVal = inParam + inoutParam
outParam = inParam . inoutParam
inoutParam = inParam * inoutParam
return retVal, inoutParam, outParam
end
The public call method is as follows:
add_method(self, 'aMeth', [
%w(in inParam),
%w(inout inoutParam),
%w(out outParam),
%w(retval return)
])
Finally, we derive the class by instantiation and call the start method to start the service:
myServer = MyServer.new('ServerName',
'urn:ruby:ServiceName', hostname, port)
myServer.start
Here's a description of the request parameters:
Parameters | Describe |
---|---|
ServerName | Service name, you can take what you like |
urn:ruby:ServiceName | Here urn:ruby is fixed, but you can get a unique ServiceName for your service |
hostname | Specify the host name |
port | The web service port |
Let's take the steps above to create a stand-alone service:
require "soap/rpc/standaloneserver"
begin
class MyServer < SOAP::RPC::StandaloneServer
# Expose our services
def initialize(*args)
add_method(self, 'add', 'a', 'b')
add_method(self, 'div', 'a', 'b')
end
# Handler methods
def add(a, b)
return a + b
end
def div(a, b)
return a / b
end
end
server = MyServer.new("MyServer",
'urn:ruby:calculation', 'localhost', 8080)
trap('INT){
server.shutdown
}
server.start
rescue => err
puts err.message
end
After executing the above procedure, a local service that listens to port 8080 is initiated and two methods are exposed: add and div.
You can perform the above services again in the background:
$ ruby MyServer.rb&
Ruby uses SOAP::RPC::D the RIVER class to develop SOAP clients. Let's take a look at SOAP::RPC::D the river class in detail.
The following information is required to invoke the SOAP service:
Next, we'll take a step-by-step approach to creating the SOAP client to call the soap methods above: add, div:
We can call its new method by instantiation soap:::RPC::D the river class, as follows:
SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)
The following is a description of the parameters:
Parameters | Describe |
---|---|
endPoint | The URL address of the connection SOAP service |
nameSpace | Namespaces for SOAP::RPC::D RPC of the river object . |
soapAction | SOAPAction field value for HTTP header. If the string is "", it defaults to nil |
To add soap service methods :D SOAP::RPC:RPC:3river, we can call the following methods by :D SOAP:::RPC:Rriver:
driver.add_method(name, *paramArg)
Here's a description of the parameters:
Parameters | Describe |
---|---|
name | The method name of the remote web service |
paramArg | Specify the parameters of the remote program |
Finally, we can call the SOAP service using soap::RPC::D instance of the server:
result = driver.serviceMethod(paramArg...)
The actual method name of the serviceMethod SOAP service, paramArg, is a list of parameters for the method.
Based on the above steps, we can write the following SOAP clients:
#!/usr/bin/ruby -w
require 'soap/rpc/driver'
NAMESPACE = 'urn:ruby:calculation'
URL = 'http://localhost:8080/'
begin
driver = SOAP::RPC::Driver.new(URL, NAMESPACE)
# Add remote sevice methods
driver.add_method('add', 'a', 'b')
# Call remote service methods
puts driver.add(20, 30)
rescue => err
puts err.message
end
Above, we're just a brief introduction to Ruby's Web Services. If you want to learn more, check out the official documentation: Ruby's Web Services