Ruby Web Services Applications - SOAP4R
What is SOAP?
Simple Object Access Protocol (SOAP, all written for the Simple Object Access Protocol) 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, is a lightweight, simple, based on XML (a subset of the Standard Generalized Markup Language under) protocol, which is designed to exchange structured on the WEB and curing information.
Please see more SOAP Tutorial: http://www.w3cschool.cc/soap/soap-tutorial.html .
SOAP4R installation
SOAP4R Hiroshi Nakamura developed by the implementation of SOAP for Ruby applications.
SOAP4R Download: http://raa.ruby-lang.org/project/soap4r/ .
Note: Your ruby environment may already installed the component.
Under Linux you can install the component using the gem, the following command:
$ gem install soap4r --include-dependencies
If you are under the window development environment, you need to download the zip file, and install by executing install.rb.
SOAP4R Service
SOAP4R supports two different types of service:
- Based on CGI / FastCGI services (SOAP :: RPC :: CGIStub)
- Independent Service (SOAP :: RPC: StandaloneServer)
This tutorial will show you how to build an independent SOAP services. Proceed as follows:
Step 1 - Inheritance SOAP :: RPC :: StandaloneServer
In order to achieve their own separate server, you need to write a new class that is SOAP :: RPC :: StandaloneServer sub-categories:
class MyServer < SOAP::RPC::StandaloneServer ............... end
Note: If you want to write a server-based FastCGI, then you need to extend SOAP :: RPC :: CGIStub class, the rest of the program will remain unchanged.
Step Two - Define Processing Methods
Next we define Web Services approach, we define the following two methods, one is the sum of two numbers, one is divided by the number two:
class MyServer < SOAP::RPC::StandaloneServer ............... # 处理方法 def add(a, b) return a + b end def div(a, b) return a / b end end
The third step - publication processing method
Next, we add methods defined on the server, initialize method is disclosed for external connections:
class MyServer < SOAP::RPC::StandaloneServer def initialize(*args) add_method(receiver, methodName, *paramArg) end end
Here is a description of each parameter:
parameter | description |
---|---|
receiver | Object that contains the method name of the method. If you define service method in the same class, the parameter isself. |
methodName | RPC method invocation request name. |
paramArg | Parameter name and parameter mode |
To understandinoutandoutparameters, consider the following service methods, you need to enter two parameters: inParam and inoutParam, the function returns three values after the completion of the implementation: retVal, inoutParam, outParam:
def aMeth(inParam, inoutParam) retVal = inParam + inoutParam outParam = inParam . inoutParam inoutParam = inParam * inoutParam return retVal, inoutParam, outParam end
Publication of call as follows:
add_method(self, 'aMeth', [ %w(in inParam), %w(inout inoutParam), %w(out outParam), %w(retval return) ])
The fourth step - open 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 parameter:
parameter | description |
---|---|
ServerName | Service name, you can take your favorite |
urn: ruby: ServiceName | Hereurn: ruby is fixed, but you can take a uniqueServiceNamefor your service |
hostname | Specify the host name |
port | web service port |
Examples
Next we Through the above steps 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 program, start a listener on port 8080 of the local services, and exposes two methods: add and div.
You can then perform the background of these services:
$ ruby MyServer.rb&
SOAP4R client
ruby use SOAP :: RPC :: Driver class development SOAP client. Next we look in detail using SOAP :: RPC :: Driver class.
Call SOAP service requires the following information:
- SOAP Service URL address (SOAP Endpoint URL)
- Namespace (Method Namespace URI) service method
- Service method name and parameter information
Next, we take a step to create a SOAP client to invoke the above SOAP method: add, div:
Step One - Create instance SOAP Driver
We can call it a new method to instantiate SOAP :: RPC :: Driver class, as follows:
SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)
The following is a description of the parameters:
parameter | description |
---|---|
endPoint | SOAP service connection URL address |
nameSpace | Namespace for SOAP :: RPC :: Driver object all RPC. |
soapAction | For SOAPAction HTTP header field values. If the string is "" the default isnil |
Step two - add service method
SOAP :: RPC :: Driver to add SOAP service method, we can call the following method by way of example SOAP :: RPC :: Driver:
driver.add_method(name, *paramArg)
The following is a description of the parameters:
parameter | description |
---|---|
name | The method name of the remote web services |
paramArg | Parameter to specify the remote program |
The third step - calling SOAP services
Finally, we can use SOAP :: RPC :: Driver instance to invoke SOAP services:
result = driver.serviceMethod(paramArg...)
The actual method name serviceMethod SOAP services, paramArg for the method's parameter list.
Examples
Based on the above steps, we can write the following SOAP client:
#!/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 simply introduce Ruby's Web Services. If you want to know more you can view the official document: the Ruby of Web Services