Skip to content

Commit

Permalink
Ensure that transfer.dat lines have always the correct line length of 97
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Jan 15, 2018
1 parent 4785199 commit 9c3cc26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
1 change: 1 addition & 0 deletions History.txt
@@ -1,6 +1,7 @@
=== 2.4.9 / 15.01.2018

* Adapt to new link of epha atc.csv
* Ensure that transfer.dat lines have always the correct line length of 97

=== 2.4.8 / 20.11.2017

Expand Down
31 changes: 13 additions & 18 deletions lib/oddb2xml/builder.rb
Expand Up @@ -1182,20 +1182,10 @@ def format_date(date_str, len=7)
end
date[0,len]
end
def format_name(name)
if RUBY_VERSION.to_f < 1.9 # for multibyte chars length support
chars = name.scan(/./mu).to_a
diff = DAT_LEN[:ABEZ] - chars.length
if diff > 0
chars += Array.new(diff, ' ')
elsif diff < 0
chars = chars[0,DAT_LEN[:ABEZ]]
end
chars.to_s
else
name = name[0,DAT_LEN[:ABEZ]] if name.length > DAT_LEN[:ABEZ]
"%-#{DAT_LEN[:ABEZ]}s" % name
end

# The migel name must be always 50 chars wide and in ISO 8859-1 format
def format_name(name, length)
("%-#{length}s" % name)[0..length-1]
end
def build_dat
prepare_articles
Expand Down Expand Up @@ -1248,7 +1238,12 @@ def build_dat
price_exf = sprintf('%06i', (pac[:prices][:exf_price][:price].to_f*100).to_i) if pac[:prices][:exf_price] and pac[:prices][:exf_price][:price]
price_public = sprintf('%06i', (pac[:prices][:pub_price][:price].to_f*100).to_i) if pac[:prices][:pub_price] and pac[:prices][:pub_price][:price]
end
row << format_name(abez)
row << format_name(Oddb2xml.patch_some_utf8(abez), DAT_LEN[:ABEZ])
if price_exf.to_s.length > DAT_LEN[:PRMO] ||
price_public.to_s.length > DAT_LEN[:PRPU]
puts "Price exfactory #{price_exf} or public #{price_public} is too high to be added into transfer.dat"
break
end
row << "%#{DAT_LEN[:PRMO]}s" % (price_exf ? price_exf.to_s : ('0' * DAT_LEN[:PRMO]))
row << "%#{DAT_LEN[:PRPU]}s" % (price_public ? price_public.to_s : ('0' * DAT_LEN[:PRPU]))
row << "%#{DAT_LEN[:CKZL]}s" % if (@lppvs[ean])
Expand Down Expand Up @@ -1303,9 +1298,9 @@ def build_with_migel_dat
obj[:desc_de].to_s + " " +
(obj[:quantity] ? obj[:quantity] : '')
).gsub(/"/, '')
row << format_name(abez)
row << "%#{DAT_LEN[:PRMO]}s" % ('0' * DAT_LEN[:PRMO])
row << "%#{DAT_LEN[:PRPU]}s" % ('0' * DAT_LEN[:PRPU])
row << format_name(Oddb2xml.patch_some_utf8(abez), DAT_LEN[:ABEZ])
row << '0' * DAT_LEN[:PRMO]
row << '0' * DAT_LEN[:PRPU]
row << "%#{DAT_LEN[:CKZL]}s" % '3' # sl_entry and lppv
row << "%#{DAT_LEN[:CLAG]}s" % '0'
row << "%#{DAT_LEN[:CBGG]}s" % '0'
Expand Down
2 changes: 1 addition & 1 deletion lib/oddb2xml/extractor.rb
Expand Up @@ -470,7 +470,7 @@ def initialize(dat, extended = false)
def to_hash
data = {}
while line = @io.gets
line = line.encode('utf-8').gsub("\u0089", "‰").gsub("\u0092", '’').gsub("\u0096", '-').chomp
line = Oddb2xml.patch_some_utf8(line)
next if line =~ /(ad us\.* vet)|(\(vet\))/i
if @@extended
next unless line =~ /(\d{13})(\d{1})$/
Expand Down
27 changes: 13 additions & 14 deletions lib/oddb2xml/util.rb
Expand Up @@ -20,25 +20,24 @@ def Oddb2xml.calc_checksum(str)
@atc_csv_origin = 'http://download.epha.ch/data/atc/atc.csv'
@atc_csv_content = {}

def Oddb2xml.patch_some_utf8(line)
begin
line.gsub("\u0089", "‰").gsub("\u0092", '’').gsub("\u0096", '-').gsub("\u2013",'-').gsub("\u201D", '"').chomp
rescue => error
puts "#{error}: in #{line}"
line
end
end

def Oddb2xml.convert_to_8859_1(line)
begin
# We want to ignore lines which are not really UTF-8 encoded
return line.encode('ISO-8859-1')
ausgabe = Oddb2xml.patch_some_utf8(line).encode('ISO-8859-1')
ausgabe.encode('ISO-8859-1')
rescue => error
ausgabe = ''
0.upto(line.size-1).each do |idx|
begin
if line[idx].ord == 8211
ausgabe += '-'
else
ausgabe += line[idx].encode('ISO-8859-1')
end
rescue => error
puts "#{error}: in #{line} at #{idx}"
end
end
puts "#{error}: in #{line}"
require 'pry'; binding.pry
end
ausgabe.encode('ISO-8859-1')
end

def Oddb2xml.add_epha_changes_for_ATC(iksnr, atc_code)
Expand Down

0 comments on commit 9c3cc26

Please sign in to comment.