class RubyDanfe::XML

Public Class Methods

new(xml) click to toggle source
# File lib/ruby_danfe/xml.rb, line 17
def initialize(xml)
  @xml = Nokogiri::XML(xml)
end

Public Instance Methods

[](xpath) click to toggle source
# File lib/ruby_danfe/xml.rb, line 21
def [](xpath)
  node = @xml.css(xpath)
  return node ? node.text : ""
end
attrib(node, attrib) click to toggle source
# File lib/ruby_danfe/xml.rb, line 71
def attrib(node, attrib)
  begin
    return @xml.css(node).attr(attrib).text
  rescue
    ""
  end
end
collect(ns, tag) { |det| ... } click to toggle source
# File lib/ruby_danfe/xml.rb, line 40
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) click to toggle source
# File lib/ruby_danfe/xml.rb, line 3
def css(xpath)
  @xml.css(xpath)
end
inject(ns, tag, acc) { |acc, det| ... } click to toggle source
# File lib/ruby_danfe/xml.rb, line 56
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) click to toggle source
# File lib/ruby_danfe/xml.rb, line 12
def regex_string(search_string, regex)
  doc = Nokogiri::HTML(search_string)
  return doc.xpath(regex)
end
render() click to toggle source
# File lib/ruby_danfe/xml.rb, line 26
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) click to toggle source
# File lib/ruby_danfe/xml.rb, line 7
def xpath(regex)
  doc = Nokogiri::HTML(@xml.to_s)
  return doc.xpath(regex)
end