English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Web Service Application SOAP4R

What is SOAP?

The Simple Object Access Protocol (SOAP, full name Simple Object Access Protocol) is a protocol specification for data exchange.

SOAP is a simple XML-based protocol that allows applications to exchange information via HTTP.

The Simple Object Access Protocol (SOAP) is a protocol specification for data exchange, which is a lightweight, simple protocol based on XML (a subset of the Standard Generalized Markup Language). It is designed to exchange structured and fixed information on the WEB.

SOAP4R installation

SOAP4R is developed and implemented by Hiroshi Nakamura, used for Ruby SOAP applications.

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

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

Under the Linux environment, you can also use gem to install this component, the command is as follows:

gem install soap4r --include-dependencies

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

SOAP4R service

SOAP4R supports two different types of services:

  • Based on CGI/FastCGI service (SOAP::RPC::CGIStub)

  • Independent service (SOAP::RPC:StandaloneServer)

This tutorial will introduce how to establish an independent SOAP service. The steps are as follows:

The1Step - Inherit SOAP::RPC::StandaloneServer

To implement your own independent 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 server based on FastCGI, you need to inherit the SOAP::RPC::CGIStub class, and the rest of the program will remain unchanged.

Step 2 - Define the handling method

Next, we define the methods of the Web Service, as follows, we define two methods, one for adding two numbers and one for dividing two numbers:

class MyServer < SOAP::RPC::StandaloneServer
   .............
 
   # Handling method
   def add(a, b)
      return a + b
   end
   def div(a, b) 
      return a / b 
   end
end

Step 3 - Announce the handling method

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

class MyServer < SOAP::RPC::StandaloneServer
   def initialize(*args)
      add_method(receiver, inoutParam, *paramArg)
   end
end

The following is the description of each parameter:

ParametersDescription
receiverThe object containing the method name. If you define the service method in the same class, this parameter is self.
methodNamethe method name of the RPC request called.
paramArgparameter names and parameter modes

To understand inout and out The parameters, consider the following service method, which requires two parameters: inParam and inoutParam, the function 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 following are the public calling methods:

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 the description of the request parameters:

ParametersDescription
ServerNameservice name, you can choose your favorite
urn:ruby:ServiceNameHere urn:ruby is fixed, but you can give your service a unique ServiceName
hostnameSpecify the hostname
portWeb service port

Online Example

Next, we create an independent service through the above steps:

Online 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 executing the above program, a listener is started 808Local service of port 0, and publicly exposes two methods: add and div.

You can run the above service in the background:

$ ruby MyServer.rb &

SOAP4R Client

Develop SOAP clients using the SOAP::RPC::Driver class in Ruby. Let's take a detailed look at the usage of the SOAP::RPC::Driver class.

The following information is required to call the SOAP service:

  • SOAP service URL address (SOAP Endpoint URL)

  • Service method namespace (Method Namespace URI)

  • Service method name and parameter information

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

Step 1 - Create SOAP Driver Example

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 is the description of the parameters:

ParametersDescription
endPointThe URL address of the connection to the SOAP service
nameSpaceNamespace for all RPCs of the SOAP::RPC::Driver object.
soapActionThe value of the SOAPAction field in the HTTP header. If it is a string and it is "", then the default is nil

Step 2 - Add service method

To add a SOAP service method for SOAP::RPC::Driver, we can call the following method through the example SOAP::RPC::Driver:

driver.add_method(name, *paramArg)

The following is the description of the parameters:

ParametersDescription
nameThe method name of the remote web service
paramArgSpecify the parameters of the remote procedure

Step 3 - Call SOAP service

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

result = driver.serviceMethod(paramArg...)

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

Online Example

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

Online 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 service methods
   driver.add_method('add', 'a', 'b')
 
   # Call remote service methods
   puts driver.add(20, 30)
rescue => err
   puts err.message
end

In this section, we just briefly introduce Ruby's Web Service. If you want to learn more, you can view the official documentation:Ruby's Web Service