<commit_before>require "cfdi_parser/version"
require 'nokogiri'

module CfdiParser
  class CfdiParser
    attr_accessor :doc

    def initialize(cfdi)
      @doc = Nokogiri::XML(cfdi)
      @namespaces = generate_namespaces
    end

    def version
      doc.xpath("//cfdi:Comprobante",namespaces).attribute('version').value rescue nil
    end

    def total
      doc.xpath("//cfdi:Comprobante",namespaces).attribute('total').value rescue nil
    end

    def subtotal
      doc.xpath("//cfdi:Comprobante",namespaces).attribute('subTotal').value rescue nil
    end

    private

    def namespaces
      @namespaces
    end

    def generate_namespaces
      namespaces = doc.collect_namespaces
      namespaces_names = {}
      namespaces.each_pair do |key, value|
        namespaces_names[key.sub(/^xmlns:/, '')] = value
      end
      namespaces_names
    end
  end
end<commit_msg>Small exploratory refactors.<commit_after>require "cfdi_parser/version"
require 'nokogiri'

module CfdiParser
  class CfdiParser
    attr_accessor :doc

    def initialize(cfdi)
      @doc = Nokogiri::XML(cfdi)
    end

    def version
      doc.xpath("//cfdi:Comprobante", namespaces).attribute('version').value rescue nil
    end

    def total
      doc.xpath("//cfdi:Comprobante", namespaces).attribute('total').value rescue nil
    end

    def subtotal
      doc.xpath("//cfdi:Comprobante", namespaces).attribute('subTotal').value rescue nil
    end

    private

    def namespaces
      @namespaces ||= generate_namespaces
    end

    def generate_namespaces
      doc.collect_namespaces.each_pair.with_object({}) do |(k, v), h|
        h[k.sub(/^xmlns:/, '')] = v
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10669828097213989, 'fraction_numerical': 0.0, 'mean_word_length': 2.9531615925058547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2051514', 'n_tokens_mistral': 562, 'n_tokens_neox': 528, 'n_words': 112}