Ruby Web Service Application - SOAP4R
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 over HTTP.
Simple Object Access Protocol is a protocol specification for exchanging data. It is a lightweight, simple protocol based on XML (a subset of the standard universal markup language) designed to be exchanged on the WEB. Structured and solidified information.
SOAP4R was developed by Hiroshi Nakamura for Ruby SOAP applications.
SOAP4R Download: http://raa.ruby-lang.org/project/soap4r /.
Note: Your component may already be installed in your ruby environment.
You can also use gem to install this component in Linux environment. The command is as follows:
If you are developing in a window environment, you will need to download the zip archive and install it by executing install.rb.
SOAP4R supports two different service types:
- Based on CGI/FastCGI service (SOAP::RPC::CGIStub)
- Standalone service (SOAP::RPC:StandaloneServer)
This tutorial will show you how to set up a standalone SOAP service. The steps are as follows:
Step 1 - Inherit SOAP::RPC::StandaloneServer
To implement your own standalone server, you need to write a new class that is a subclass of SOAP::RPC::StandaloneServer:
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 unchanged.
Step 2 - Define the processing method
Next we define the Web Service method, as we define two methods, one is the addition of two numbers, one is the division of two numbers:
Step 3 - Announce processing method
Next add the methods we defined on the server, the initialize method is public, for external connections:
The following is a description of each parameter:
|receiver||The object of the method containing 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||Parameter Name and Parameter Mode|
In order to understand the inout and out parameters, consider the following service methods, you need to enter two parameters: inParam and inoutParam, return three values after the function is completed: retVal, inoutParam, outParam:
The public calling method is as follows:
Step 4 - Turn on the service
Last we finally start the service by instantiating the derived class and calling the start method:
The following is a description of the request parameters:
|ServerName||Service name, you can take your favorite|
|urn:ruby:ServiceName||Here urn:ruby is fixed, but you can get a unique one for your service ServiceName|
|port||web service port|
Next we will create a separate service through the above steps:
After executing the above program, a local service that listens on port 8080 is started and two methods are exposed: add and div.
You can perform the above services in the background:
$ ruby MyServer.< /span>rb &
Use the SOAP::RPC::Driver class in ruby to develop a SOAP client. Next, let's take a closer look at the use of the SOAP::RPC::Driver class.
The following information is required to invoke the SOAP service:
- SOAP Service URL Address (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 above SOAP method: add, div:
First Step - Create a SOAP Driver Instance
We can call its new method by instantiating the SOAP::RPC::Driver class as follows:
SOAP::RPC< /span>::Driver.new(endPoint, nameSpace,soapAction)Above we are just a brief introduction to Ruby's Web Service. If you want to know more, you can check out the official documentation: Ruby's Web Service