Class: BrDanfe::XML

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

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XML

Returns a new instance of XML.



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

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

Instance Method Details

#[](xpath) ⇒ Object



11
12
13
14
# File 'lib/br_danfe/xml.rb', line 11

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

#collect(ns, tag) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/br_danfe/xml.rb', line 16

def collect(ns, tag)
  result = []
  # With namespace
  begin
    @xml.xpath("//#{ns}:#{tag}").each do |det|
      result << yield(det)
    end
  rescue StandardError
    # Without namespace
    @xml.xpath("//#{tag}").each do |det|
      result << yield(det)
    end
  end
  result
end

#css(xpath) ⇒ Object



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

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

#version_is_310_or_newer?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/br_danfe/xml.rb', line 32

def version_is_310_or_newer?
  @xml.css('infNFe').attr('versao').to_s.to_f >= 3.10
end