6.43. Ruby Web Service Application-SOAP4R

发布时间 : 2025-10-25 13:34:00 UTC      

Page Views: 9 views

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

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

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

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

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

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

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

name

The method name of the remote web service

paramArg

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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。