Sha256: 5aa9b5775c1622539afb3d26bbaffe355235a961485d926db95404052eff7194

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module RubyDanfe
  class XML
    def css(xpath)
      @xml.css(xpath)
    end

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

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

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

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

    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

    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

    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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_danfe-1.15.4 lib/ruby_danfe/xml.rb
ruby_danfe-1.15.2 lib/ruby_danfe/xml.rb
ruby_danfe-1.15.1 lib/ruby_danfe/xml.rb
ruby_danfe-1.15.0 lib/ruby_danfe/xml.rb