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)
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 methodNext 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 endThe third step - publish the processing methodNext 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 endThe following is a description of each parameter:
Description | |
---|---|
receiver | The object of the method containing the method name. If you define the service method in the same class, this parameter isself. |
methodName | The method name for calling the RPC request.|
paramArg | Parameter name and parameter pattern
Parameters | Description |
---|---|
ServerName | Service name, you can choose whatever you like |
##urn:ruby:ServiceName | Hereurn: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:
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 |
driver.add_method(name, *paramArg)
The following is a description of the parameters: The method name of the remote web service | |
Specifies the parameters of the remote program |