home PYTHONJAVA
 

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

Simple Object Access Protocol is a protocol specification for exchanging data. It is a lightweight, simple protocol based on XML (a subset of the standard universal markup language) designed to be exchanged on the WEB. Structured and solidified information.

SOAP4R installation

SOAP4R was developed by Hiroshi Nakamura for Ruby SOAP applications.

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

Note: Your component may already be installed in your ruby ​​environment.

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

gem install soap4r --include-dependencies

If you are developing in a window environment, you will need to download the zip archive and install it by executing install.rb.


SOAP4R Service

SOAP4R supports two different service types:

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

This tutorial will show you how to set up a standalone SOAP service. The steps are as follows:

Step 1 - Inherit 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 are writing a FastCGI-based server, you need to inherit the SOAP::RPC::CGIStub class, and the rest of the program will remain unchanged.

Step 2 - Define the processing method

Next we define the Web Service method, as we define two methods, one is the addition of two numbers, one is the division of two numbers:

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

Step 3 - Announce processing method

Next add the methods we defined on the server, the initialize method is public, 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:

ParametersDescription
receiverThe object of the method containing the method name. If you define a service method in the same class, the parameter is self.
methodName The name of the method that called the RPC request.
paramArgParameter Name and Parameter Mode

In order to understand the inout and out parameters, consider the following service methods, you need to enter two parameters: inParam and inoutParam, return three values ​​after the function is completed: 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 - Turn on the service

Last we finally start the service by instantiating the derived class and calling the start method:

myServer = MyServer.new( 'ServerName', 'urn:ruby:ServiceName'< /span>, hostname, port) myServer.start< /div>

The following is a description of the request parameters:

ParametersDescription
ServerNameService name, you can take your favorite
urn:ruby:ServiceNameHere urn:ruby is fixed, but you can get a unique one for your service ServiceName
hostnameSpecify hostname
portweb service port

Instance

Next we will create a separate service through the above steps:

Instance

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 local service that listens on port 8080 is started and two methods are exposed: add and div.

You can perform the above services in the background:

$ ruby ​​MyServer.< /span>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 invoke the SOAP service:

  • SOAP Service URL Address (SOAP Endpoint URL)
  • Method Namespace URI
  • Service method name and parameter information

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

First Step - Create a SOAP Driver Instance

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

SOAP::RPC< /span>::Driver.new(endPoint, nameSpace,soapAction)
Above we are just a brief introduction to Ruby's Web Service. If you want to know more, you can check out the official documentation: Ruby's Web Service






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.