Ruby Web Services



What is SOAP?

Simple Object Access Protocol (SOAP, written in full as 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.

Simple Object Access Protocol is a protocol specification for exchanging data. It is a lightweight, simple, XML-based (a subset of the Standard Universal Markup Language) protocol. It is designed to Exchange structured and solid 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 Ruby SOAP applications.

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

Note: Your ruby ​​environment may already have this component installed.

You can also use gem to install this component in a Linux environment. The command is as follows:

$ gem install soap4r --include-dependencies

If you are developing in a window environment, you need to download the zip file and execute install.rb to install.


SOAP4R service

SOAP4R supports two different service types:

  • ## 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:

Step 1 - Inherit SOAP::RPC::StandaloneServer

In order to implement your own independent server, you need to write a new class, which is SOAP::RPC Subclass of ::StandaloneServer:

class MyServer < SOAP::RPC::StandaloneServer
  ...............
end

Note: If you want to write a server based on FastCGI, then you need to inherit the SOAP::RPC::CGIStub class, and the rest of the program will constant.

Step 2 - Define the processing method

Next we define the method of Web Services. As follows, we define two methods, one is to add two numbers, and the other is to divide two numbers. :

class MyServer < SOAP::RPC::StandaloneServer
   ...............

   # 处理方法
   def add(a, b)
      return a + b
   end
   def div(a, b) 
      return a / b 
   end
end

The third step - publish the processing method

Next add the method 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 is a description of each parameter:

ParameterDescriptionThe object of the method containing the method name. If you define the service method in the same class, this parameter is The method name for calling the RPC request. Parameter name and parameter pattern

In order to understand the inout and out parameters, consider the following service method, which requires inputting two parameters: inParam and inoutParam. After the function is executed, it returns three values: retVal, inoutParam, 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 is a description of the request parameters:

receiver self.
methodName
paramArg
Here portExample
ParametersDescription
ServerName Service name, you can choose whatever you like
##urn:ruby:ServiceNameurn:ruby is fixed, but you can get a unique ServiceName
hostname## for your service #Specify host name
web service port

Next we create an independent service through the above steps:

require "soap/rpc/standaloneserver"

begin
   class MyServer < SOAP::RPC::StandaloneServer

      # Expose our services
      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 local service listening to port 8080 is started and two public services are opened. Methods: add and div.

You can execute the above services in the background:

$ ruby MyServer.rb&

SOAP4R client

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 call the SOAP service:

    SOAP service URL address (SOAP Endpoint URL)
  • Name of the service method Space (Method Namespace URI)
  • Service method name and parameter information
  • Next we will create a SOAP client step by step to call the above SOAP methods: add, div:

Step 1 - Create a SOAP Driver instance

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

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

The following is the description of the parameters:

ParameterendPoint##nameSpacesoapActionStep 2 - Add the service method
Description
URL address to connect to the SOAP service
The namespace is used for SOAP: :RPC::Driver object for all RPC .
The SOAPAction field value used for the HTTP header. If the string is "", the default is nil
to SOAP::RPC ::Driver adds a SOAP service method. We can call the following method through the instance SOAP::RPC::Driver:

driver.add_method(name, *paramArg)

The following is a description of the parameters:

ParametersDescriptionnameparamArg

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 The actual method name of the SOAP service, paramArg is The method's parameter list.

Example

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

#!/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 just briefly introduce Ruby's Web Services. If you want to know more, you can check out the official documentation: Ruby's Web Services

The method name of the remote web service
Specifies the parameters of the remote program