removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionwebservice / lib / action_web_service / client / xmlrpc_client.rb
blob42b5c5d4f9abce79f23449dd197a636758c308f3
1 require 'uri'
2 require 'xmlrpc/client'
4 module ActionWebService # :nodoc:
5   module Client # :nodoc:
7     # Implements XML-RPC client support
8     #
9     # ==== Example Usage
10     #
11     #   class BloggerAPI < ActionWebService::API::Base
12     #     inflect_names false
13     #     api_method :getRecentPosts, :returns => [[Blog::Post]]
14     #   end
15     #
16     #   blog = ActionWebService::Client::XmlRpc.new(BloggerAPI, "http://.../RPC", :handler_name => "blogger")
17     #   posts = blog.getRecentPosts
18     class XmlRpc < Base
20       # Creates a new web service client using the XML-RPC protocol.
21       #
22       # +api+ must be an ActionWebService::API::Base derivative, and
23       # +endpoint_uri+ must point at the relevant URL to which protocol requests
24       # will be sent with HTTP POST.
25       #
26       # Valid options:
27       # [<tt>:handler_name</tt>]    If the remote server defines its services inside special
28       #                             handler (the Blogger API uses a <tt>"blogger"</tt> handler name for example),
29       #                             provide it here, or your method calls will fail
30       def initialize(api, endpoint_uri, options={})
31         @api = api
32         @handler_name = options[:handler_name]
33         @protocol = ActionWebService::Protocol::XmlRpc::XmlRpcProtocol.new
34         @client = XMLRPC::Client.new2(endpoint_uri, options[:proxy], options[:timeout])
35       end
37       protected
38         def perform_invocation(method_name, args)
39           method = @api.api_methods[method_name.to_sym]
40           if method.expects && method.expects.length != args.length
41             raise(ArgumentError, "#{method.public_name}: wrong number of arguments (#{args.length} for #{method.expects.length})")
42           end
43           args = method.cast_expects(args.dup) rescue args
44           if method.expects
45             method.expects.each_with_index{ |type, i| args[i] = @protocol.value_to_xmlrpc_wire_format(args[i], type) }
46           end
47           ok, return_value = @client.call2(public_name(method_name), *args)
48           return (method.cast_returns(return_value.dup) rescue return_value) if ok
49           raise(ClientError, "#{return_value.faultCode}: #{return_value.faultString}")
50         end
52         def public_name(method_name)
53           public_name = @api.public_api_method_name(method_name)
54           @handler_name ? "#{@handler_name}.#{public_name}" : public_name
55         end
56     end
57   end
58 end