Ruby Web Service Application-SOAP4R


Release date:2023-11-03 Update date:2023-11-03 Editor:admin View counts:164

Label:

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 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.

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.

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:

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.

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

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

receiver

The object of the method containing the method name. If you define a servicemethod in the same class, this parameter is? self.

methodName

The name of the method that calls the RPC request.

paramArg

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 retValinoutParamoutParam :

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)])

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

ServerName

Service name. You can take whatever you like.

urn:ruby:ServiceName

Here?urn:ruby? It’s fixed, but you can get a unique ServiceName for your service.

hostname

Specify hostname

port

Web service port

Example

Next, let’s create a stand-alone service by following the steps above:

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

After executing the above program, a local service listening on port 8080 isstarted, and two methods are exposed: add and div .

You can perform the above services at the backend:

$ ruby MyServer.rb &

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 :

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

endPoint

URL address to connect to the SOAP service

nameSpace

The namespace is used for all RPC of the SOAP::RPC::Driver object.

soapAction

The SOAPAction field value used for the HTTP header. If the string is “”, the default is nil

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

name

The method name of the remote web service

paramArg

Specify the parameters of the remote program

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.

Example

Based on the above steps, we can write the following 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

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

Powered by TorCMS (https://github.com/bukun/TorCMS).