Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ruby Web Service Application – SOAP4R

Ruby Web Service Application - SOAP4R


What is SOAP?

Simple Object Access Protocol (SOAP, short for 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.

SOAP is a protocol specification for exchanging data. It is a lightweight, simple, XML-based protocol (a subset of Standard Generalized Markup Language) designed to exchange structured and fixed information on the Web.

For more SOAP tutorials, please see:http://www.w3cschool.cc/soap/soap-tutorial.html。


SOAP4R Installation

SOAP4R was developed and implemented by Hiroshi Nakamura for SOAP applications in Ruby.

SOAP4R download address:http://raa.ruby-lang.org/project/soap4r/。

**Note:** Your Ruby environment may already have this component installed.

In a Linux environment, you can also use gem to install this component, with the following command:

gem

install

soap4r

--

include

dependencies

If you are developing in a Windows environment, you need to download the zip file and install it by running install.rb.


SOAP4R Services

SOAP4R supports two different types of services:

This tutorial introduces how to build a standalone SOAP service. The steps are as follows:

Step 1 - Inherit from SOAP::RPC::StandaloneServer

To implement your own standalone server, you need to write a new class that is a subclass of SOAP::RPC::StandaloneServer:

class

MyServer

<

SOAP

::

RPC

::

StandaloneServer

...............

end

**Note:** If you want to write a FastCGI-based server, you need to inherit from the SOAP::RPC::CGIStub class, and the rest of the program remains unchanged.

Step 2 - Define Handler Methods

Next we define the Web Service methods. Here we define two methods: one for adding two numbers and one for dividing two numbers:

class

MyServer

<

SOAP

::

RPC

::

StandaloneServer

...............

Handling Methods

def

add

(

a

,

b

)

return

a

b

end

def

div

(

a

,

b

)

return

a

/

b end end

Step 3 - Publish Handler Methods

Next, add the methods we defined on the server. The initialize method is public and used for external connections:

class

MyServer

<

SOAP

::

RPC

::

StandaloneServer

def

initialize

(

args

)

add_method

(

receiver

,

methodName

, *

paramArg

)

end

end

The following describes each parameter:

Parameters Description
receiver The object containing the method with the given name. If you define the service methods in the same class, this parameter is self。
methodName The name of the method to call for the RPC request.
paramArg Parameter names and parameter modes

To understand the inout and out parameter, consider the following service method, which takes two input parameters: inParam and inoutParam, and returns three values after execution: retVal, inoutParam, and outParam:

def

aMeth

(

inParam

,

inoutParam

)

retVal

=

inParam

inoutParam

outParam

=

inParam

.

inoutParam

inoutParam

=

inParam

inoutParam

return

retVal

,

inoutParam

,

outParam

end

The public calling method is 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 start the service by instantiating the derived class and calling the start method:

myServer

=

MyServer

.

new

(

'

ServerName

'

,

'

urn:ruby:ServiceName

'

,

hostname

,

port

)

myServer

.

start

The following describes the request parameters:

Parameters Description
ServerName The service name, which you can choose as you like
urn:ruby:ServiceName Here urn:ruby is fixed, but you can give your service a unique ServiceName
hostname Specify the host name
port the web service port

Example

Next, through the above steps, we create a standalone service:

Example

require

"

soap/rpc/standaloneserver

"

begin

class

MyServer

<

SOAP

::

RPC

::

StandaloneServer

Expose our service

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 running the above program, a local service listening on port 8080 is started, exposing two methods: add and div.

You can run the above service in the background:


$ ruby MyServer.rb &

SOAP4R Client

In Ruby, the SOAP::RPC::Driver class is used to develop SOAP clients. Next, let's take a detailed look at the use of the SOAP::RPC::Driver class.

Calling a SOAP service requires the following information:

Next, we will create a SOAP client step by step to call the above SOAP methods: add and div:

Step 1 - Create a SOAP Driver Instance

We can call its new method by instantiating the SOAP::RPC::Driver class, as shown below:


SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)

The following describes the parameters:

Parameters Description
endPoint The URL address for connecting to the SOAP service
nameSpace The namespace used for all RPCs of the SOAP::RPC::Driver object.
soapAction The value of the SOAPAction field used in the HTTP header. If the string is "", it defaults to nil

Step 2 - Add Service Methods

To add SOAP service methods to a SOAP::RPC::Driver, we can call the following methods on a SOAP::RPC::Driver instance:


driver.add_method(name, *paramArg)

The following describes the parameters:

Parameters Description
name The method name of the remote web service
paramArg Specifies the parameters of the remote procedure

Step 3 - Call the SOAP Service

Finally, we can use the SOAP::RPC::Driver instance to call the SOAP service:


result = driver.serviceMethod(paramArg...)

serviceMethod is the actual method name of the SOAP service, and paramArg is the parameter list of the method.

Example

Based on the above steps, we can write the following SOAP client:

Example

!/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 have only briefly introduced Ruby's Web Service. If you want to learn more, you can check the official documentation:Ruby Web Service