Ruby Web Service Application - SOAP4R

What is SOAP?

Simple Object Access Protocol (SOAP) is a convention detail for trading information.

SOAP is a straightforward XML-based convention that empowers applications to trade data over HTTP.

Simple Object Access Protocol is a convention detail for trading information. It is a lightweight, basic convention dependent on XML (a subset of the standard all inclusive markup language) intended to be traded on the WEB. Organized and cemented data.

SOAP4R installation

SOAP4R was created by Hiroshi Nakamura for Ruby SOAP applications.

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

Note: Your part may as of now be introduced in your ruby ​​environment.

You can likewise utilize pearl to introduce this segment in Linux condition. The order is as follows:

gem install soap4r - include-dependencies

If you are creating in a window situation, you should download the zip document and introduce it by executing install.rb.

SOAP4R Service

SOAP4R bolsters two diverse administration types:

  • In view of CGI/FastCGI administration (SOAP::RPC::CGIStub)
  • Independent administration (SOAP::RPC:StandaloneServer)

This instructional exercise will tell you the best way to set up an independent SOAP administration. The means are as follows:

Step 1 - Inherit SOAP::RPC::StandaloneServer

To actualize your very own independent server, you have to compose another class that is a subclass of SOAP::RPC::StandaloneServer:

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

Note: If you are composing a FastCGI-based server, you have to acquire the SOAP::RPC::CGIStub class, and the remainder of the program will stay unaltered.

Step 2 - Define the preparing method

Next we characterize the Web Service technique, as we characterize two strategies, one is the expansion 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:

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:

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


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


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.