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