Class: Nfse::Pdf::XML

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

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XML

Returns a new instance of XML.



28
29
30
# File 'lib/danfe/xml.rb', line 28

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

Instance Method Details

#[](xpath) ⇒ Object



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

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

#attrib(node, attrib) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/danfe/xml.rb', line 82

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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/danfe/xml.rb', line 51

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



14
15
16
# File 'lib/danfe/xml.rb', line 14

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

#inject(ns, tag, acc, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/danfe/xml.rb', line 67

def inject(ns, tag, acc, &block)
  # Tenta primeiro com uso de namespace
  begin
    @xml.xpath("//#{ns}:#{tag}").each do |det|
      acc = yield(acc, det)
    end
  rescue
    # Caso dê erro, tenta sem
    @xml.xpath("//#{tag}").each do |det|
      acc = yield(acc, det)
    end
  end
  acc
end

#regex_string(search_string, regex) ⇒ Object



23
24
25
26
# File 'lib/danfe/xml.rb', line 23

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

#renderObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/danfe/xml.rb', line 37

def render
  if @xml.at_css('infNFe/ide')
    RubyDanfe.render @xml.to_s, :danfe
  elsif @xml.at_css('InfNfse/Numero')
    RubyDanfe.render @xml.to_s, :danfse
  else
    if @xml.at_css('CTeOS')
      RubyDanfe.render @xml.to_s, :dacteos
    else
      RubyDanfe.render @xml.to_s, :dacte
    end
  end
end

#xpath(regex) ⇒ Object



18
19
20
21
# File 'lib/danfe/xml.rb', line 18

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