Sha256: 5fdec6a7fb4c0d404646237315959eec57e161d32c6775451c98166fce5ea653

Contents?: true

Size: 1.56 KB

Versions: 36

Compression:

Stored size: 1.56 KB

Contents

module Daengine::TaxonomyParser

  @@logger = nil
  @error_msg = []

  def self.logger=(some_logger)
    @@logger = some_logger
    self
  end

  def self.log(args)
    @@logger.error(args) unless @@logger.blank?
  end

  def self.parse_taxonomy_file(file)
    @error_msg=[]
    time do
      doc = Nokogiri::XML(file) { |config| config.strict }

      TaxonomyTerm.purge! if doc.xpath('//taxonomy/facet')
      doc.xpath('//taxonomy/facet').each do |i|
        parse_taxonomy_term(i)
      end
    end
  # rescue
  #   p "ERROR!  Unable to parse / update taxonomy file! #{$!.backtrace.join('\n')}"
  #   Daengine.log($!.backtrace.join('\n'), "error")
  end

  def self.parse_taxonomy_term(entity)
    category = TaxonomyTerm.new
    category.label = entity.xpath('label').inner_html
    category.term_id = entity.xpath('@id')
    alternate_name = entity.xpath('altname/@type')
    unless alternate_name.blank?
      a_text = entity.xpath('altname').inner_html
      category.term_type = {alternate_name[0].value => a_text}
    end
    entity.xpath('term').each do |j|
      a_term = parse_taxonomy_term(j)
      # a_term.parent_term = category
      category.child_terms << a_term
    end
    # end
    # begin
      category.save!
    # rescue Exception => e
      #puts "--**Exception**-- #{e}"
      # p "Failed to add record #{category.try(:label)}, #{category.try(:errors).try(:full_messages)}"
    # end
    category
  end

  def self.time
    start = Time.now
    yield
    self.log "elapsed time was #{Time.now - start}"
  end

end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
daengine-0.5.10 lib/daengine/taxonomy_parser.rb
daengine-0.5.9 lib/daengine/taxonomy_parser.rb
daengine-0.5.8 lib/daengine/taxonomy_parser.rb
daengine-0.5.0 lib/daengine/taxonomy_parser.rb
daengine-0.4.9 lib/daengine/taxonomy_parser.rb
daengine-0.4.8 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.9 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.8 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.7 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.6 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.5 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.3 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.2 lib/daengine/taxonomy_parser.rb
daengine-0.4.7.1 lib/daengine/taxonomy_parser.rb
daengine-0.4.7 lib/daengine/taxonomy_parser.rb
daengine-0.4.6.5 lib/daengine/taxonomy_parser.rb
daengine-0.4.6.4 lib/daengine/taxonomy_parser.rb
daengine-0.4.6.3 lib/daengine/taxonomy_parser.rb
daengine-0.4.6.2 lib/daengine/taxonomy_parser.rb
daengine-0.4.6 lib/daengine/taxonomy_parser.rb