Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • xmlrpc/server.rb

XMLRPC::BasicServer

Public Class Methods

new(class_delim=".") click to toggle source
 
               # File xmlrpc/server.rb, line 170
def initialize(class_delim=".")
  @handler = []
  @default_handler = nil
  @service_hook = nil

  @class_delim = class_delim
  @create = nil
  @parser = nil

  add_multicall     if Config::ENABLE_MULTICALL
  add_introspection if Config::ENABLE_INTROSPECTION
end
            

Public Instance Methods

add_handler(prefix, obj_or_signature=nil, help=nil, &block) click to toggle source
 
               # File xmlrpc/server.rb, line 183
def add_handler(prefix, obj_or_signature=nil, help=nil, &block)
  if block_given?
    # proc-handler
    @handler << [prefix, block, obj_or_signature, help]
  else
    if prefix.kind_of? String
      # class-handler
      raise ArgumentError, "Expected non-nil value" if obj_or_signature.nil?
      @handler << [prefix + @class_delim, obj_or_signature]
    elsif prefix.kind_of? XMLRPC::Service::BasicInterface
      # class-handler with interface
      # add all methods
      @handler += prefix.get_methods(obj_or_signature, @class_delim)
    else
      raise ArgumentError, "Wrong type for parameter 'prefix'"
    end
  end
  self
end
            
add_introspection() click to toggle source
 
               # File xmlrpc/server.rb, line 263
def add_introspection
  add_handler("system.listMethods",%w(array), "List methods available on this XML-RPC server") do
    methods = []
    @handler.each do |name, obj|
      if obj.kind_of? Proc
        methods << name
      else
        obj.class.public_instance_methods(false).each do |meth|
          methods << "#{name}#{meth}"
        end
      end
    end
    methods
  end

  add_handler("system.methodSignature", %w(array string), "Returns method signature") do |meth|
    sigs = []
    @handler.each do |name, obj, sig|
      if obj.kind_of? Proc and sig != nil and name == meth
        if sig[0].kind_of? Array
          # sig contains multiple signatures, e.g. [["array"], ["array", "string"]]
          sig.each {|s| sigs << s}
        else
          # sig is a single signature, e.g. ["array"]
          sigs << sig
        end
      end
    end
    sigs.uniq! || sigs  # remove eventually duplicated signatures
  end

  add_handler("system.methodHelp", %w(string string), "Returns help on using this method") do |meth|
    help = nil
    @handler.each do |name, obj, sig, hlp|
      if obj.kind_of? Proc and name == meth
        help = hlp
        break
      end
    end
    help || ""
  end

  self
end
            
add_multicall() click to toggle source
 
               # File xmlrpc/server.rb, line 221
def add_multicall
  add_handler("system.multicall", %w(array array), "Multicall Extension") do |arrStructs|
    unless arrStructs.is_a? Array
      raise XMLRPC::FaultException.new(ERR_MC_WRONG_PARAM, "system.multicall expects an array")
    end

    arrStructs.collect {|call|
      if call.is_a? Hash
        methodName = call["methodName"]
        params     = call["params"]

        if params.nil?
          multicall_fault(ERR_MC_MISSING_PARAMS, "Missing params")
        elsif methodName.nil?
          multicall_fault(ERR_MC_MISSING_METHNAME, "Missing methodName")
        else
          if methodName == "system.multicall"
            multicall_fault(ERR_MC_RECURSIVE_CALL, "Recursive system.multicall forbidden")
          else
            unless params.is_a? Array
              multicall_fault(ERR_MC_WRONG_PARAM_PARAMS, "Parameter params have to be an Array")
            else
              ok, val = call_method(methodName, *params)
              if ok
                # correct return value
                [val]
              else
                # exception
                multicall_fault(val.faultCode, val.faultString)
              end
            end
          end
        end

      else
        multicall_fault(ERR_MC_EXPECTED_STRUCT, "system.multicall expected struct")
      end
    }
  end # end add_handler
  self
end
            
get_default_handler() click to toggle source
 
               # File xmlrpc/server.rb, line 212
def get_default_handler
  @default_handler
end
            
get_service_hook() click to toggle source
 
               # File xmlrpc/server.rb, line 203
def get_service_hook
  @service_hook
end
            
process(data) click to toggle source
 
               # File xmlrpc/server.rb, line 310
def process(data)
  method, params = parser().parseMethodCall(data)
  handle(method, *params)
end
            
set_default_handler(&handler) click to toggle source
 
               # File xmlrpc/server.rb, line 216
def set_default_handler (&handler)
  @default_handler = handler
  self
end
            
set_service_hook(&handler) click to toggle source
 
               # File xmlrpc/server.rb, line 207
def set_service_hook(&handler)
  @service_hook = handler
  self
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus