Class: XMLRPC::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlrpc/create.rb

Overview

Creates XML-RPC call/response documents

Instance Method Summary collapse

Constructor Details

#initialize(xml_writer = nil) ⇒ Create

Returns a new instance of Create.



108
109
110
# File 'lib/xmlrpc/create.rb', line 108

def initialize(xml_writer = nil)
  @writer = xml_writer || Config::DEFAULT_WRITER.new
end

Instance Method Details

#methodCall(name, *params) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/xmlrpc/create.rb', line 113

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) ⇒ Object

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.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/xmlrpc/create.rb', line 147

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