In Files

  • xmlrpc/create.rb

XMLRPC::Create

Creates XML-RPC call/response documents

Public Class Methods

new(xml_writer = nil) click to toggle source
 
               # File xmlrpc/create.rb, line 106
def initialize(xml_writer = nil)
  @writer = xml_writer || Config::DEFAULT_WRITER.new
end
            

Public Instance Methods

methodCall(name, *params) click to toggle source
 
               # File xmlrpc/create.rb, line 111
def methodCall(name, *params)
  name = name.to_s

  if name !~ /[a-zA-Z0-9_.:\/]+/
    raise ArgumentError, "Wrong XML-RPC method-name"
  end

  parameter = params.collect do |param|
    @writer.ele("param", conv2value(param))
  end

  tree = @writer.document(
           @writer.pi("xml", 'version="1.0"'),
           @writer.ele("methodCall",
             @writer.tag("methodName", name),
             @writer.ele("params", *parameter)
           )
         )

  @writer.document_to_str(tree) + "\n"
end
            
methodResponse(is_ret, *params) click to toggle source

Generates a XML-RPC methodResponse document

When is_ret is false then the params array must contain only one element, which is a structure of a fault return-value.

When is_ret is true then a normal return-value of all the given params is created.

 
               # File xmlrpc/create.rb, line 145
def methodResponse(is_ret, *params)

  if is_ret
    resp = params.collect do |param|
      @writer.ele("param", conv2value(param))
    end

    resp = [@writer.ele("params", *resp)]
  else
    if params.size != 1 or params[0] === XMLRPC::FaultException
      raise ArgumentError, "no valid fault-structure given"
    end
    resp = @writer.ele("fault", conv2value(params[0].to_h))
  end


  tree = @writer.document(
           @writer.pi("xml", 'version="1.0"'),
           @writer.ele("methodResponse", resp)
         )

  @writer.document_to_str(tree) + "\n"
end
            

Private Instance Methods

conv2value(param) click to toggle source

Converts a Ruby object into a XML-RPC <value> tag

 
               # File xmlrpc/create.rb, line 176
def conv2value(param) # :doc:

    val = case param
    when Fixnum, Bignum
      # XML-RPC's int is 32bit int, and Fixnum also may be beyond 32bit
      if Config::ENABLE_BIGINT
        @writer.tag("i4", param.to_s)
      else
        if param >= -(2**31) and param <= (2**31-1)
          @writer.tag("i4", param.to_s)
        else
          raise "Bignum is too big! Must be signed 32-bit integer!"
        end
      end
    when TrueClass, FalseClass
      @writer.tag("boolean", param ? "1" : "0")

    when Symbol
      @writer.tag("string", param.to_s)

    when String
      @writer.tag("string", param)

    when NilClass
      if Config::ENABLE_NIL_CREATE
        @writer.ele("nil")
      else
        raise "Wrong type NilClass. Not allowed!"
      end

    when Float
      raise "Wrong value #{param}. Not allowed!" unless param.finite?
      @writer.tag("double", param.to_s)

    when Struct
      h = param.members.collect do |key|
        value = param[key]
        @writer.ele("member",
          @writer.tag("name", key.to_s),
          conv2value(value)
        )
      end

      @writer.ele("struct", *h)

    when Hash
      # TODO: can a Hash be empty?

      h = param.collect do |key, value|
        @writer.ele("member",
          @writer.tag("name", key.to_s),
          conv2value(value)
        )
      end

      @writer.ele("struct", *h)

    when Array
      # TODO: can an Array be empty?
      a = param.collect {|v| conv2value(v) }

      @writer.ele("array",
        @writer.ele("data", *a)
      )

    when Time, Date, ::DateTime
      @writer.tag("dateTime.iso8601", param.strftime("%Y%m%dT%H:%M:%S"))

    when XMLRPC::DateTime
      @writer.tag("dateTime.iso8601",
        format("%.4d%02d%02dT%02d:%02d:%02d", *param.to_a))

    when XMLRPC::Base64
      @writer.tag("base64", param.encoded)

    else
      if Config::ENABLE_MARSHALLING and param.class.included_modules.include? XMLRPC::Marshallable
        # convert Ruby object into Hash
        ret = {"___class___" => param.class.name}
        param.instance_variables.each {|v|
          name = v[1..-1]
          val = param.instance_variable_get(v)

          if val.nil?
            ret[name] = val if Config::ENABLE_NIL_CREATE
          else
            ret[name] = val
          end
        }
        return conv2value(ret)
      else
        ok, pa = wrong_type(param)
        if ok
          return conv2value(pa)
        else
          raise "Wrong type!"
        end
      end
    end

    @writer.ele("value", val)
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.