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



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

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

Instance Method Details

#to_hashObject



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
578
579
580
581
# File 'lib/oddb2xml/extractor.rb', line 524

def to_hash
  data = {}
  if @io
    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 /(ad us\.* vet)|(\(vet\))/i.match?(line)
      if @@extended
        next unless (match_data = line.match(/(\d{13})(\d{1})$/))
      else
        next unless (match_data = line.match(/(7680\d{9})(\d{1})$/))
      end
      pharma_code = line[3..9]
      if match_data[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 = match_data[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) && 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
  end
  if defined?(@@extended) && @@extended
    @@error_file.puts get_error_msg
  end
  @@error_file.close
  @@error_file = nil
  data
end