oddb2xml 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.travis.yml +1 -1
  2. data/Gemfile.lock +7 -7
  3. data/README.md +14 -2
  4. data/Rakefile +10 -1
  5. data/bin/oddb2xml +2 -2
  6. data/lib/oddb2xml/builder.rb +36 -26
  7. data/lib/oddb2xml/cli.rb +19 -4
  8. data/lib/oddb2xml/compressor.rb +7 -7
  9. data/lib/oddb2xml/downloader.rb +77 -58
  10. data/lib/oddb2xml/extractor.rb +31 -21
  11. data/lib/oddb2xml/util.rb +18 -13
  12. data/lib/oddb2xml/version.rb +1 -1
  13. data/oddb2xml.gemspec +8 -5
  14. data/spec/builder_spec.rb +68 -31
  15. data/spec/cli_spec.rb +43 -69
  16. data/spec/compressor_spec.rb +36 -13
  17. data/spec/data/compressor/oddb2xml_files_bm_update.txt +4 -0
  18. data/spec/data/compressor/oddb2xml_files_lppv.txt +4 -0
  19. data/spec/data/compressor/oddb2xml_files_nonpharma.xls +0 -0
  20. data/spec/data/{oddb_article.xml → compressor/oddb_article.xml} +0 -0
  21. data/spec/data/{oddb_fi.xml → compressor/oddb_fi.xml} +0 -0
  22. data/spec/data/{oddb_fi_product.xml → compressor/oddb_fi_product.xml} +0 -0
  23. data/spec/data/{oddb_limitation.xml → compressor/oddb_limitation.xml} +0 -0
  24. data/spec/data/{oddb_product.xml → compressor/oddb_product.xml} +0 -0
  25. data/spec/data/{oddb_substance.xml → compressor/oddb_substance.xml} +0 -0
  26. data/spec/data/epha_interactions.csv +4 -1
  27. data/spec/data/swissindex_nonpharma.xml +13 -1
  28. data/spec/data/swissindex_pharma.xml +12 -0
  29. data/spec/data/{swissmedic_fridges.xlsx → swissmedic_fridge.xlsx} +0 -0
  30. data/spec/data/swissmedic_orphan.xlsx +0 -0
  31. data/spec/data/{swissmedic_packages.xlsx → swissmedic_package.xlsx} +0 -0
  32. data/spec/data/zurrose_transfer.dat +12 -11
  33. data/spec/downloader_spec.rb +48 -13
  34. data/spec/extractor_spec.rb +10 -11
  35. data/spec/spec_helper.rb +44 -15
  36. data/test_options.rb +24 -8
  37. metadata +71 -45
  38. checksums.yaml +0 -15
  39. data/spec/data/Gestrichene_Packungen_Emballages_radies.xls +0 -1
  40. data/spec/data/Publications.xls +0 -1
  41. data/spec/data/swissmedic_fridges.xls +0 -0
  42. data/spec/data/swissmedic_packages.xls +0 -0
@@ -6,7 +6,7 @@ before_install:
6
6
  - gem update --system 2.1.11
7
7
  - gem --version
8
8
 
9
- script: bundle exec rake spec
9
+ script: bundle exec rake test
10
10
 
11
11
  rvm:
12
12
  - 1.9.3
@@ -1,15 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oddb2xml (1.8.1)
5
- archive-tar-minitar
4
+ oddb2xml (1.8.2)
5
+ archive-tar-minitar (~> 0.5.2)
6
6
  mechanize (~> 2.5.1)
7
7
  nokogiri (~> 1.5.10)
8
8
  rubyXL (~> 2.5)
9
- rubyzip (~> 1.0)
10
- savon (~> 2.0)
11
- sax-machine
12
- spreadsheet
9
+ rubyzip (~> 1.1.3)
10
+ savon (~> 2.4.0)
11
+ sax-machine (~> 0.1.0)
12
+ spreadsheet (~> 0.9.7)
13
13
 
14
14
  GEM
15
15
  remote: https://rubygems.org/
@@ -79,7 +79,7 @@ GEM
79
79
  rubyzip (>= 1.0.0)
80
80
  rubyntlm (0.3.4)
81
81
  rubyzip (1.1.3)
82
- safe_yaml (1.0.2)
82
+ safe_yaml (1.0.3)
83
83
  savon (2.4.0)
84
84
  akami (~> 1.2.0)
85
85
  builder (>= 2.1.2)
data/README.md CHANGED
@@ -20,7 +20,7 @@ and
20
20
  * oddb2xml -f dat
21
21
  * oddb2xml -f dat -a nonpharma
22
22
 
23
- creates .dat files according to ([IGM-11](http://dev.ywesee.com/uploads/att/IGM.pdf))
23
+ creates .dat files according to ([IGM-11](http://dev.ywesee.com/uploads/att/IGM.pdf)). IGM-11 describes the structure of the zurrose_transfer.dat.
24
24
 
25
25
  * oddb.dat
26
26
  * oddb_with_migel.dat
@@ -43,7 +43,7 @@ The following additional data is in the files:
43
43
  see `--help`.
44
44
 
45
45
  ```
46
- /usr/local/bin/oddb2xml ver.1.7.8
46
+ /usr/local/bin/oddb2xml ver.1.8.2
47
47
  Usage:
48
48
  oddb2xml [option]
49
49
  produced files are found under data
@@ -87,6 +87,18 @@ FR
87
87
  Pharma products: 14801
88
88
  ```
89
89
 
90
+ ## Supported ruby version
91
+
92
+ We run tests on travis-ci.org for the Ruby versions mentioned in the .travis.yml file. These are
93
+ * 1.9.3
94
+ * 2.0.0
95
+ * 2.1
96
+ * ruby-head
97
+
98
+ If you are running an older Ruby-Version (eg. 1.8 or 1.9.1), please upgrade before reporting a bug.
99
+ See also http://bugs.ruby-lang.org/projects/ruby/wiki/ReleaseEngineering
100
+
101
+
90
102
  ## XSD files
91
103
  If you need the XSD files, generate them yourself using the javabeans tool:
92
104
 
data/Rakefile CHANGED
@@ -19,16 +19,25 @@ task :spec => :clean
19
19
 
20
20
  desc 'Run oddb2xml with all commonly used combinations'
21
21
  task :test => [:clean, :spec, :gem] do
22
- system("./test_options.rb 2>&1 | tee test_options.log")
22
+ log_file = 'test_options.log'
23
+ puts "Running test_options.rb with Output redirected to #{log_file}. This will take some time (e.g. 20 minutes)"
24
+ # must use bash -o pipefail to catch error in test_options.rb and not tee
25
+ # see http://stackoverflow.com/questions/985876/tee-and-exit-status
26
+ res = system("bash -c 'set -o pipefail && ./test_options.rb 2>&1 | tee #{log_file}'")
27
+ puts "Running test_options.rb returned #{res.inspect}. Output was redirected to #{log_file}"
28
+ exit 1 unless res
23
29
  end
24
30
 
25
31
  require 'rake/clean'
32
+ CLEAN.include FileList['pkg/*.gem']
33
+ CLEAN.include FileList['*.zip*']
26
34
  CLEAN.include FileList['*.xls*']
27
35
  CLEAN.include FileList['*.xml*']
28
36
  CLEAN.include FileList['*.dat*']
29
37
  CLEAN.include FileList['*.tar.gz']
30
38
  CLEAN.include FileList['*.txt.*']
31
39
  CLEAN.include FileList['*.csv.*']
40
+ CLEAN.include FileList['*.zip.*']
32
41
  CLEAN.include FileList['ruby*.tmp']
33
42
  CLEAN.include FileList['data/download']
34
43
  CLEAN.include FileList['duplicate_ean13_from_zur_rose.txt']
@@ -28,8 +28,8 @@ Usage:
28
28
  -x N, --context=N context N {product|address}. product is default.
29
29
 
30
30
  For debugging purposes
31
- --skip-download skips downloading files it the file is already under data/download.
32
- Downloaded files are saved under data/download
31
+ --skip-download skips downloading files it the file is already under downloads.
32
+ Downloaded files are saved under downloads
33
33
  --log log important actions
34
34
  -h, --help Show this help message.
35
35
  EOS
@@ -39,6 +39,7 @@ module Oddb2xml
39
39
  @companies = []
40
40
  @people = []
41
41
  @tag_suffix = nil
42
+ @pharmacode = {} # index pharmacode => item
42
43
  if block_given?
43
44
  yield self
44
45
  end
@@ -78,6 +79,7 @@ module Oddb2xml
78
79
  obj[:seq] = seq
79
80
  end
80
81
  @articles << obj
82
+ @pharmacode[phar] = obj
81
83
  end
82
84
  # add
83
85
  @migel.values.compact.each do |migel|
@@ -117,37 +119,44 @@ module Oddb2xml
117
119
  Oddb2xml.log("prepare_articles extended")
118
120
  @prices.each{
119
121
  |ean13, info|
122
+ pharmacode = info[:pharmacode]
123
+ if @pharmacode[pharmacode]
124
+ @pharmacode[pharmacode][:price] = info[:price]
125
+ @pharmacode[pharmacode][:pub_price] = info[:pub_price]
126
+ next
127
+ end
120
128
  obj = {}
121
129
  found = false
122
130
  %w[de fr].each do |lang|
123
131
  # existing = @articles.find{|art| art[lang.intern] and art[lang.intern][0][:ean] == ean13 }
124
- existing = local_index[lang][ean13]
132
+
133
+ existing = local_index[lang][ean13]
125
134
  if existing
126
135
  found = true
127
- existing[:price] = info[:price]
128
- existing[:pub_price] = info[:pub_price]
129
- else
130
- entry = {
131
- :desc => info[:description],
132
- :status => 'I', # or it will not be emitted in the dat
133
- :atc_code => '',
134
- :ean => ean13,
135
- :lang => lang.capitalize,
136
- :pharmacode => info[:pharmacode],
137
- :price => info[:price],
138
- :pub_price => info[:pub_price],
139
- :type => info[:type],
140
- }
141
- obj[lang.intern] = [entry]
142
- @index[lang.upcase][ean13] = entry
143
- end
144
- end
145
- unless found
146
- @articles << obj
147
- nrAdded += 1
148
- end
149
- }
136
+ existing[:price] = info[:price]
137
+ existing[:pub_price] = info[:pub_price]
138
+ else
139
+ entry = {
140
+ :desc => info[:description],
141
+ :status => 'A', # for ZurRose 'A' means aktive, aka available in trade
142
+ :atc_code => '',
143
+ :ean => ean13,
144
+ :lang => lang.capitalize,
145
+ :pharmacode => pharmacode,
146
+ :price => info[:price],
147
+ :pub_price => info[:pub_price],
148
+ :type => info[:type],
149
+ }
150
+ obj[lang.intern] = [entry]
151
+ @index[lang.upcase][ean13] = entry
152
+ end
150
153
  end
154
+ unless found
155
+ @articles << obj
156
+ nrAdded += 1
157
+ end
158
+ }
159
+ end
151
160
  end
152
161
  Oddb2xml.log("prepare_articles done. Added #{nrAdded} prices. Total #{@articles.size}")
153
162
  end
@@ -604,7 +613,7 @@ module Oddb2xml
604
613
  'PROD_DATE' => datetime,
605
614
  'VALID_DATE' => datetime
606
615
  ) {
607
- @articles.each do |obj|
616
+ @articles.each do |obj|
608
617
  idx += 1
609
618
  Oddb2xml.log "build_article #{idx} of #{@articles.size} articles" if idx % 500 == 0
610
619
  obj[:de].each_with_index do |de_idx, i|
@@ -612,6 +621,7 @@ module Oddb2xml
612
621
  pac,no8 = nil,de_idx[:ean][4..11] # BAG-XML(SL/LS)
613
622
  ppac = nil # Packungen
614
623
  ean = de_idx[:ean]
624
+ pharma_code = de_idx[:pharmacode]
615
625
  ean = nil if ean.match(/^000000/)
616
626
  if obj[:seq]
617
627
  pac = obj[:seq][:packages][de_idx[:pharmacode]]
@@ -777,7 +787,7 @@ module Oddb2xml
777
787
  }
778
788
  end
779
789
  }
780
- end
790
+ end if obj[:de]
781
791
  end
782
792
  xml.RESULT {
783
793
  xml.OK_ERROR 'OK'
@@ -9,6 +9,7 @@ require 'oddb2xml/util'
9
9
  require 'rubyXL'
10
10
 
11
11
  module Oddb2xml
12
+
12
13
  class Cli
13
14
  SUBJECTS = %w[product article]
14
15
  ADDITIONS = %w[substance limitation interaction code]
@@ -89,6 +90,7 @@ module Oddb2xml
89
90
  end
90
91
  private
91
92
  def build
93
+ Oddb2xml.log("Start build")
92
94
  begin
93
95
  files.each_pair do |sbj, file|
94
96
  builder = Builder.new(@options) do |builder|
@@ -141,7 +143,7 @@ module Oddb2xml
141
143
  else
142
144
  output = builder.to_xml
143
145
  end
144
- File.open(file, 'w:utf-8'){ |fh| fh << output }
146
+ File.open(File.join(WorkDir, file), 'w:utf-8'){ |fh| fh << output }
145
147
  end
146
148
  rescue Interrupt
147
149
  files.values.each do |file|
@@ -171,6 +173,7 @@ module Oddb2xml
171
173
  @mutex.synchronize do
172
174
  hsh = SwissmedicInfoExtractor.new(xml).to_hash
173
175
  @infos = hsh
176
+ Oddb2xml.log("SwissmedicInfoExtractor added #{@infos.size} fachinfo")
174
177
  end
175
178
  end
176
179
  when :orphan, :fridge
@@ -182,6 +185,7 @@ module Oddb2xml
182
185
  "@#{var}".intern,
183
186
  SwissmedicExtractor.new(bin, what).to_arry
184
187
  )
188
+ # Oddb2xml.log("SwissmedicExtractor added #{self.instance_variable_get("@#{var}".intern).size} #{var}. File #{bin} was #{File.size(bin)} bytes")
185
189
  end
186
190
  when :interaction
187
191
  Thread.new do
@@ -189,6 +193,7 @@ module Oddb2xml
189
193
  str = downloader.download
190
194
  @mutex.synchronize do
191
195
  @actions = EphaExtractor.new(str).to_arry
196
+ Oddb2xml.log("EphaExtractor added #{@actions.size} interactions")
192
197
  end
193
198
  end
194
199
  when :migel
@@ -197,6 +202,7 @@ module Oddb2xml
197
202
  bin = downloader.download
198
203
  @mutex.synchronize do
199
204
  @migel = MigelExtractor.new(bin).to_hash
205
+ Oddb2xml.log("MigelExtractor added #{@migel.size} migel items")
200
206
  end
201
207
  end
202
208
  when :package
@@ -205,6 +211,7 @@ module Oddb2xml
205
211
  bin = downloader.download
206
212
  @mutex.synchronize do
207
213
  @packs = SwissmedicExtractor.new(bin, :package).to_hash
214
+ # Oddb2xml.log("SwissmedicExtractor added #{@packs.size} packs. File #{bin} was #{File.size(bin)} bytes")
208
215
  end
209
216
  end
210
217
  when :bm_update
@@ -213,6 +220,7 @@ module Oddb2xml
213
220
  str = downloader.download
214
221
  @mutex.synchronize do
215
222
  @flags = BMUpdateExtractor.new(str).to_hash
223
+ Oddb2xml.log("BMUpdateExtractor added #{@flags.size} flags")
216
224
  end
217
225
  end
218
226
  when :lppv
@@ -221,6 +229,7 @@ module Oddb2xml
221
229
  str = downloader.download
222
230
  @mutex.synchronize do
223
231
  @lppvs = LppvExtractor.new(str).to_hash
232
+ Oddb2xml.log("LppvExtractor added #{@lppvs.size} lppvs")
224
233
  end
225
234
  end
226
235
  when :bag
@@ -230,16 +239,18 @@ module Oddb2xml
230
239
  @mutex.synchronize do
231
240
  hsh = BagXmlExtractor.new(xml).to_hash
232
241
  @items = hsh
242
+ Oddb2xml.log("BagXmlDownloader added #{@items.size} items")
233
243
  end
234
244
  end
235
245
  when :zurrose
236
246
  Thread.new do
237
247
  downloader = ZurroseDownloader.new(@options, @options[:transfer_dat])
238
248
  xml = downloader.download
249
+ Oddb2xml.log("zurrose xml #{xml.size} bytes")
239
250
  @mutex.synchronize do
240
251
  hsh = ZurroseExtractor.new(xml, @options[:extended]).to_hash
241
252
  @prices = hsh
242
- Oddb2xml.log("zurrose added #{@prices.size} prices")
253
+ Oddb2xml.log("zurrose added #{@prices.size} prices from xml with #{xml.size} bytes")
243
254
  end
244
255
  end
245
256
  when :index
@@ -265,8 +276,9 @@ module Oddb2xml
265
276
  def compress
266
277
  compressor = Compressor.new(prefix, @options)
267
278
  files.values.each do |file|
268
- if File.exists?(file)
269
- compressor.contents << file
279
+ work_file = File.join(WorkDir, file)
280
+ if File.exists?(work_file)
281
+ compressor.contents << work_file
270
282
  end
271
283
  end
272
284
  compressor.finalize!
@@ -319,6 +331,9 @@ module Oddb2xml
319
331
  end
320
332
  end
321
333
  end
334
+ if @options[:extended]
335
+ lines << sprintf("\tPrices zur Rose: %i", @prices.length)
336
+ end
322
337
  else
323
338
  {
324
339
  'Betrieb' => :@companies,
@@ -12,13 +12,14 @@ module Oddb2xml
12
12
  @options = options
13
13
  @options[:compress_ext] ||= 'tar.gz'
14
14
  @options[:format] ||= :xml
15
- @compress_file = "#{prefix}_#{@options[:format].to_s}_" +
16
- Time.now.strftime("%d.%m.%Y_%H.%M.#{@options[:compress_ext]}")
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]}"))
17
18
  @contents = []
18
19
  super()
19
20
  end
20
21
  def finalize!
21
- if @contents.empty?
22
+ if @contents.empty? and @contents.size == 0
22
23
  return false
23
24
  end
24
25
  begin
@@ -34,13 +35,12 @@ module Oddb2xml
34
35
  end
35
36
  end
36
37
  end
37
- if File.exists? @compress_file
38
+ if File.exists? @compress_file and not defined?(Rspec)
38
39
  @contents.each do |file|
39
- Oddb2xml.download_finished(file)
40
+ FileUtils.rm(file)
40
41
  end
41
42
  end
42
- rescue Errno::ENOENT, StandardError
43
- Oddb2xml.download_finished(@compress_file)
43
+ rescue Errno::ENOENT, StandardError => e
44
44
  return false
45
45
  end
46
46
  return true
@@ -8,8 +8,11 @@ module Oddb2xml
8
8
  module DownloadMethod
9
9
  private
10
10
  def download_as(file, option='r')
11
- Oddb2xml.log "download_as file #{file} from #{@url}"
11
+ tempFile = File.join(WorkDir, File.basename(file))
12
+ file2save = File.join(Downloads, File.basename(file))
13
+ Oddb2xml.log "download_as file #{file2save} via #{tempFile} from #{@url}"
12
14
  data = nil
15
+ FileUtils.rm_f(tempFile, :verbose => false)
13
16
  if Oddb2xml.skip_download(file)
14
17
  io = File.open(file, option)
15
18
  data = io.read
@@ -24,7 +27,7 @@ module Oddb2xml
24
27
  retrievable? ? retry : raise
25
28
  ensure
26
29
  io.close if io and !io.closed? # win
27
- Oddb2xml.download_finished(file)
30
+ Oddb2xml.download_finished(tempFile)
28
31
  end
29
32
  end
30
33
  return data
@@ -63,12 +66,29 @@ module Oddb2xml
63
66
  false
64
67
  end
65
68
  end
66
- def read_xml_form_zip(target, zipfile)
69
+ def read_xml_from_zip(target, zipfile)
70
+ Oddb2xml.log "read_xml_from_zip target is #{target} zip: #{zipfile} #{File.exists?(zipfile)}"
71
+ if Oddb2xml.skip_download?
72
+ entry = nil
73
+ Dir.glob(File.join(Downloads, '*')).each { |name| if target.match(name) then entry = name; break end }
74
+ if entry
75
+ dest = "#{Downloads}/#{File.basename(entry)}"
76
+ if File.exists?(dest)
77
+ Oddb2xml.log "read_xml_from_zip return content of #{dest} #{File.size(dest)} bytes "
78
+ return IO.read(dest)
79
+ else
80
+ Oddb2xml.log "read_xml_from_zip could not read #{dest}"
81
+ end
82
+ else
83
+ Oddb2xml.log "read_xml_from_zip could not find #{target.to_s}"
84
+ end
85
+ end
67
86
  xml = ''
68
87
  if RUBY_PLATFORM =~ /mswin|mingw|bccwin|cygwin/i
69
88
  Zip::File.open(zipfile) do |zipFile|
70
89
  zipFile.each do |entry|
71
90
  if entry.name =~ target
91
+ Oddb2xml.log "read_xml_from_zip reading #{__LINE__}: #{entry.name}"
72
92
  io = entry.get_input_stream
73
93
  until io.eof?
74
94
  bytes = io.read(1024)
@@ -76,13 +96,20 @@ module Oddb2xml
76
96
  bytes = nil
77
97
  end
78
98
  io.close if io.respond_to?(:close)
99
+ dest = "#{Downloads}/#{File.basename(target)}"
100
+ File.open(dest, 'w+') { |f| f.write xml }
101
+ Oddb2xml.log "read_xml_from_zip saved as #{dest}"
79
102
  end
80
103
  end
81
104
  end
82
105
  else
83
106
  Zip::File.foreach(zipfile) do |entry|
84
107
  if entry.name =~ target
108
+ Oddb2xml.log "read_xml_from_zip #{__LINE__}: reading #{entry.name}"
109
+ dest = "#{Downloads}/#{File.basename(entry.name)}"
85
110
  entry.get_input_stream { |io| xml = io.read }
111
+ File.open(dest, 'w+') { |f| f.write xml }
112
+ Oddb2xml.log "read_xml_from_zip saved as #{dest}"
86
113
  end
87
114
  end
88
115
  end
@@ -123,9 +150,12 @@ module Oddb2xml
123
150
  @url ||= 'http://zurrose.com/fileadmin/main/lib/download.php?file=/fileadmin/user_upload/downloads/ProduktUpdate/IGM11_mit_MwSt/Vollstamm/transfer.dat'
124
151
  unless @url =~ /^http/
125
152
  io = File.open(@url, 'r:iso-8859-1:utf-8')
126
- io.read
153
+ content = io.read
154
+ Oddb2xml.log("ZurroseDownloader #{__LINE__} download #{@url} @url returns #{content.bytes}")
155
+ content
127
156
  else
128
- download_as('oddb2xml_zurrose_transfer.dat', 'r:iso-8859-1:utf-8')
157
+ Oddb2xml.log("ZurroseDownloader #{__LINE__} download #{@url} @url")
158
+ download_as('zurrose_transfer.dat', 'r:iso-8859-1:utf-8')
129
159
  end
130
160
  end
131
161
  end
@@ -154,30 +184,23 @@ module Oddb2xml
154
184
  @url ||= 'http://bag.e-mediat.net/SL2007.Web.External/File.axd?file=XMLPublications.zip'
155
185
  end
156
186
  def download
157
- file = 'XMLPublications.zip'
158
- begin
159
- if @options[:debug]
160
- FileUtils.copy(File.expand_path("../../../spec/data/#{file}", __FILE__), '.')
161
- else
162
- unless Oddb2xml.skip_download(file)
163
- response = @agent.get(@url)
164
- response.save_as(file)
165
- response = nil # win
166
- end
167
- end
168
- inhalt = read_xml_form_zip(/^Preparation/iu, file)
169
- if @options[:skip_download]
170
- FileUtils.makedirs(Backup)
171
- outfile = File.join(Backup, 'Preparations.xml')
172
- Oddb2xml.log "Downloader saving outfile #{outfile} for #{self.class}"
173
- File.open(outfile, 'w+') { |file| file.write inhalt}
187
+ file = File.join(WorkDir, 'XMLPublications.zip')
188
+ Oddb2xml.log "BagXmlDownloader #{__LINE__}: #{file}"
189
+ unless Oddb2xml.skip_download(file)
190
+ Oddb2xml.log "BagXmlDownloader #{__LINE__}: #{file}"
191
+ begin
192
+ response = @agent.get(@url)
193
+ response.save_as(file)
194
+ response = nil # win
195
+ rescue Timeout::Error, Errno::ETIMEDOUT
196
+ retrievable? ? retry : raise
197
+ ensure
198
+ Oddb2xml.download_finished(file)
174
199
  end
175
- inhalt
176
- rescue Timeout::Error, Errno::ETIMEDOUT
177
- retrievable? ? retry : raise
178
- ensure
179
- Oddb2xml.download_finished(file)
180
200
  end
201
+ content = read_xml_from_zip(/Preparations.xml/, File.join(Downloads, File.basename(file)))
202
+ FileUtils.rm_f(file, :verbose => false) unless defined?(Rspec)
203
+ content
181
204
  end
182
205
  end
183
206
  class SwissIndexDownloader < Downloader
@@ -199,9 +222,10 @@ module Oddb2xml
199
222
  end
200
223
  def download
201
224
  begin
202
- if @options[:debug]
203
- return File.read(File.expand_path("../../../spec/data/swissindex_#{@type}_#{@lang}.xml", __FILE__))
204
- end
225
+ filename = "swissindex_#{@type}_#{@lang}.xml"
226
+ file2save = File.join(Downloads, "swissindex_#{@type}_#{@lang}.xml")
227
+ return IO.read(file2save) if Oddb2xml.skip_download? and File.exists?(file2save)
228
+ FileUtils.rm_f(file2save, :verbose => false)
205
229
  soap = <<XML
206
230
  <?xml version="1.0" encoding="utf-8"?>
207
231
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
@@ -214,12 +238,8 @@ XML
214
238
  if response.success?
215
239
  if xml = response.to_xml
216
240
  response = nil # win
217
- if @options[:skip_download]
218
- file2save = File.expand_path("../../../data/download/swissindex_#{@type}_#{@lang}.xml", __FILE__)
219
- FileUtils.makedirs(File.dirname(file2save)) unless File.directory?(File.dirname(file2save))
220
- File.open(file2save, 'w+') { |file| file.puts xml }
221
- end
222
- return xml
241
+ FileUtils.makedirs(Downloads)
242
+ File.open(file2save, 'w+') { |file| file.write xml }
223
243
  else
224
244
  # received broken data or internal error
225
245
  raise StandardError
@@ -232,6 +252,7 @@ XML
232
252
  rescue Timeout::Error, Errno::ETIMEDOUT
233
253
  retrievable? ? retry : raise
234
254
  end
255
+ xml
235
256
  end
236
257
  end
237
258
  class SwissmedicDownloader < Downloader
@@ -257,11 +278,9 @@ XML
257
278
  page = @agent.get(@url)
258
279
  if link_node = page.search(@xpath).first
259
280
  link = Mechanize::Page::Link.new(link_node, @agent, page)
260
- unless Oddb2xml.skip_download(file)
261
- response = link.click
262
- response.save_as(file)
263
- response = nil # win
264
- end
281
+ response = link.click
282
+ response.save_as(file)
283
+ response = nil # win
265
284
  end
266
285
  return File.expand_path(file)
267
286
  rescue Timeout::Error, Errno::ETIMEDOUT
@@ -269,6 +288,7 @@ XML
269
288
  ensure
270
289
  Oddb2xml.download_finished(file, false)
271
290
  end
291
+ return File.expand_path(file)
272
292
  end
273
293
  end
274
294
  class SwissmedicInfoDownloader < Downloader
@@ -278,32 +298,31 @@ XML
278
298
  @url ||= "http://download.swissmedicinfo.ch/Accept.aspx?ReturnUrl=%2f"
279
299
  end
280
300
  def download
281
- file = "swissmedic_info.zip"
301
+ file = File.join(Downloads, "swissmedic_info.zip")
302
+ FileUtils.rm_f(file, :verbose => false) unless Oddb2xml.skip_download?
282
303
  begin
283
- unless Oddb2xml.skip_download(file)
284
- response = nil
285
- if home = @agent.get(@url)
286
- form = home.form_with(:id => 'Form1')
287
- bttn = form.button_with(:name => 'ctl00$MainContent$btnOK')
288
- if page = form.submit(bttn)
289
- form = page.form_with(:id => 'Form1')
290
- bttn = form.button_with(:name => 'ctl00$MainContent$BtnYes')
291
- response = form.submit(bttn)
292
- end
293
- end
294
- if response
295
- response.save_as(file)
296
- response = nil # win
304
+ response = nil
305
+ if home = @agent.get(@url)
306
+ form = home.form_with(:id => 'Form1')
307
+ bttn = form.button_with(:name => 'ctl00$MainContent$btnOK')
308
+ if page = form.submit(bttn)
309
+ form = page.form_with(:id => 'Form1')
310
+ bttn = form.button_with(:name => 'ctl00$MainContent$BtnYes')
311
+ response = form.submit(bttn)
297
312
  end
298
313
  end
299
- read_xml_form_zip(/^AipsDownload_/iu, file)
314
+ if response
315
+ response.save_as(file)
316
+ response = nil # win
317
+ end
300
318
  rescue Timeout::Error, Errno::ETIMEDOUT
301
319
  retrievable? ? retry : raise
302
320
  rescue NoMethodError
303
321
  # pass
304
322
  ensure
305
323
  Oddb2xml.download_finished(file)
306
- end
324
+ end unless File.exists?(file)
325
+ read_xml_from_zip(/^AipsDownload_/iu, file)
307
326
  end
308
327
  end
309
328
  end