Class: XMLRPC::Create (Ruby 2.3.7)
Description: Class : XMLRPC::Create - Ruby 2.3.7

- Home
- Core 2.3.7
- Std-lib 2.3.7
- Downloads

Support for the Ruby 2.3 series has ended. See here for reference.

### Home Classes Methods

### In Files

- xmlrpc/create.rb

### Parent

Object

### Methods

- ::new
- #conv2value
- #methodCall
- #methodResponse

### Class/Module Index

Quicksearch

- NQXML
- NQXML::Node
- XMLRPC
- XMLRPC::Base64
- XMLRPC::BasicServer
- XMLRPC::CGIServer
- XMLRPC::Client
- XMLRPC::Client::Proxy
- XMLRPC::Config
- XMLRPC::Convert
- XMLRPC::Create
- XMLRPC::DateTime
- XMLRPC::FaultException
- XMLRPC::Marshal
- XMLRPC::Marshallable
- XMLRPC::ModRubyServer
- XMLRPC::ParseContentType
- XMLRPC::ParserWriterChooseMixin
- XMLRPC::Server
- XMLRPC::Service
- XMLRPC::Service::BasicInterface
- XMLRPC::Service::Interface
- XMLRPC::Service::PublicInstanceMethodsInterface
- XMLRPC::WEBrickServlet
- XMLRPC::XMLParser
- XMLRPC::XMLParser::AbstractStreamParser
- XMLRPC::XMLParser::AbstractTreeParser
- XMLRPC::XMLParser::LibXMLStreamParser
- XMLRPC::XMLParser::LibXMLStreamParser::LibXMLStreamListener
- XMLRPC::XMLParser::NQXMLParser
- XMLRPC::XMLParser::NQXMLStreamParser
- XMLRPC::XMLParser::NQXMLStreamParser::XMLRPCParser
- XMLRPC::XMLParser::NQXMLTreeParser
- XMLRPC::XMLParser::REXMLStreamParser
- XMLRPC::XMLParser::REXMLStreamParser::StreamListener
- XMLRPC::XMLParser::StreamParserMixin
- XMLRPC::XMLParser::XMLParser
- XMLRPC::XMLParser::XMLScanStreamParser
- XMLRPC::XMLParser::XMLScanStreamParser::XMLScanParser
- XMLRPC::XMLParser::XMLStreamParser
- XMLRPC::XMLParser::XMLTreeParser
- XMLRPC::XMLWriter
- XMLRPC::XMLWriter::Abstract
- XMLRPC::XMLWriter::Simple
- XMLRPC::XMLWriter::XMLParser

No matching classes.

# XMLRPC::Create

Creates XML-RPC call/response documents

### Public Class Methods

new(xml_writer = nil) click to toggle source

                   # File xmlrpc/create.rb, line 105
    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 110
    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 144
    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 175
    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