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:



17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 17

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:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 63

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 84

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)


31
32
33
34
35
36
37
38
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 31

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



44
45
46
47
48
49
50
# File 'lib/rack/rpc/endpoint/xmlrpc.rb', line 44

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