3. Ruby and SOAP
Simple Object Access Protocol (SOAP) has become a standard protocol for remote procedure call (RPC). (More information about SOAP can be found in http://www.linuxmagazine.com/2001-10/soap_04.html and http://www.linuxmagazine.com/2002-08/webs_01.html
Ruby provides powerful support for SOAP, whether it is in the client or the server, use SOAP4R, only 4 parts can be created to create a SOAP request:
One endpoint (endpoint) or the network address of the SOAP request, and an endpoint is generally code that runs in a web server environment, but there are some other SOAP transfer, including mail. A namespace defines an environment context, which resolves the called method name here. A method name. The name of the method of remote procedure call. A set of parameters.
When using SOAP4R, we need to specify the first two parameters when creating a SOAP driver. The third parameter is used for this drive binding method, and the final parameter is used when calling the actual method.
For example, we have a SOAP that handles sales orders running at http://my.server.com, we want to access this service on the client, first create a SOAP :: Driver object, you need to specify the namespace when you create this object. And server addresses (the first two parameters for creating this object) are related to the log log, here we can do not need to test)
NS = "urn: orderrsservice"
SVR = "http://my.server.com/onders"
DRV = SOAP :: Driver.new (NIL, NIL, NS, SVR)
Once we built this driver, you can use its AddMethod method to add the name of the method we need to call to the server. The first parameter is the name of this method. The rest of the parameters are the parameter name required by this method, here, we need The way to access is ORDERS_FOR_PRODUCT (Translator Note: Whether it should be ORDERS_FOR?), Pass it to the parameter 10 customer account and product code. (Ruby does not use WSDL to describe the SOAP interface)
Drv.addmethod ("ORDERS_FOR", "Cust_Acct", "Prod_code")
Once all things are completed, we can call the server with this method:
Customers.each Do | Cust_Acct |
Products.each Do | Products.each Do | Products.each |
ORDERS = drv.orders_for (Cust_Acct, Prod_code)
Process (Orders)
end
end
This example also benefits from the dynamics of the Ruby language, such as the Orders_for method dynamically add to the SOAP driver object, so we can call this method with Drv.OrDers_for. And we don't need to define the return value of this method, and the SOAP driver will automatically convert the results obtained from the server to the appropriate Ruby object.
For more detailed explanation, let's take a real example. Listing 4 shows how to get query results from the GooGEL search engine with SOAP, this code is based on Ian MacDonald's Google.rb. Before running this program, you need to register on Google (http://www.google.com/accounts), then get a key, specify the key you get in the third line. Listing 4: Query Google using SOAP
1 Require "SOAP / Driver"
2 endpoint = 'http://api.google.com/search/beta2'
3 ns = 'URN: Googlesearch'
4 key = "get_a_key_from_google"
5
6 fail "missing query args" if argv.empty?
Seduce
8 query = argv.join ("")
9
10 soap = soap :: driver.new (NIL, NIL, NS, Endpoint)
11 soap.addmethodwithsoapaction
12 'Dogooglesearch', NS, 'Key', 'Q', 'Start', 'MaxResults',
13 'Filter', 'Restrict', 'SafeSearch', 'LR', 'IE', 'OE'
14 res = soap.dogoogleaserch (
15 KEY, Query, 0, 10, False, NIL, False, NIL, 'Latin1', 'Latin1')
16
17 PUTS "Estimated Result Count:" Res.stimatedTotalResultscount
18
19 Res.ResulTelements.each Do | Entry |
20 PUTS
21 puts "# {entry.URL}: # {entry.title}"
22 Puts entry.snippet
23 end
The actual Google query is executed by the method Dogooglesearch on the Google's server. This method receives 10 parameters (for the specific meaning of these 10 parameters, you can refer to Google's Web API document), but our example, we only specify We have used the default value for query conditions. In Chapter 11, we drive SOAP to add Method Dogooglesearch, chapter 14, we call this method to perform real queries.
From Google Returns is a very complex object, from the high-level, this result includes some information of this query itself, such as the index value of the start and end, querying the condition, the query consumption time, etc. The result of the query exists in an array. This is the same as the result of the number you see on the Google page. Each query result record itself is also a very complex object. In our example, we only took out its title, URL, and part of the text.
The SOAP interface makes our work simple, it automatically creates iterations of the result object, create an access method for the various properties of the result object, then we can use: Res.ResulTelements.each Do | Element |
...
end
If you run a list of lists 4 at the command prompt, the query parameter is "Ruby SOAP", the result is as follows:
$ ruby Google_Search.rb Ruby Language
Estimated Result Count: 206000
http://www.ruby-lang.org/EN/: Ruby b> Home Page
... b> Japanese Page if you can read this oriental Language b>,
you can get more information about ruby b>. site ... b>
http://slashdot.org/developers/01/08/11/2211254.shtml: Slashdot | Progra ...
... b> Programming in The Ruby b> language b>. ... b>
This Discussion Has
BEEN Archived. No New Comments Can Be Posted.
http://dev.rubycentral.com/faq/rubyfaq.html: The Ruby Language b> FAQ
The Ruby b> language b> FAQ. Originally by: ShuGo
Maeda.
NOW maintained by Dave Thomas with help from Andy hunt. ... b>
(Note: This table is the result after the translator's folding, the original content may exceed the width, affect reading)
It is also very simple to write a SOAP server side in Ruby. What you need to do is to publish interface objects and then put these objects on the servlet of the SOAP server. You have written objects don't need to know anything in SOAP. For example, inventory 5 indicates a simple class, there is a simple method Double, receives a parameter, returns two of this parameter added.
Listing 5: The File Doubler.rb, The Ruby SOAP DOUBLING CLASS
Class Doubler
Def Double (Arg)
Arg arg
end
end
To access this method in the SOAP server, we need to assemble it into the namespace of the SOAP server. In Ruby, the easiest way is to use the web server toolbox Webrick. Combine SOAPLET.RB this servlet code (in the SAMPLES / Webrick directory of the SOAP4R package), we can use few code to implement a complete SOAP server, see Listing 6.
Listing 6: a Ruby SOAP Server
1 Require 'Webrick'
2 Require 'SOAPLET'3 Require' Doubler '
4
5 Server = Webrick :: httpserver.new (: port => 2001)
6
7 SOAPLET = SOAP :: WebricksoAplet.new
8 SOAPLET.AddServant ('URN: DoublerService', Doubler.new)
9 Server.mount ("/ Doubler", SOAPLET)
10
11 TRAP ("int") {server.shutdown}
12 Server.Start
The top three lines are simply introduced into the needed library, SOAPLET, DOUBLE class, etc. The 5th line is to create a web server must have step (this port 2001), and the 7th line creates a SOAPLET (a servlet that sends the SOAP request to an object). Chapter 8 helps this servlet to a Doubler object, line 9 maps this SOAPLET to Web Server / Doubler. Chapter 12 launches the server process, but what is the use of the 11th? When you start a Webrick service, this server program will process the request, return the result, but we think our server program is completely closed, and the 11th line is to complete this function, this line registers a handling SIGINT for Server The processor of the signal, when receiving such a signal, call the SHUTDOWN method of Server, under most operating systems, the Control-c will generate a sigint signal, so we can control our web server at the command prompt.
We can test the server side with the SOAP client program in Listing 7 below. Chain 10 also demonstrates another feature of Rescue: This statement first tries to convert parameters (passing the time as a string) into an integer. If the conversion fails, RESCUE will capture this exception and return the original parameter. Such a result is that the parameters of the Double method can be intellectual and string, let us see what it will be different when the client is running.
$ ruby soap_client.rb 12 Wiki
twenty four
Wikiwiki
$
The incoming parameter is 12, the result we get 24, when we pass the string wiki, we get wikiwiki. The polymorphism of the type in Ruby also propagates to the SOAP interface. Because the Double method is Arg Arg, if the parameter is integer, return two numbers added, if the parameter is a string, then the result of the two string connectivity is returned.
Listing 7: The Doubler SOAP Client
1 Require "SOAP / Driver"
2
3 SVR = 'http:// localhost: 2001 / doubler'
4 ns = 'URN: DoublerService'
5
6 soap = soap :: driver.new (NIL, NIL, NS, SVR)
7 soap.addmethod ('Double', 'Arg')
8
9 Argv.each Do | Arg |
10 arg = (Integer (Arg) Rescue Arg)
11 puts soap.double (arg)
12 END