oddb2xml 2.6.9 → 2.7.4

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