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, String, or StringIO 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

Class Method 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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 68

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

    if self.class.debug
        $stderr.puts "Parsing:\n#{@string}";
    end

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

    @params.freeze
    @method.freeze
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



61
62
63
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 61

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



60
61
62
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 60

def params
  @params
end

Class Method Details

.debugObject

get the debugging state



54
55
56
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 54

def self.debug
    @debug
end

.debug=(x) ⇒ Object

set the debugging state



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

def self.debug=(x)
    @debug = x
end

Instance Method Details

#[](x) ⇒ Object

Obtain param x, where x is an integer.

Same as:

xml.params[x]


128
129
130
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 128

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

#eachObject

Iterate over each parameter.



136
137
138
139
140
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 136

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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/xml/libxml/xmlrpc/parser.rb', line 95

def parse!

    if @string.empty?
        raise ParserError, 
            "String is empty - libxml-ruby would normally crash your program here."
    end

    if Object.const_defined?("LibXML")
        klass = LibXML::XML
    else
        klass = XML
    end


    document = klass::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