oddb2xml 2.7.1 → 2.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.standard.yml +2 -0
  4. data/Gemfile +3 -3
  5. data/History.txt +8 -0
  6. data/README.md +1 -1
  7. data/Rakefile +24 -23
  8. data/bin/check_artikelstamm +11 -11
  9. data/bin/compare_v5 +23 -23
  10. data/bin/oddb2xml +14 -13
  11. data/lib/oddb2xml.rb +1 -1
  12. data/lib/oddb2xml/builder.rb +1070 -1038
  13. data/lib/oddb2xml/calc.rb +232 -233
  14. data/lib/oddb2xml/chapter_70_hack.rb +38 -32
  15. data/lib/oddb2xml/cli.rb +252 -236
  16. data/lib/oddb2xml/compare.rb +70 -59
  17. data/lib/oddb2xml/compositions_syntax.rb +448 -430
  18. data/lib/oddb2xml/compressor.rb +20 -20
  19. data/lib/oddb2xml/downloader.rb +153 -127
  20. data/lib/oddb2xml/extractor.rb +302 -289
  21. data/lib/oddb2xml/options.rb +34 -35
  22. data/lib/oddb2xml/parslet_compositions.rb +263 -269
  23. data/lib/oddb2xml/semantic_check.rb +39 -33
  24. data/lib/oddb2xml/util.rb +163 -163
  25. data/lib/oddb2xml/version.rb +1 -1
  26. data/lib/oddb2xml/xml_definitions.rb +32 -33
  27. data/oddb2xml.gemspec +31 -32
  28. data/spec/artikelstamm_spec.rb +111 -110
  29. data/spec/builder_spec.rb +489 -505
  30. data/spec/calc_spec.rb +552 -593
  31. data/spec/check_artikelstamm_spec.rb +26 -26
  32. data/spec/cli_spec.rb +173 -174
  33. data/spec/compare_spec.rb +9 -11
  34. data/spec/composition_syntax_spec.rb +390 -409
  35. data/spec/compressor_spec.rb +48 -48
  36. data/spec/data/transfer.dat +1 -0
  37. data/spec/data_helper.rb +47 -49
  38. data/spec/downloader_spec.rb +247 -260
  39. data/spec/extractor_spec.rb +171 -159
  40. data/spec/galenic_spec.rb +233 -256
  41. data/spec/options_spec.rb +116 -119
  42. data/spec/parslet_spec.rb +833 -861
  43. data/spec/spec_helper.rb +154 -153
  44. data/test_options.rb +39 -42
  45. data/tools/win_fetch_cacerts.rb +2 -3
  46. metadata +19 -3
@@ -1,31 +1,30 @@
1
- # encoding: utf-8
2
-
3
- require 'zlib'
4
- require 'minitar'
5
- require 'zip'
1
+ require "zlib"
2
+ require "minitar"
3
+ require "zip"
6
4
 
7
5
  module Oddb2xml
8
- class Compressor
6
+ class Compressor
9
7
  include Archive::Tar
10
8
  attr_accessor :contents
11
- def initialize(prefix='oddb', options={})
9
+ def initialize(prefix = "oddb", options = {})
12
10
  @options = options
13
- @options[:compress_ext] ||= 'tar.gz'
14
- @options[:format] ||= :xml
15
- @compress_file = "#{prefix}_#{@options[:format].to_s}_" + Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}")
16
- # @compress_file = File.join(WorkDir, "#{prefix}_#{@options[:format].to_s}_" +
17
- #Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}"))
11
+ @options[:compress_ext] ||= "tar.gz"
12
+ @options[:format] ||= :xml
13
+ @compress_file = "#{prefix}_#{@options[:format]}_" + Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}")
14
+ # @compress_file = File.join(WORK_DIR, "#{prefix}_#{@options[:format].to_s}_" +
15
+ # Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}"))
18
16
  @contents = []
19
17
  super()
20
18
  end
19
+
21
20
  def finalize!
22
- if @contents.empty? and @contents.size == 0
21
+ if @contents.empty? && (@contents.size == 0)
23
22
  return false
24
23
  end
25
24
  begin
26
25
  case @compress_file
27
26
  when /\.tar\.gz$/
28
- tgz = Zlib::GzipWriter.new(File.open(@compress_file, 'wb'))
27
+ tgz = Zlib::GzipWriter.new(File.open(@compress_file, "wb"))
29
28
  Minitar.pack(@contents, tgz)
30
29
  when /\.zip$/
31
30
  Zip::File.open(@compress_file, Zip::File::CREATE) do |zip|
@@ -35,18 +34,19 @@ module Oddb2xml
35
34
  end
36
35
  end
37
36
  end
38
- if File.exists? @compress_file
37
+ if File.exist? @compress_file
39
38
  puts "#{__LINE__}: @compress_file"
40
39
  @contents.each do |file|
41
40
  @tmpfile = file
42
- puts "#{__LINE__}: @tmpfile"
43
- FileUtils.rm(file) if file && File.exists?(file)
41
+ puts "#{__LINE__}: @tmpfile"
42
+ FileUtils.rm(file) if file && File.exist?(file)
44
43
  end
45
44
  end
46
- rescue Errno::ENOENT, StandardError => e
47
- return false
45
+ rescue Errno::ENOENT
46
+ puts "Unable to compress #{@compress_file}"
47
+ raise RuntimeError
48
48
  end
49
- return true
49
+ true
50
50
  end
51
51
  end
52
52
  end
@@ -1,20 +1,19 @@
1
- # encoding: utf-8
1
+ require "net/ntlm/version" # needed to avoid error: uninitialized constant Net::NTLM::VERSION
2
+ require "rubyntlm"
3
+ require "mechanize"
4
+ require "zip"
5
+ require "savon"
6
+ require "open-uri"
2
7
 
3
- require 'net/ntlm/version' # needed to avoid error: uninitialized constant Net::NTLM::VERSION
4
- require 'rubyntlm'
5
- require 'mechanize'
6
- require 'zip'
7
- require 'savon'
8
- require 'open-uri'
9
-
10
- SkipMigelDownloader = true # https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls
8
+ SKIP_MIGEL_DOWNLOADER = true # https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls
11
9
 
12
10
  module Oddb2xml
13
11
  module DownloadMethod
14
12
  private
15
- def download_as(file, option='w+')
16
- tempFile = File.join(WorkDir, File.basename(file))
17
- @file2save = File.join(Downloads, File.basename(file))
13
+
14
+ def download_as(file, option = "w+")
15
+ temp_file = File.join(WORK_DIR, File.basename(file))
16
+ @file2save = File.join(DOWNLOADS, File.basename(file))
18
17
  report_download(@url, @file2save)
19
18
  data = nil
20
19
  if Oddb2xml.skip_download(file)
@@ -28,44 +27,49 @@ module Oddb2xml
28
27
  rescue => error
29
28
  puts "error #{error} while fetching #{@url}"
30
29
  ensure
31
- io.close if io and !io.closed? # win
32
- Oddb2xml.download_finished(tempFile)
30
+ io.close if io && !io.closed? # win
31
+ Oddb2xml.download_finished(temp_file)
33
32
  end
34
33
  end
35
- return data
34
+ data
36
35
  end
37
36
  end
37
+
38
38
  class Downloader
39
- attr_reader :type, :agent, :url; :file2save
40
- def initialize(options={}, url=nil)
41
- @options = options
42
- @url = url
39
+ attr_reader :type, :agent, :url, :file2save
40
+ def initialize(options = {}, url = nil)
41
+ @options = options
42
+ @url = url
43
43
  @retry_times = 3
44
44
  HTTPI.log = false # disable httpi warning
45
45
  Oddb2xml.log "Downloader from #{@url} for #{self.class}"
46
46
  init
47
47
  end
48
+
48
49
  def report_download(url, file)
49
50
  Oddb2xml.log sprintf("%-20s: download_as %-24s from %s",
50
- self.class.to_s.split('::').last,
51
- File.basename(file),
52
- url)
51
+ self.class.to_s.split("::").last,
52
+ File.basename(file),
53
+ url)
53
54
  end
55
+
54
56
  def init
55
57
  @agent = Mechanize.new
56
- @agent.user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0'
57
- @agent.redirect_ok = true
58
- @agent.redirection_limit = 5
58
+ @agent.user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0"
59
+ @agent.redirect_ok = true
60
+ @agent.redirection_limit = 5
59
61
  @agent.follow_meta_refresh = true
60
- if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i and
61
- ENV['SSL_CERT_FILE'].nil?
62
+ if RUBY_PLATFORM =~ (/mswin|mingw|bccwin|cygwin/i) &&
63
+ ENV["SSL_CERT_FILE"].nil?
62
64
  cert_store = OpenSSL::X509::Store.new
63
- cert_store.add_file(File.expand_path('../../../tools/cacert.pem', __FILE__))
65
+ cert_store.add_file(File.expand_path("../../../tools/cacert.pem", __FILE__))
64
66
  @agent.cert_store = cert_store
65
67
  end
66
68
  @agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
67
69
  end
70
+
68
71
  protected
72
+
69
73
  def retrievable?
70
74
  if @retry_times > 0
71
75
  sleep 5
@@ -75,29 +79,35 @@ module Oddb2xml
75
79
  false
76
80
  end
77
81
  end
82
+
78
83
  def read_xml_from_zip(target, zipfile)
79
- Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exists?(zipfile)}"
84
+ Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exist?(zipfile)}"
80
85
  if Oddb2xml.skip_download?
81
86
  entry = nil
82
- Dir.glob(File.join(Downloads, '*')).each { |name| if target.match(name) then entry = name; break end }
87
+ Dir.glob(File.join(DOWNLOADS, "*")).each do |name|
88
+ if target.match(name)
89
+ entry = name
90
+ break
91
+ end
92
+ end
83
93
  if entry
84
- dest = "#{Downloads}/#{File.basename(entry)}"
94
+ dest = "#{DOWNLOADS}/#{File.basename(entry)}"
85
95
  @file2save = dest
86
- if File.exists?(dest)
96
+ if File.exist?(dest)
87
97
  Oddb2xml.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
88
98
  return IO.read(dest)
89
99
  else
90
100
  Oddb2xml.log "read_xml_from_zip could not read #{dest}"
91
101
  end
92
102
  else
93
- Oddb2xml.log "read_xml_from_zip could not find #{target.to_s}"
103
+ Oddb2xml.log "read_xml_from_zip could not find #{target}"
94
104
  end
95
105
  end
96
- xml = ''
97
- if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i
98
- Zip::File.open(zipfile) do |zipFile|
99
- zipFile.each do |entry|
100
- if entry.name =~ target
106
+ xml = ""
107
+ if RUBY_PLATFORM.match?(/mswin|mingw|bccwin|cygwin/i)
108
+ Zip::File.open(zipfile) do |a_zip_file|
109
+ a_zip_file.each do |entry|
110
+ if entry.name&.match?(target)
101
111
  Oddb2xml.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
102
112
  io = entry.get_input_stream
103
113
  until io.eof?
@@ -106,19 +116,19 @@ module Oddb2xml
106
116
  bytes = nil
107
117
  end
108
118
  io.close if io.respond_to?(:close)
109
- dest = "#{Downloads}/#{File.basename(entry.name)}"
110
- File.open(dest, 'w+') { |f| f.write xml }
119
+ dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
120
+ File.open(dest, "w+") { |f| f.write xml }
111
121
  Oddb2xml.log "read_xml_from_zip saved as #{dest}"
112
122
  end
113
123
  end
114
124
  end
115
125
  else
116
126
  Zip::File.foreach(zipfile) do |entry|
117
- if entry.name =~ target
127
+ if entry.name&.match?(target)
118
128
  Oddb2xml.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
119
- dest = "#{Downloads}/#{File.basename(entry.name)}"
129
+ dest = "#{DOWNLOADS}/#{File.basename(entry.name)}"
120
130
  entry.get_input_stream { |io| xml = io.read }
121
- File.open(dest, 'w+') { |f| f.write xml }
131
+ File.open(dest, "w+") { |f| f.write xml }
122
132
  Oddb2xml.log "read_xml_from_zip saved as #{dest}"
123
133
  end
124
134
  end
@@ -126,119 +136,129 @@ module Oddb2xml
126
136
  xml
127
137
  end
128
138
  end
129
- class MigelDownloader < Downloader
130
- include DownloadMethod
131
- def download
132
- @url ||= 'https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls'
133
- download_as('oddb2xml_files_nonpharma.xls', 'rb')
139
+ unless SKIP_MIGEL_DOWNLOADER
140
+ class MigelDownloader < Downloader
141
+ include DownloadMethod
142
+ def download
143
+ @url ||= "https://github.com/zdavatz/oddb2xml_files/raw/master/NON-Pharma.xls"
144
+ download_as("oddb2xml_files_nonpharma.xls", "rb")
145
+ end
134
146
  end
135
- end unless SkipMigelDownloader
147
+ end
136
148
  class EphaDownloader < Downloader
137
149
  include DownloadMethod
138
150
  def download
139
- @url ||= 'https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/interactions_de_utf8.csv'
140
- file = 'epha_interactions.csv'
141
- content = download_as(file, 'w+')
142
- FileUtils.rm_f(file, :verbose => false)
151
+ @url ||= "https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/interactions_de_utf8.csv"
152
+ file = "epha_interactions.csv"
153
+ content = download_as(file, "w+")
154
+ FileUtils.rm_f(file, verbose: false)
143
155
  content
144
156
  end
145
157
  end
158
+
146
159
  class LppvDownloader < Downloader
147
160
  include DownloadMethod
148
161
  def download
149
- @url ||= 'https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/LPPV.txt'
150
- download_as('oddb2xml_files_lppv.txt', 'w+')
162
+ @url ||= "https://raw.githubusercontent.com/zdavatz/oddb2xml_files/master/LPPV.txt"
163
+ download_as("oddb2xml_files_lppv.txt", "w+")
151
164
  end
152
165
  end
166
+
153
167
  class ZurroseDownloader < Downloader
154
168
  include DownloadMethod
155
169
  def download
156
- @url ||= 'http://pillbox.oddb.org/TRANSFER.ZIP'
157
- zipfile = File.join(WorkDir, 'transfer.zip')
170
+ @url ||= "http://pillbox.oddb.org/TRANSFER.ZIP"
171
+ zipfile = File.join(WORK_DIR, "transfer.zip")
158
172
  download_as(zipfile)
159
- dest = File.join(Downloads, 'transfer.dat')
160
- cmd = "unzip -o '#{zipfile}' -d '#{Downloads}'"
173
+ dest = File.join(DOWNLOADS, "transfer.dat")
174
+ cmd = "unzip -o '#{zipfile}' -d '#{DOWNLOADS}'"
161
175
  system(cmd)
162
176
  if @options[:artikelstamm]
163
- cmd = "iconv -f ISO8859-1 -t utf-8 -o #{dest.sub('.dat','.utf8')} #{dest}"
177
+ cmd = "iconv -f ISO8859-1 -t utf-8 -o #{dest.sub(".dat", ".utf8")} #{dest}"
164
178
  Oddb2xml.log(cmd)
165
179
  system(cmd)
166
180
  end
167
181
  # read file and convert it to utf-8
168
- File.open(dest, 'r:iso-8859-1:utf-8').read
182
+ File.open(dest, "r:iso-8859-1:utf-8").read
169
183
  ensure
170
184
  FileUtils.rm(zipfile) if File.exist?(dest) && File.exist?(zipfile)
171
185
  end
172
186
  end
187
+
173
188
  class MedregbmDownloader < Downloader
174
189
  include DownloadMethod
175
- def initialize(type=:company)
190
+ def initialize(type = :company)
176
191
  @type = type
177
- case @type
192
+ action = case @type
178
193
  when :company # betrieb
179
- action = 'CreateExcelListBetriebs'
180
- when :person # medizinalperson
181
- action = 'CreateExcelListMedizinalPersons'
194
+ "CreateExcelListBetriebs"
195
+ when :person # medizinalperson
196
+ "CreateExcelListMedizinalPersons"
182
197
  else
183
- action = ''
198
+ ""
184
199
  end
185
200
  url = "https://www.medregbm.admin.ch/Publikation/#{action}"
186
201
  super({}, url)
187
202
  end
203
+
188
204
  def download
189
- file = "medregbm_#{@type.to_s}.txt"
190
- download_as(file, 'w+:iso-8859-1:utf-8')
205
+ file = "medregbm_#{@type}.txt"
206
+ download_as(file, "w+:iso-8859-1:utf-8")
191
207
  report_download(@url, file)
192
- FileUtils.rm_f(file, :verbose => false) # we need it only in the download
208
+ FileUtils.rm_f(file, verbose: false) # we need it only in the download
193
209
  file
194
210
  end
195
211
  end
212
+
196
213
  class BagXmlDownloader < Downloader
197
214
  include DownloadMethod
198
215
  def init
199
216
  super
200
- @url ||= 'http://www.xn--spezialittenliste-yqb.ch/File.axd?file=XMLPublications.zip'
217
+ @url ||= "http://www.xn--spezialittenliste-yqb.ch/File.axd?file=XMLPublications.zip"
201
218
  end
219
+
202
220
  def download
203
- file = File.join(WorkDir, 'XMLPublications.zip')
221
+ file = File.join(WORK_DIR, "XMLPublications.zip")
204
222
  download_as(file)
205
223
  report_download(@url, file)
206
224
  if defined?(RSpec)
207
- src = File.join(Oddb2xml::SpecData, 'Preparations.xml')
208
- content = File.read(src)
209
- FileUtils.cp(src, File.join(Downloads, File.basename(file)))
225
+ src = File.join(Oddb2xml::SpecData, "Preparations.xml")
226
+ content = File.read(src)
227
+ FileUtils.cp(src, File.join(DOWNLOADS, File.basename(file)))
210
228
  else
211
- content = read_xml_from_zip(/Preparations.xml/, File.join(Downloads, File.basename(file)))
229
+ content = read_xml_from_zip(/Preparations.xml/, File.join(DOWNLOADS, File.basename(file)))
212
230
  end
213
231
  if @options[:artikelstamm]
214
232
  cmd = "xmllint --format --output Preparations.xml Preparations.xml"
215
233
  Oddb2xml.log(cmd)
216
234
  system(cmd)
217
235
  end
218
- FileUtils.rm_f(file, :verbose => false) unless defined?(RSpec)
236
+ FileUtils.rm_f(file, verbose: false) unless defined?(RSpec)
219
237
  content
220
238
  end
221
239
  end
240
+
222
241
  class RefdataDownloader < Downloader
223
- def initialize(options={}, type=:pharma)
224
- @type = (type == :pharma ? 'Pharma' : 'NonPharma')
242
+ def initialize(options = {}, type = :pharma)
243
+ @type = (type == :pharma ? "Pharma" : "NonPharma")
225
244
  url = "http://refdatabase.refdata.ch/Service/Article.asmx?WSDL"
226
245
  super(options, url)
227
246
  end
247
+
228
248
  def init
229
249
  config = {
230
- :log_level => :info,
231
- :log => false, # $stdout
232
- :raise_errors => true,
233
- :wsdl => @url
250
+ log_level: :info,
251
+ log: false, # $stdout
252
+ raise_errors: true,
253
+ wsdl: @url
234
254
  }
235
255
  @client = Savon::Client.new(config)
236
256
  end
257
+
237
258
  def download
238
259
  begin
239
- filename = "refdata_#{@type}.xml"
240
- @file2save = File.join(Downloads, "refdata_#{@type}.xml")
241
- soap = %(<?xml version="1.0" encoding="UTF-8"?>
260
+ @file2save = File.join(DOWNLOADS, "refdata_#{@type}.xml")
261
+ soap = %(<?xml version="1.0" encoding="UTF-8"?>
242
262
  <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://refdatabase.refdata.ch/Article_in" xmlns:ns2="http://refdatabase.refdata.ch/">
243
263
  <SOAP-ENV:Body>
244
264
  <ns2:DownloadArticleInput>
@@ -249,15 +269,15 @@ module Oddb2xml
249
269
  </ns1:ATYPE></ns2:DownloadArticleInput></SOAP-ENV:Body>
250
270
  )
251
271
  report_download(@url, @file2save)
252
- return IO.read(@file2save) if Oddb2xml.skip_download? and File.exists?(@file2save)
253
- FileUtils.rm_f(@file2save, :verbose => false)
254
- response = @client.call(:download, :xml => soap)
272
+ return IO.read(@file2save) if Oddb2xml.skip_download? && File.exist?(@file2save)
273
+ FileUtils.rm_f(@file2save, verbose: false)
274
+ response = @client.call(:download, xml: soap)
255
275
  if response.success?
256
- if xml = response.to_xml
257
- xml = File.read(File.join(Oddb2xml::SpecData, File.basename(@file2save))) if defined?(RSpec)
276
+ if (xml = response.to_xml)
277
+ xml = File.read(File.join(Oddb2xml::SpecData, File.basename(@file2save))) if defined?(RSpec)
258
278
  response = nil # win
259
- FileUtils.makedirs(Downloads)
260
- File.open(@file2save, 'w+') { |file| file.write xml }
279
+ FileUtils.makedirs(DOWNLOADS)
280
+ File.open(@file2save, "w+") { |file| file.write xml }
261
281
  if @options[:artikelstamm]
262
282
  cmd = "xmllint --format --output #{@file2save} #{@file2save}"
263
283
  Oddb2xml.log(cmd)
@@ -278,34 +298,36 @@ module Oddb2xml
278
298
  xml
279
299
  end
280
300
  end
301
+
281
302
  class SwissmedicDownloader < Downloader
282
- BASE_URL = 'https://www.swissmedic.ch'
303
+ BASE_URL = "https://www.swissmedic.ch"
283
304
  include DownloadMethod
284
- def initialize(type=:orphan, options = {})
285
- url = BASE_URL + '/swissmedic/de/home/services/listen_neu.html'
305
+ def initialize(type = :orphan, options = {})
306
+ url = BASE_URL + "/swissmedic/de/home/services/listen_neu.html"
286
307
  doc = Nokogiri::HTML(Oddb2xml.uri_open(url))
287
308
  @type = type
288
309
  @options = options
289
310
  case @type
290
311
  when :orphan
291
- @direct_url_link = BASE_URL + doc.xpath("//a").find{|x| /Humanarzneimittel mit Status Orphan Drug/.match(x.children.text) }.attributes['href'].value
312
+ @direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Humanarzneimittel mit Status Orphan Drug/.match(x.children.text) }.attributes["href"].value
292
313
  when :package
293
- @direct_url_link = BASE_URL + doc.xpath("//a").find{|x| /Zugelassene Packungen/.match(x.children.text) }.attributes['href'].value
314
+ @direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Zugelassene Packungen/.match(x.children.text) }.attributes["href"].value
294
315
  end
295
316
  end
317
+
296
318
  def download
297
- @file2save = File.join(Oddb2xml::WorkDir, "swissmedic_#{@type}.xlsx")
319
+ @file2save = File.join(Oddb2xml::WORK_DIR, "swissmedic_#{@type}.xlsx")
298
320
  report_download(@url, @file2save)
299
- if @options[:calc] and @options[:skip_download] and File.exists?(@file2save) and (Time.now-File.ctime(@file2save)).to_i < 24*60*60
321
+ if @options[:calc] && @options[:skip_download] && File.exist?(@file2save) && ((Time.now - File.ctime(@file2save)).to_i < 24 * 60 * 60)
300
322
  Oddb2xml.log "SwissmedicDownloader #{__LINE__}: Skip downloading #{@file2save} #{File.size(@file2save)} bytes"
301
- return File.expand_path(@file2save)
323
+ return File.expand_path(@file2save)
302
324
  end
303
325
  begin
304
- FileUtils.rm(File.expand_path(@file2save), :verbose => !defined?(RSpec)) if File.exists?(File.expand_path(@file2save))
326
+ FileUtils.rm(File.expand_path(@file2save), verbose: !defined?(RSpec)) if File.exist?(File.expand_path(@file2save))
305
327
  @url = @direct_url_link
306
- download_as(@file2save, 'w+')
328
+ download_as(@file2save, "w+")
307
329
  if @options[:artikelstamm]
308
- cmd = "ssconvert '#{@file2save}' '#{File.join(Downloads, File.basename(@file2save).sub(/\.xls.*/, '.csv'))}' 2> /dev/null"
330
+ cmd = "ssconvert '#{@file2save}' '#{File.join(DOWNLOADS, File.basename(@file2save).sub(/\.xls.*/, ".csv"))}' 2> /dev/null"
309
331
  Oddb2xml.log(cmd)
310
332
  system(cmd)
311
333
  end
@@ -315,41 +337,45 @@ module Oddb2xml
315
337
  ensure
316
338
  Oddb2xml.download_finished(@file2save, false)
317
339
  end
318
- return File.expand_path(@file2save)
340
+ File.expand_path(@file2save)
319
341
  end
320
342
  end
343
+
321
344
  class SwissmedicInfoDownloader < Downloader
322
345
  def init
323
346
  super
324
347
  @agent.ignore_bad_chunking = true
325
348
  @url ||= "http://download.swissmedicinfo.ch/Accept.aspx?ReturnUrl=%2f"
326
349
  end
350
+
327
351
  def download
328
- file = File.join(Downloads, "swissmedic_info.zip")
352
+ file = File.join(DOWNLOADS, "swissmedic_info.zip")
329
353
  report_download(@url, file)
330
- FileUtils.rm_f(file, :verbose => false) unless Oddb2xml.skip_download?
331
- begin
332
- response = nil
333
- if home = @agent.get(@url)
334
- form = home.form_with(:id => 'Form1')
335
- bttn = form.button_with(:name => 'ctl00$MainContent$btnOK')
336
- if page = form.submit(bttn)
337
- form = page.form_with(:id => 'Form1')
338
- bttn = form.button_with(:name => 'ctl00$MainContent$BtnYes')
339
- response = form.submit(bttn)
354
+ FileUtils.rm_f(file, verbose: false) unless Oddb2xml.skip_download?
355
+ unless File.exist?(file)
356
+ begin
357
+ response = nil
358
+ if (home = @agent.get(@url))
359
+ form = home.form_with(id: "Form1")
360
+ bttn = form.button_with(name: "ctl00$MainContent$btnOK")
361
+ if (page = form.submit(bttn))
362
+ form = page.form_with(id: "Form1")
363
+ bttn = form.button_with(name: "ctl00$MainContent$BtnYes")
364
+ response = form.submit(bttn)
365
+ end
340
366
  end
367
+ if response
368
+ response.save_as(file)
369
+ response = nil # win
370
+ end
371
+ rescue Timeout::Error, Errno::ETIMEDOUT
372
+ retrievable? ? retry : raise
373
+ rescue NoMethodError
374
+ # pass
375
+ ensure
376
+ Oddb2xml.download_finished(file)
341
377
  end
342
- if response
343
- response.save_as(file)
344
- response = nil # win
345
- end
346
- rescue Timeout::Error, Errno::ETIMEDOUT
347
- retrievable? ? retry : raise
348
- rescue NoMethodError
349
- # pass
350
- ensure
351
- Oddb2xml.download_finished(file)
352
- end unless File.exists?(file)
378
+ end
353
379
  read_xml_from_zip(/^AipsDownload_/iu, file)
354
380
  end
355
381
  end