Module: RAWS::XML::Nokogiri

Defined in:
lib/raws/xml/nokogiri.rb

Class Method Summary collapse

Class Method Details

._parse(doc, params = {}, ret = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/raws/xml/nokogiri.rb', line 6

def self._parse(doc, params={}, ret={})
  multiple = params[:multiple] || []
  unpack   = params[:unpack]   || []

  name = nil
  doc.children.each do |tag|
    name = tag.name

    unless ret[name].is_a? Array
      if ret.key?(name)
        ret[name] = [ret[name]]
      elsif multiple.include? name
        ret[name] = []
      end
    end

    case tag.child
    when nil
      ret[name] = nil
    when ::Nokogiri::XML::Text
      if ret.key? name
        ret[name] << tag.content
      else
        ret[name] = tag.content
      end
    else
      if ret.key? name
        ret[name] << {}
        _parse(tag, params, ret[name].last)
      else
        ret[name] = {}
        _parse(tag, params, ret[name])
      end
    end
  end
  ret[name] = RAWS::XML.unpack_attrs(ret[name]) if unpack.include?(name)

  ret
end

.parse(xml, params = {}) ⇒ Object



46
47
48
# File 'lib/raws/xml/nokogiri.rb', line 46

def self.parse(xml, params={})
  _parse(::Nokogiri::XML.parse(xml), params)
end