Module: Daengine::TaxonomyParser

Defined in:
lib/daengine/taxonomy_parser.rb

Constant Summary collapse

@@logger =
nil

Class Method Summary collapse

Class Method Details

.log(args) ⇒ Object



11
12
13
# File 'lib/daengine/taxonomy_parser.rb', line 11

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

.logger=(some_logger) ⇒ Object



6
7
8
9
# File 'lib/daengine/taxonomy_parser.rb', line 6

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

.parse_taxonomy_file(file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/daengine/taxonomy_parser.rb', line 15

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

.parse_taxonomy_term(entity) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/daengine/taxonomy_parser.rb', line 30

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

.timeObject



54
55
56
57
58
# File 'lib/daengine/taxonomy_parser.rb', line 54

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