Class: RubyDanfe::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_danfe/xml.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XML

Returns a new instance of XML.



17
18
19
# File 'lib/ruby_danfe/xml.rb', line 17

def initialize(xml)
  @xml = Nokogiri::XML(xml)
end

Instance Method Details

#[](xpath) ⇒ Object



21
22
23
24
# File 'lib/ruby_danfe/xml.rb', line 21

def [](xpath)
  node = @xml.css(xpath)
  return node ? node.text : ""
end

#attrib(node, attrib) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ruby_danfe/xml.rb', line 50

def attrib(node, attrib)
  begin
    return @xml.css(node).attr(attrib).text
  rescue
    ""
  end
end

#collect(ns, tag, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_danfe/xml.rb', line 34

def collect(ns, tag, &block)
  result = []
  # Tenta primeiro com uso de namespace
  begin
    @xml.xpath("//#{ns}:#{tag}").each do |det|
      result << yield(det)
    end
  rescue
    # Caso dĂȘ erro, tenta sem
    @xml.xpath("//#{tag}").each do |det|
      result << yield(det)
    end
  end
  result
end

#css(xpath) ⇒ Object



3
4
5
# File 'lib/ruby_danfe/xml.rb', line 3

def css(xpath)
  @xml.css(xpath)
end

#regex_string(search_string, regex) ⇒ Object



12
13
14
15
# File 'lib/ruby_danfe/xml.rb', line 12

def regex_string(search_string, regex)
  doc = Nokogiri::HTML(search_string)
  return doc.xpath(regex)      
end

#renderObject



26
27
28
29
30
31
32
# File 'lib/ruby_danfe/xml.rb', line 26

def render
  if @xml.at_css('infNFe/ide')
    RubyDanfe.render @xml.to_s, :danfe
  else
    RubyDanfe.render @xml.to_s, :dacte
  end
end

#xpath(regex) ⇒ Object



7
8
9
10
# File 'lib/ruby_danfe/xml.rb', line 7

def xpath(regex)
  doc = Nokogiri::HTML(@xml.to_s)
  return doc.xpath(regex)
end