Sha256: 7065172abbe0f4347ff413e90ea375e0716a63a316b573cc79f1b72939da70b5

Contents?: true

Size: 1.79 KB

Versions: 34

Compression:

Stored size: 1.79 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
      begin
        doc = Nokogiri::XML(file) { |config| config.strict }
      rescue Nokogiri::XML::SyntaxError => e
        raise "ERROR: TaxonomyProcessor: XML parse error :: #{e}"
      end
      TaxonomyTerm.purge!
      doc.xpath('//taxonomy/facet').map do |i|
        parse_taxonomy_term(i)
      end
      log_txt = "Failed to save/update following Taxonomy terms in database:\n"
      @error_msg.each do |taxonomy_term|
        log_txt = log_txt + "> #{taxonomy_term}\n"
      end
      Daengine.log(log_txt, "warn") if !@error_msg.empty?
    end
  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
    my_terms = entity.xpath('term').map
    unless my_terms.nil?
      entity.xpath('term').map do |j|
        a_term = parse_taxonomy_term(j)
        category.child_terms << a_term
      end
    end
    begin
      category.save!
    rescue Exception => e
      #puts "--**Exception**-- #{e}"
      @error_msg << "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

34 entries across 34 versions & 1 rubygems

Version Path
daengine-0.3.8.2 lib/daengine/taxonomy_parser.rb
daengine-0.3.8.1 lib/daengine/taxonomy_parser.rb
daengine-0.3.8 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.8 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.7 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.5 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.4 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.3 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.2 lib/daengine/taxonomy_parser.rb
daengine-0.3.7.1 lib/daengine/taxonomy_parser.rb
daengine-0.3.7 lib/daengine/taxonomy_parser.rb
daengine-0.3.5 lib/daengine/taxonomy_parser.rb
daengine-0.3.4 lib/daengine/taxonomy_parser.rb
daengine-0.3.3 lib/daengine/taxonomy_parser.rb
daengine-0.3.2 lib/daengine/taxonomy_parser.rb
daengine-0.3.1 lib/daengine/taxonomy_parser.rb
daengine-0.3 lib/daengine/taxonomy_parser.rb
daengine-0.2.19 lib/daengine/taxonomy_parser.rb
daengine-0.2.18 lib/daengine/taxonomy_parser.rb
daengine-0.2.17 lib/daengine/taxonomy_parser.rb