Using soap4r of ruby to write a tutorial of soap server

Time:2022-5-2

What isSOAP ?

Simple object access protocol (soap) is a cross platform, language independent, XML based RPC Protocol, usually (but not necessarily) http.

It uses XML to encode information, enabling remote procedure calls, HTTP over the network from clients toThe serverTo transmit information, and vice versa.

Soap has several advantages over other technologies, such as COM and CORBA. For example, it has relatively low deployment and debugging costs, its scalability and ease of use. There are several different languages and platforms.

See the simple tutorial for soap

This tutorial will familiarize you with the soap implementationRubySOAP4R)。 This is a basic tutorial, so if you need to go into details, you need to refer to other resources.
Install soap4r:

Soap4r is compiled by Hiroshi Nakamura. You can download the soap implementation developed by ruby directly from the Internet:

Note: this component may already be installed.

Download SOAP

If you know the gem utility, you can use the following command to install soap4r and related packages.

$ gem install soap4r –include-dependencies

If you are working on windows, you need to download a compressed file. From the above location, you need to install it and run ruby install.exe using the standard installation method rb.
Write soap4r server:

Soap4r supports two different types of servers:

  1.     CGI/FastCGI based (SOAP::RPC::CGIStub)

  2.     Standalone (SOAP::RPC:StandaloneServer)

This tutorial will write a separate server in detail. The following steps are involved in writing a soap server:
Step 1 – inherit soap:: RPC:: standaloneserver class:

To implement your own independent server, you need to write a new class, which is a subclass of soap:: standaloneserver class, as follows:

 

Copy codeThe code is as follows:
class MyServer < SOAP::RPC::StandaloneServer
  ……………
end

 

Note: if you want to write a fastcgi based server, you need to inherit the soap:: RPC:: cgistub class, and the other steps will remain the same.
Step 2 – define the handler method:

The second step is to write web service methods, hoping to be exposed to the outside world.

They can be written as simple Ruby methods. For example, let’s write the method of adding two numbers and dividing two numbers:

?
1
2
3
4
5
6
7
8
9
10
11
class MyServer < SOAP::RPC::StandaloneServer
  ...............
 
  # Handler methods
  def add(a, b)
   return a + b
  end
  def div(a, b)
   return a / b
  end
end

Step 3 – exposure procedure method:

The next step is to add the method we defined to our server. The initialize method is used to expose the service, using one of the following two methods:

?
1
2
3
4
5
class MyServer < SOAP::RPC::StandaloneServer
  def initialize(*args)
   add_method(receiver, methodName, *paramArg)
  end
end

The following parameter description:

Using soap4r of ruby to write a tutorial of soap server

 To understand the usage of inout or out parameters, consider the following service method that takes two parameters (inParam and inoutParam), returns one normal return value (retVal) and two further parameters: inoutParam and outParam:

?
1
2
3
4
5
6
def aMeth(inParam, inoutParam)
  retVal = inParam + inoutParam
  outParam = inParam . inoutParam
  inoutParam = inParam * inoutParam
  return retVal, inoutParam, outParam
end

Now, we can disclose this method as follows:

?
1
2
3
4
5
6
add_method(self, 'aMeth', [
  %w(in inParam),
  %w(inout inoutParam),
  %w(out outParam),
  %w(retval return)
])

Step 4 – start the server:

The last step is to start the server through an instance of the derived class of the instance and call the start method.

?
1
2
3
4
myServer = MyServer.new('ServerName',
            'urn:ruby:ServiceName', hostname, port)
 
myServer.start

This is a description of the required parameters:

Using soap4r of ruby to write a tutorial of soap server

For example:

Now, using the above steps, let’s write a separate server:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

When the application starts to listen on the local host port of the soap server 8080. It exposes a service method: add and div, which requires two parameters and returns the result.

Now you can run the server background as follows:

?
1
$ ruby MyServer.rb&

Write soap4r client:

The soap:: RPC:: driver class is used to provide support for writing soap client applications. This tutorial introduces this class, showing the basics of the application it uses.

The following is the minimum information required to call the soap service:

  • Soap service (soap endpoint URL)

  • Service method (method namespace URI)

  • Name of service method and its parameters

Now we will write a soap client to call the method defined by the service. In the above example, the names are add and Div.

The following are the main steps to create a soap client:
Step 1 – create a soap driver instance:

We create an instance of soap:: RPC:: driver by calling the new method as follows:

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

This is a description of the required parameters:

Using soap4r of ruby to write a tutorial of soap server

Step 2 – how to add a service:

To add the soap service method to the soap:: RPC:: driver, we can call the following method to use the soap:: RPC:: Driver instance:

driver.add_method(name, *paramArg)

The following parameter description:

Using soap4r of ruby to write a tutorial of soap server

 Step 3 – call soap service:

The last step is to call the soap service. The soap:: RPC:: Driver instance is as follows:

?
1
result = driver.serviceMethod(paramArg...)

Here servicemethod is the actual web service method and paramarg Is a list parameter that needs to be passed in the service method.
For example:

According to the above steps, we will write a soap client as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/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

 

Recommended Today

JMeter – the difference between HTTP request following redirection and automatic redirection

Automatic redirection: After httpclient receives the request, if the request contains a redirection request, httpclient can jump automatically, but only for get and head requests. If this item is checked, the “follow redirection” will be invalid; Automatic redirection can automatically turn to the final target page, but JMeter does not record the content of the […]