Using Ruby's SOAP4R to compile a SOAP server tutorial, rubysoap4r to compile soap

Source: Internet
Author: User
Tags soap client

Using Ruby's SOAP4R to compile a SOAP server tutorial, rubysoap4r to compile soap

What is SOAP?

Simple Object Access Protocol (SOAP) is a cross-platform and language-independent RPC protocol based on XML, usually (but not necessarily) HTTP.

It uses XML to encode information for remote process calls. HTTP transmits information from the client to the server on the network, 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, and has several different languages and platforms.

See a simple tutorial to learn about SOAP.

This tutorial will familiarize you with SOAP implementation Ruby (SOAP4R ). This is a basic tutorial, so if you need in-depth 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 from the Internet:

Note: This component may have been 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 it is working on Windows, you need to download a compressed file. From the above location, you need to install it and run Ruby install. rb using the standard installation method.
Compile SOAP4R Server:

SOAP4R supports two different types of servers:

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

This tutorial will detail a separate server. To write a SOAP server, follow these steps:
Step 2-inherit from the SOAP: RPC: StandaloneServer class:

To implement your own independent server, you need to write a new class, which includes the sub-classes of the 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 remaining steps will be the same.
Step 2-define the handler method:

The second step is to compile the Web service method and make it public to the outside world.

They can be written into a simple Ruby method. For example, let's write the method of adding two numbers and division two numbers:

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 2-expose the handler method:

The next step is to add the method we defined to our server. The initialize method is used to expose services. One of the following two methods is used:

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

The following parameter description:


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:

def aMeth(inParam, inoutParam)
  retVal = inParam + inoutParam
  outParam = inParam . inoutParam
  inoutParam = inParam * inoutParam
  return retVal, inoutParam, outParam
end

Now, we can publish this method as follows:

add_method(self, 'aMeth', [
  %w(in inParam),
  %w(inout inoutParam),
  %w(out outParam),
  %w(retval return)
])

Step 2-start the server:

The last step is to start the server by using an instance of the Instance's derived class and calling the start method.

myServer = MyServer.new('ServerName',
            'urn:ruby:ServiceName', hostname, port)

myServer.start

This is the description of the required parameter:


For example:

Now, using the above steps, let's write an independent server:

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

During execution, the server application starts an independent SOAP service listening to port 8080 requests on localhost. 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:

$ ruby MyServer.rb&

Compile the SOAP4R client:

The SOAP: RPC: Driver Class is used to write data to a SOAP client application. This tutorial introduces this class to show the basics of the application it uses.

The following is the minimum required information and you need to call the SOAP service:

  • SOAP Service (SOAP endpoint URL)
  • Service method (method namespace URI)
  • Service method name and its parameters

Now we will compile a SOAP client to call the service definition method. The above example is named "add" and "div.

Follow these steps to create a SOAP client:
Step 1-create a SOAP driver instance:

We create an instance SOAP: RPC: Driver by calling the new method as follows:

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

This is the description of the required parameter:


Step 2-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:


 Step 2-call the SOAP service:

The last step is to call the SOAP Service to use the SOAP: RPC: Driver instance as follows:

result = driver.serviceMethod(paramArg...)

Here serviceMethod is the actual Web service method and paramArg... Is the list parameter that needs to be passed in the service method.
For example:

Based on the above steps, we will compile a SOAP client as follows:

#!/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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.