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

Constructor Details

#initialize(dat, extended = false, artikelstamm = false) ⇒ ZurroseExtractor



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/oddb2xml/extractor.rb', line 502

def initialize(dat, extended = false, artikelstamm = false)
  @@extended = extended
  @artikelstamm = artikelstamm
  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



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/oddb2xml/extractor.rb', line 521

def to_hash
  data = {}
  while line = @io.gets
    ean13 = "-1"
    line = Oddb2xml.patch_some_utf8(line).chomp
    # next unless /(7680\d{9})(\d{1})$/.match(line) # Skip non pharma
    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
      next if @artikelstamm && pharma_code.to_i == 0
      ean13 = Oddb2xml::FAKE_GTIN_START + pharma_code.to_s unless @artikelstamm
    else
      ean13 = $1
    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

    pexf = sprintf("%.2f", line[60,6].gsub(/(\d{2})$/, '.\1').to_f)
    ppub =  sprintf("%.2f", line[66,6].gsub(/(\d{2})$/, '.\1').to_f)
    next if  @artikelstamm && /^113/.match(line) && /^7680/.match(ean13)
    next if  @artikelstamm && /^113/.match(line) && ppub.eql?('0.0') && pexf.eql?('0.0')
    next unless ean13
    key = ean13
    key =  (Oddb2xml::FAKE_GTIN_START + pharma_code.to_s) if ean13.to_i <= 0 # dummy ean13
    data[key] = {
      :data_origin   => 'zur_rose',
      :line   => line.chomp,
      :ean13 => ean13,
      :clag  => line[73],
      :vat   => line[96],
      :description => line[10..59].sub(/\s+$/, ''),
      :quantity => '',
      :pharmacode => pharma_code,
      :price => pexf,
      :pub_price => ppub,
      :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