Class: Rack::RPC::Endpoint::XMLRPC::Server

Inherits:
XMLRPC::BasicServer
  • Object
show all
Defined in:
lib/rack/rpc/endpoint/xmlrpc.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(server, options = {}) ⇒ Server

Returns a new instance of Server.

Parameters:



26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 26

def initialize(server, options = {})
  @server = server
  super()
  add_multicall     unless options[:multicall]     == false
  add_introspection unless options[:introspection] == false
  add_capabilities  unless options[:capabilities]  == false
  server.class.rpc.each do |rpc_name, method_name|
    add_handler(rpc_name, nil, nil, &server.method(method_name))
  end
end

Instance Method Details

#add_capabilities(options = {}) ⇒ void

This method returns an undefined value.

Implements the ‘system.getCapabilities` standard method, enabling clients to determine whether a given capability is supported by this server.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :faults_interop (Boolean) — default: true

    whether to indicate support for the XMLRPC-EPI Specification for Fault Code Interoperability

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 72

def add_capabilities(options = {})
  add_handler('system.getCapabilities', %w(struct), '') do
    capabilities = {}
    unless options[:faults_interop] == false
      capabilities['faults_interop'] = {
        'specUrl'     => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
        'specVersion' => 20010516,
      }
    end
    capabilities
  end
  self
end

#error_response(code, message) ⇒ String

Create a valid error response for a given code and message

Parameters:

  • error (Int)

    code

  • error (String)

    message

Returns:

  • (String)

    response xml string



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 93

def error_response(code, message)
  xml = Builder::XmlMarkup.new
  xml.instruct! :xml, :version=>"1.0"
  xml.methodResponse{ 
    xml.fault {
      xml.value{
        xml.struct{
          xml.member{
            xml.name('faultCode')
            xml.value{
              xml.int(code)
            }
          }
          xml.member{
            xml.name('faultString')
            xml.value{
              xml.string(message)
            } 
          }
        } 
      } 
    } 
  }
end

#execute(request) ⇒ Rack::Response

Parameters:

  • request (Rack::Request)

Returns:

  • (Rack::Response)


40
41
42
43
44
45
46
47
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 40

def execute(request)
  @server.request = request # Store the request so it can be accessed from the server methods
  request_body = request.body.read
  request_body.force_encoding(Encoding::UTF_8) if request_body.respond_to?(:force_encoding) # Ruby 1.9+
  Rack::Response.new([process(request_body)], 200, {
    'Content-Type' => (request.content_type || CONTENT_TYPE).to_s,
  })
end

#process(request_body) ⇒ Object

Process requests and ensure errors are handled properly

Parameters:

  • request (String)

    body



53
54
55
56
57
58
59
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 53

def process(request_body)
  begin
    super(request_body)
  rescue RuntimeError => e
    error_response(-32500, "application error - #{e.message}")
  end
end