Class: Oddb2xml::ZurroseExtractor

Inherits:
Extractor
  • Object
show all
Defined in:
lib/oddb2xml/extractor.rb

Instance Attribute Summary

Attributes inherited from Extractor

#xml

Instance Method Summary collapse

Methods inherited from Extractor

#correct_code

Constructor Details

#initialize(dat, extended = false) ⇒ ZurroseExtractor



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/oddb2xml/extractor.rb', line 457

def initialize(dat, extended = false)
  @@extended = extended
  FileUtils.makedirs(WorkDir)
  @@error_file ||= File.open(File.join(WorkDir, "duplicate_ean13_from_zur_rose.txt"), 'wb+:ISO-8859-14')
  @@items_without_ean13s ||= 0
  @@duplicated_ean13s ||= 0
  @@zur_rose_items ||= 0
  if dat
    if File.exists?(dat)
      @io = File.open(dat, 'rb:ISO-8859-14')
    else
      @io = StringIO.new(dat)
    end
    @io
  else
     nil
  end
end

Instance Method Details

#to_hashObject



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/oddb2xml/extractor.rb', line 475

def to_hash
  data = {}
  while line = @io.gets
    line = line.encode('utf-8').gsub("\u0089", "‰").gsub("\u0092", '’').gsub("\u0096", '-').chomp
    next if line =~ /(ad us\.* vet)|(\(vet\))/i
    if @@extended
      next unless line =~ /(\d{13})(\d{1})$/
    else
      next unless line =~ /(7680\d{9})(\d{1})$/
    end
    pharma_code = line[3..9]
    if $1.to_s == '0000000000000'
      @@items_without_ean13s += 1
      ean13 = '000000' + pharma_code # dummy ean13
    else
      ean13 = $1.to_s
    end
    if data[ean13]
      @@error_file.puts "Duplicate ean13 #{ean13} in line \nact: #{line.chomp}\norg: #{data[ean13][:line]}"
      @@items_without_ean13s -= 1
      @@duplicated_ean13s += 1
      next
    end

    data[ean13] = {
      :line   => line.chomp,
      :ean   => ean13,
      :vat   => line[96],
      :description => line[10..59].sub(/\s+$/, ''),
      :additional_desc => '',
      :pharmacode => pharma_code,
      :price => sprintf("%.2f", line[60,6].gsub(/(\d{2})$/, '.\1').to_f),
      :pub_price => sprintf("%.2f", line[66,6].gsub(/(\d{2})$/, '.\1').to_f),
      :type => :nonpharma,
      :cmut => line[2],
    }
    @@zur_rose_items += 1
  end if @io
  if defined?(@@extended) and @@extended
    @@error_file.puts get_error_msg
  end
  @@error_file.close
  @@error_file = nil
  data
end