Class: XML::XMLRPC::Parser

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xml/libxml/xmlrpc/parser.rb

Overview

Spec-compliant XML-RPC parser. Converts XML-RPC types to native Ruby types.

Overview:

<?xml version="1.0"?>
  <methodResponse>
      <params>
          <param>
              <value><string>South Dakota</string></value>
          </param>
      </params>
  </methodResponse> 

xml = XML::XMLRPC::Parser.new(IO or String object)
xml[0] == "South Dakota"

Notes:

* Structs and Arrays are Hashes and Arrays respectively.
* Base64 is auto-decoded.
* Any interpreter-level (as opposed to syntax-level or exception
  handling) crash you see in ruby is the fault of libxml, not this code.
* In a case where you're parsing a methodCall request, the method
  attribute will have data. In the case where you parse a response, method
  will be nil.

Defined Under Namespace

Modules: Call, ValueParser Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Parser

Takes a String or IO object, which contains a response or call.

Parses the document immediately.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 56

def initialize(io)
    @string = ""

    if io.kind_of? String
        @string = io
    elsif io.kind_of? IO or io.kind_of? StringIO # stupid StringIO
        @string = io.read
    else
        raise ParserError, "Argument to new must be String or IO"
    end

    @params = []
    @method = nil
    self.parse!

    @params.freeze
    @method.freeze
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



49
50
51
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 49

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



48
49
50
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 48

def params
  @params
end

Instance Method Details

#[](x) ⇒ Object

Obtain param x, where x is an integer.

Same as:

xml.params[x]


99
100
101
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 99

def [](x)
    @params[x]
end

#eachObject

Iterate over each parameter.



107
108
109
110
111
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 107

def each
    @params.each do |x|
        yield x
    end
end

#parse!Object

Parses the document. Should not be required – Parser#new already does this for you.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 79

def parse!
    document = XML::Parser.string(@string).parse
    node = document.root
    case node.name
    when "methodCall"
        @method, @params = Parser::Call.parse(node)
    when "methodResponse"
        @params = Parser::Response.parse(node)
    else
        raise ParserError, "XMLRPC is invalid - no call or response"
    end
end