Ruby 2.3.0 リファレンスマニュアル > ライブラリ一覧 > xmlrpc/clientライブラリ > XMLRPC::Clientクラス

class XMLRPC::Client

クラスの継承リスト: XMLRPC::Client < Object < Kernel < BasicObject

要約

Class XMLRPC::Client provides remote procedure calls to a XML-RPC server.

After setting the connection-parameters with XMLRPC::Client.new which creates a new XMLRPC::Client instance, you can execute a remote procedure by sending the XMLRPC::Client#call or XMLRPC::Client#call2 message to this new instance. The given parameters indicate which method to call on the remote-side and of course the parameters for the remote procedure.

require "xmlrpc/client"

server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
begin
  param = server.call("michael.add", 4, 5)
  puts "4 + 5 = #{param}"
rescue XMLRPC::FaultException => e
  puts "Error:"
  puts e.faultCode
  puts e.faultString
end

or

require "xmlrpc/client"

server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
ok, param = server.call2("michael.add", 4, 5)
if ok then
  puts "4 + 5 = #{param}"
else
  puts "Error:"
  puts param.faultCode
  puts param.faultString
end

目次

特異メソッド
new new2 new_from_uri new3 new_from_hash
インスタンスメソッド
call call2 call2_async call_async multicall2_async multicall_async proxy2_async proxy_async cookie cookie= http_header_extra http_header_extra= http_last_response multicall multicall2 password timeout user password= timeout= user= proxy proxy2 set_parser set_writer

特異メソッド

new(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil, user=nil, password=nil, use_ssl=false, timeout =nil)[permalink][rdoc]

Creates an object which represents the remote XML-RPC server on the given host host. If the server is CGI-based, path is the path to the CGI-script, which will be called, otherwise (in the case of a standalone server) path should be "/RPC2". port is the port on which the XML-RPC server listens. If proxy_host is given, then a proxy server listening at proxy_host is used. proxy_port is the port of the proxy server.

Default values for host, path and port are 'localhost', '/RPC2' and '80' respectively using SSL '443'.

If user and password are given, each time a request is send, a Authorization header is send. Currently only Basic Authentification is implemented no Digest.

If use_ssl is set to true, comunication over SSL is enabled. Note, that you need the SSL package from RAA installed.

Parameter timeout is the time to wait for a XML-RPC response, defaults to 30.

new2(uri, proxy=nil, timeout=nil)[permalink][rdoc]
new_from_uri(uri, proxy=nil, timeout=nil)
uri

URI specifying protocol (http or https), host, port, path, user and password. Example: https://user:password@host:port/path

proxy

Is of the form "host:port".

timeout

Defaults to 30.

new3(hash={})[permalink][rdoc]
new_from_hash(hash={})

Parameter hash has following case-insensitive keys:

Calls XMLRPC::Client.new with the corresponding values.

インスタンスメソッド

call(method, *args)[permalink][rdoc]

Invokes the method named method with the parameters given by args on the XML-RPC server. The parameter method is converted into a String and should be a valid XML-RPC method-name. Each parameter of args must be of one of the following types, where Hash, Struct and Array can contain any of these listed ((:types:)):

The method returns the return-value from the RPC ((-stands for Remote Procedure Call-)). The type of the return-value is one of the above shown, only that a Bignum is only allowed when it fits in 32-bit and that a XML-RPC (('dateTime.iso8601')) type is always returned as a XMLRPC::DateTime object and a Struct is never returned, only a Hash, the same for a Symbol, where always a String is returned. A XMLRPC::Base64 is returned as a String from xmlrpc4r version 1.6.1 on.

If the remote procedure returned a fault-structure, then a XMLRPC::FaultException exception is raised, which has two accessor-methods faultCode and faultString of type Integer and String.

call2(method, *args)[permalink][rdoc]

The difference between this method and XMLRPC::Client#call is, that this method do ((*not*)) raise a XMLRPC::FaultException exception. The method returns an array of two values. The first value indicates if the second value is a return-value (true) or an object of type XMLRPC::FaultException. Both are explained in XMLRPC::Client#call.

Simple to remember: The "2" in "call2" denotes the number of values it returns.

call_async(...)[permalink][rdoc]
call2_async(...)
multicall_async(...)
multicall2_async(...)
proxy_async(...)
proxy2_async(...)

In contrast to corresponding methods without "_async", these can be called concurrently and use for each request a new connection, where the non-asynchronous counterparts use connection-alive (one connection for all requests) if possible.

Note, that you have to use Threads to call these methods concurrently. The following example calls two methods concurrently:

Thread.new {
  p client.call_async("michael.add", 4, 5)
}

Thread.new {
  p client.call_async("michael.div", 7, 9)
}
cookie=()

Get and set the HTTP Cookie header.

http_header_extra[permalink][rdoc]

Access the via XMLRPC::Client#http_header_extra= assigned header.

http_header_extra=()[permalink][rdoc]

Set extra HTTP headers that are included in the request.

http_last_response[permalink][rdoc]

Returns the Net::HTTPResponse object of the last RPC.

multicall(*methods)[permalink][rdoc]

You can use this method to execute several methods on a XMLRPC server which supports the multi-call extension. Example:

s.multicall(
  ['michael.add', 3, 4],
  ['michael.sub', 4, 5]
)
# => [7, -1]
multicall2(*methods)[permalink][rdoc]

Same as XMLRPC::Client#multicall, but returns like XMLRPC::Client#call2 two parameters instead of raising an XMLRPC::FaultException.

timeout[permalink][rdoc]
user
password

Return the corresponding attributes.

timeout=()[permalink][rdoc]
user=()
password=()

Set the corresponding attributes.

proxy(prefix, *args)[permalink][rdoc]

Returns an object of class XMLRPC::Client::Proxy, initialized with prefix and args. A proxy object returned by this method behaves like XMLRPC::Client#call, i.e. a call on that object will raise a XMLRPC::FaultException when a fault-structure is returned by that call.

proxy2(prefix, *args)[permalink][rdoc]

Almost the same like XMLRPC::Client#proxy only that a call on the returned XMLRPC::Client::Proxy object behaves like XMLRPC::Client#call2, i.e. a call on that object will return two parameters.

set_parser(parser)[permalink][rdoc]

Sets the XML parser to use for parsing XML documents. Should be an instance of a class from module XMLRPC::XMLParser. If this method is not called, then XMLRPC::Config::DEFAULT_PARSER is used.

set_writer(writer)[permalink][rdoc]

Sets the XML writer to use for generating XML output. Should be an instance of a class from module XMLRPC::XMLWriter. If this method is not called, then XMLRPC::Config::DEFAULT_WRITER is used.