Sha256: 6f0377332cb3752a72717eaf201d4669c23704e543fb5bf2b964088dce280a2b

Contents?: true

Size: 1.58 KB

Versions: 26

Compression:

Stored size: 1.58 KB

Contents

require 'nokogiri'

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

26 entries across 26 versions & 1 rubygems

Version Path
daengine-0.6.4 lib/daengine/taxonomy_parser.rb
daengine-0.6.3 lib/daengine/taxonomy_parser.rb
daengine-0.6.2 lib/daengine/taxonomy_parser.rb
daengine-0.6.1 lib/daengine/taxonomy_parser.rb
daengine-0.6 lib/daengine/taxonomy_parser.rb
daengine-0.5.13 lib/daengine/taxonomy_parser.rb