Class: ChupakabraTools::XmlParser

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

Instance Method Summary collapse

Constructor Details

#initialize(node, &block) ⇒ XmlParser

Returns a new instance of XmlParser.



3
4
5
6
7
8
# File 'lib/chupakabra_tools/xml_parser.rb', line 3

def initialize(node, &block)
    @node = node
    @node.each do
        self.instance_eval &block
    end
end

Instance Method Details

#attribute(attribute) ⇒ Object



30
31
32
# File 'lib/chupakabra_tools/xml_parser.rb', line 30

def attribute(attribute)
    @node.attribute(attribute)
end

#for_element(name, &block) ⇒ Object



34
35
36
37
# File 'lib/chupakabra_tools/xml_parser.rb', line 34

def for_element(name, &block)
    return unless self.name == name and is_start?
    self.instance_eval &block
end

#inner_xmlObject



14
15
16
# File 'lib/chupakabra_tools/xml_parser.rb', line 14

def inner_xml
    @node.inner_xml.strip
end

#inside_element(name = nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chupakabra_tools/xml_parser.rb', line 39

def inside_element(name=nil, &block)
    return if @node.self_closing?
    return unless name.nil? or (self.name == name and is_start?)

    name = @node.name
    depth = @node.depth

    @node.each do
        return if self.name == name and is_end? and @node.depth == depth
        self.instance_eval &block
    end
end

#is_end?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/chupakabra_tools/xml_parser.rb', line 26

def is_end?
    @node.node_type == ::Nokogiri::XML::Reader::TYPE_END_ELEMENT
end

#is_start?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chupakabra_tools/xml_parser.rb', line 22

def is_start?
    @node.node_type == ::Nokogiri::XML::Reader::TYPE_ELEMENT
end

#nameObject



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

def name
    @node.name
end

#outer_xmlObject



18
19
20
# File 'lib/chupakabra_tools/xml_parser.rb', line 18

def outer_xml
    @node.outer_xml.strip
end