Class: Sox::XML::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sox/xml_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/sox/xml_parser.rb', line 6

def initialize(data)
  @data = data
end

Instance Method Details

#parse(&block) ⇒ Object



10
11
12
13
# File 'lib/sox/xml_parser.rb', line 10

def parse(&block)
  @callback = block
  parse_data @data
end

#parser(parser, parseErrorOccurred: parse_error) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sox/xml_parser.rb', line 19

def parser(parser, didStartElement:element, namespaceURI:uri, qualifiedName:name, attributes:attrs)
  element = element.to_sym
  parent = @result.last

  child = {}
  attrs.each { |k, v| child[k.to_sym] = v } if attrs

  existing = parent[element]

  if existing
    values = existing.is_a?(Array) ? existing << child : [existing, child]
    parent[element] = values
  else
    parent[element] = child
  end

  @result << child
end

#parserDidEndDocument(parser) ⇒ Object



46
47
48
# File 'lib/sox/xml_parser.rb', line 46

def parserDidEndDocument(parser)
  @callback.call(@result.first) if @callback
end

#parserDidStartDocument(parser) ⇒ Object



15
16
17
# File 'lib/sox/xml_parser.rb', line 15

def parserDidStartDocument(parser)
  @result = [{}]
end