oddb2xml 2.1.7 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +60 -54
- data/History.txt +9 -0
- data/README.md +3 -0
- data/lib/oddb2xml/builder.rb +278 -300
- data/lib/oddb2xml/cli.rb +45 -55
- data/lib/oddb2xml/downloader.rb +23 -19
- data/lib/oddb2xml/extractor.rb +19 -21
- data/lib/oddb2xml/util.rb +2 -1
- data/lib/oddb2xml/version.rb +1 -1
- data/lib/oddb2xml/xml_definitions.rb +29 -12
- data/oddb2xml.gemspec +14 -10
- data/spec/builder_spec.rb +318 -406
- data/spec/calc_spec.rb +7 -14
- data/spec/cli_spec.rb +119 -51
- data/spec/downloader_spec.rb +372 -115
- data/spec/extractor_spec.rb +126 -44
- data/spec/fixtures/vcr_cassettes/oddb2xml.json +1 -0
- data/spec/galenic_spec.rb +1 -3
- data/spec/options_spec.rb +0 -13
- data/spec/parslet_spec.rb +0 -6
- data/spec/spec_helper.rb +105 -240
- data/test_options.rb +7 -2
- metadata +92 -50
- data/spec/data/swissindex.xml +0 -56
- data/spec/data/swissindex_NonPharma_DE.xml +0 -65
- data/spec/data/swissindex_NonPharma_FR.xml +0 -65
- data/spec/data/swissindex_Pharma_DE.xml +0 -82
- data/spec/data/swissindex_Pharma_FR.xml +0 -82
- data/spec/data/swissindex_nonpharma.xml +0 -65
- data/spec/data/swissindex_pharma.xml +0 -82
- data/spec/data/swissmedic_package-galenic.xlsx +0 -0
data/spec/downloader_spec.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
require 'spec_helper'
|
4
3
|
|
4
|
+
VCR.eject_cassette # we use insert/eject around each example
|
5
|
+
|
6
|
+
XML_VERSION_1_0 = /xml\sversion=["']1.0["']/
|
7
|
+
PREP_XML = 'Preparations.xml'
|
5
8
|
shared_examples_for 'any downloader' do
|
6
9
|
# this takes 5 sec. by call for sleep
|
7
10
|
it 'should count retry times as retrievable or not', :slow => true do
|
@@ -14,7 +17,7 @@ shared_examples_for 'any downloader' do
|
|
14
17
|
}.to change {
|
15
18
|
@downloader.instance_variable_get(:@retry_times)
|
16
19
|
}.from(3).to(0)
|
17
|
-
end
|
20
|
+
end if false # as vcr does not support threads for the moment
|
18
21
|
end
|
19
22
|
|
20
23
|
def common_before
|
@@ -25,94 +28,173 @@ end
|
|
25
28
|
|
26
29
|
def common_after
|
27
30
|
Dir.chdir(@savedDir) if @savedDir and File.directory?(@savedDir)
|
31
|
+
VCR.eject_cassette
|
28
32
|
end
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
after(:each) do common_after end
|
38
|
-
|
39
|
-
it_behaves_like 'any downloader'
|
40
|
-
context 'when download is called' do
|
41
|
-
let(:xml) { @downloader.download }
|
42
|
-
it 'should parse zip to string' do
|
43
|
-
xml.should be_a String
|
44
|
-
xml.length.should_not == 0
|
34
|
+
# Zips input_filenames (using the basename)
|
35
|
+
def zip_files(zipfile_name, input_filenames)
|
36
|
+
FileUtils.rm_f(zipfile_name)
|
37
|
+
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
|
38
|
+
input_filenames.each do |filename|
|
39
|
+
puts "Add #{filename} #{File.size(filename)} bytes as #{File.basename(filename)} #{Dir.pwd}" if $VERBOSE
|
40
|
+
zipfile.add(File.basename(filename), filename)
|
45
41
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Unzips into a specific directory
|
46
|
+
def unzip_files(zipfile_name, directory)
|
47
|
+
savedDir = Dir.pwd
|
48
|
+
FileUtils.makedirs(directory)
|
49
|
+
Dir.chdir(directory)
|
50
|
+
Zip::File.open(zipfile_name) do |zip_file|
|
51
|
+
# Handle entries one by one
|
52
|
+
zip_file.each do |entry|
|
53
|
+
# Extract to file/directory/symlink
|
54
|
+
puts "Extracting #{entry.name} into #{directory}"
|
55
|
+
entry.extract(entry.name)
|
50
56
|
end
|
51
57
|
end
|
58
|
+
ensure
|
59
|
+
Dir.chdir(savedDir)
|
52
60
|
end
|
53
61
|
|
54
|
-
describe Oddb2xml::
|
62
|
+
describe Oddb2xml::RefdataDownloader do
|
55
63
|
include ServerMockHelper
|
56
|
-
before(:
|
57
|
-
|
64
|
+
before(:all) do
|
65
|
+
VCR.eject_cassette
|
66
|
+
VCR.configure do |c|
|
67
|
+
c.before_record(:Refdata_DE) do |i|
|
68
|
+
if not /WSDL$/.match(i.request.uri) and /refdatabase.refdata.ch\/Service/.match(i.request.uri) and i.response.body.size > 1024*1024
|
69
|
+
puts "#{Time.now}: #{__LINE__}: Parsing response.body (#{i.response.body.size} bytes) will take some time. URI was #{i.request.uri}"
|
70
|
+
doc = REXML::Document.new(i.response.body)
|
71
|
+
items = doc.root.children.first.elements.first
|
72
|
+
nrItems = doc.root.children.first.elements.first.elements.size
|
73
|
+
puts "#{Time.now}: #{__LINE__}: Removing most of the #{nrItems} items will take some time"
|
74
|
+
nrSearched = 0
|
75
|
+
items.elements.each{
|
76
|
+
|x|
|
77
|
+
nrSearched += 1
|
78
|
+
puts "#{Time.now}: #{__LINE__}: nrSearched #{nrSearched}/#{nrItems}" if nrSearched % 1000 == 0
|
79
|
+
items.delete x unless x.elements['GTIN'] and Oddb2xml::GTINS_DRUGS.index(x.elements['GTIN'].text)
|
80
|
+
}
|
81
|
+
i.response.body = doc.to_s
|
82
|
+
puts "#{Time.now}: response.body is now #{i.response.body.size} bytes long"
|
83
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
VCR.insert_cassette('oddb2xml', :tag => :Refdata_DE)
|
58
88
|
common_before
|
59
89
|
end
|
60
|
-
after(:
|
61
|
-
|
62
|
-
|
63
|
-
|
90
|
+
after(:all) do
|
91
|
+
common_after
|
92
|
+
end
|
93
|
+
context 'Pharma' do
|
94
|
+
before(:all) do
|
95
|
+
@downloader = Oddb2xml::RefdataDownloader.new({}, :pharma)
|
96
|
+
@xml = @downloader.download
|
64
97
|
end
|
65
98
|
it_behaves_like 'any downloader'
|
66
|
-
context 'when download_by is called
|
67
|
-
let(:xml) { @downloader.download }
|
99
|
+
context 'when download_by is called' do
|
68
100
|
it 'should parse response hash to xml' do
|
69
|
-
xml.should be_a String
|
70
|
-
xml.length.should_not == 0
|
71
|
-
xml.should =~
|
101
|
+
@xml.should be_a String
|
102
|
+
@xml.length.should_not == 0
|
103
|
+
@xml.should =~ XML_VERSION_1_0
|
72
104
|
end
|
73
105
|
it 'should return valid xml' do
|
74
|
-
xml.should =~ /PHAR/
|
75
|
-
xml.should =~ /ITEM/
|
106
|
+
@xml.should =~ /PHAR/
|
107
|
+
@xml.should =~ /ITEM/
|
76
108
|
end
|
77
109
|
end
|
78
110
|
end
|
79
|
-
|
80
|
-
|
81
|
-
@downloader = Oddb2xml::SwissIndexDownloader.new({}, :nonpharma, 'FR')
|
82
|
-
end
|
111
|
+
|
112
|
+
context 'NonPharma' do
|
83
113
|
it_behaves_like 'any downloader'
|
84
|
-
|
85
|
-
|
114
|
+
before(:all) do
|
115
|
+
@downloader = Oddb2xml::RefdataDownloader.new({}, :nonpharma)
|
116
|
+
@xml = @downloader.download
|
117
|
+
end
|
118
|
+
context 'when download_by is ' do
|
86
119
|
it 'should parse response hash to xml' do
|
87
|
-
xml.should be_a String
|
88
|
-
xml.length.should_not == 0
|
89
|
-
xml.should =~
|
120
|
+
@xml.should be_a String
|
121
|
+
@xml.length.should_not == 0
|
122
|
+
@xml.should =~ XML_VERSION_1_0
|
90
123
|
end
|
91
124
|
it 'should return valid xml' do
|
92
|
-
xml.should =~ /NONPHAR/
|
93
|
-
xml.should =~ /ITEM/
|
125
|
+
@xml.should =~ /NONPHAR/
|
126
|
+
@xml.should =~ /ITEM/
|
94
127
|
end
|
95
128
|
end
|
96
129
|
end
|
97
130
|
end
|
98
131
|
|
99
|
-
|
132
|
+
if true
|
133
|
+
describe Oddb2xml::SwissmedicDownloader do
|
100
134
|
include ServerMockHelper
|
135
|
+
before(:all) do VCR.eject_cassette end
|
136
|
+
before(:each) do
|
137
|
+
VCR.configure do |c|
|
138
|
+
c.before_record(:swissmedic) do |i|
|
139
|
+
if i.response.headers['Content-Disposition'] and /www.swissmedic.ch/.match(i.request.uri)
|
140
|
+
puts "#{Time.now}: URI was #{i.request.uri}"
|
141
|
+
m = /filename=.([^\d]+)/.match(i.response.headers['Content-Disposition'][0])
|
142
|
+
puts "#{Time.now}: SwissmedicDownloader #{m[1]} (#{i.response.body.size} bytes)."
|
143
|
+
# binding.pry
|
144
|
+
if m and true
|
145
|
+
name = m[1].chomp('_')
|
146
|
+
swissmedic_dir = File.join(Oddb2xml::WorkDir, 'swissmedic')
|
147
|
+
FileUtils.makedirs(swissmedic_dir)
|
148
|
+
xlsx_name = File.join(swissmedic_dir, name + '.xlsx')
|
149
|
+
if /Packungen/i.match(xlsx_name)
|
150
|
+
File.open(xlsx_name, 'wb+') { |f| f.write(i.response.body) }
|
151
|
+
puts "#{Time.now}: Openening saved #{xlsx_name} (#{File.size(xlsx_name)} bytes) will take some time. URI was #{i.request.uri}"
|
152
|
+
workbook = RubyXL::Parser.parse(xlsx_name)
|
153
|
+
worksheet = workbook[0]
|
154
|
+
drugs = []
|
155
|
+
Oddb2xml::GTINS_DRUGS.each{ |x| next unless x.to_s.size == 13; drugs << [x.to_s[4..8].to_i, x.to_s[9..11].to_i] };
|
156
|
+
idx = 6; to_delete = []
|
157
|
+
puts "#{Time.now}: Finding items to delete will take some time"
|
158
|
+
while (worksheet.sheet_data[idx])
|
159
|
+
idx += 1
|
160
|
+
next unless worksheet.sheet_data[idx-1][0]
|
161
|
+
to_delete << (idx-1) unless drugs.find{ |x| x[0]== worksheet.sheet_data[idx-1][0].value.to_i and
|
162
|
+
x[1]== worksheet.sheet_data[idx-1][10].value.to_i
|
163
|
+
}
|
164
|
+
end
|
165
|
+
if to_delete.size > 0
|
166
|
+
puts "#{Time.now}: Deleting #{to_delete.size} of the #{idx} items will take some time"
|
167
|
+
to_delete.reverse.each{ |row_id| worksheet.delete_row(row_id) }
|
168
|
+
workbook.write(xlsx_name)
|
169
|
+
i.response.body = IO.binread(xlsx_name)
|
170
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
171
|
+
puts "#{Time.now}: response.body is now #{i.response.body.size} bytes long. #{xlsx_name} was #{File.size(xlsx_name)}"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
# 2015-06-10 18:54:40 UTC: SwissmedicDownloader attachment; filename="Zugelassene_Packungen_310515.xlsx" (785630 bytes). URI was https://www.swissmedic.ch/arzneimittel/00156/00221/00222/00230/index.html?download=NHzLpZeg7t,lnp6I0NTU042l2Z6ln1acy4Zn4Z2qZpnO2Yuq2Z6gpJCDdHx7hGym162epYbg2c_JjKbNoKSn6A--&lang=de
|
180
|
+
|
101
181
|
context 'orphan' do
|
102
182
|
before(:each) do
|
103
|
-
|
104
|
-
@downloader = Oddb2xml::SwissmedicDownloader.new(:orphan)
|
183
|
+
VCR.insert_cassette('oddb2xml', :tag => :swissmedic, :exclusive => false)
|
105
184
|
common_before
|
185
|
+
@downloader = Oddb2xml::SwissmedicDownloader.new(:orphan)
|
106
186
|
end
|
107
187
|
after(:each) do common_after end
|
108
188
|
it_behaves_like 'any downloader'
|
109
189
|
context 'download_by for orphan xls' do
|
110
|
-
let(:bin) {
|
190
|
+
let(:bin) {
|
191
|
+
@downloader.download
|
192
|
+
}
|
111
193
|
it 'should return valid Binary-String' do
|
112
|
-
unless [:orphan, :package].index(@downloader.type)
|
194
|
+
# unless [:orphan, :package].index(@downloader.type)
|
113
195
|
bin.should be_a String
|
114
196
|
bin.bytes.should_not nil
|
115
|
-
end
|
197
|
+
# end
|
116
198
|
end
|
117
199
|
it 'should clean up current directory' do
|
118
200
|
unless [:orphan, :package].index(@downloader.type)
|
@@ -124,12 +206,15 @@ describe Oddb2xml::SwissmedicDownloader do
|
|
124
206
|
end
|
125
207
|
context 'fridge' do
|
126
208
|
before(:each) do
|
127
|
-
|
128
|
-
@downloader = Oddb2xml::SwissmedicDownloader.new(:fridge)
|
209
|
+
VCR.insert_cassette('oddb2xml', :tag => :swissmedic, :exclusive => false)
|
129
210
|
common_before
|
211
|
+
@downloader = Oddb2xml::SwissmedicDownloader.new(:fridge)
|
130
212
|
end
|
213
|
+
after(:each) do common_after end
|
131
214
|
context 'download_by for fridge xls' do
|
132
|
-
let(:bin) {
|
215
|
+
let(:bin) {
|
216
|
+
@downloader.download
|
217
|
+
}
|
133
218
|
it 'should return valid Binary-String' do
|
134
219
|
bin.should be_a String
|
135
220
|
bin.bytes.should_not nil
|
@@ -138,50 +223,56 @@ describe Oddb2xml::SwissmedicDownloader do
|
|
138
223
|
end
|
139
224
|
context 'package' do
|
140
225
|
before(:each) do
|
141
|
-
|
226
|
+
VCR.insert_cassette('oddb2xml', :tag => :swissmedic, :exclusive => false)
|
227
|
+
# VCR.insert_cassette('oddb2xml', :tag => :swissmedic, :record => :all)
|
228
|
+
common_before
|
142
229
|
@downloader = Oddb2xml::SwissmedicDownloader.new(:package)
|
143
230
|
end
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
end
|
154
|
-
after(:each) do common_after end
|
155
|
-
it_behaves_like 'any downloader'
|
156
|
-
context 'when download is called' do
|
157
|
-
let(:xml) { @downloader.download }
|
158
|
-
it 'should parse zip to String' do
|
159
|
-
xml.should be_a String
|
160
|
-
xml.length.should_not == 0
|
161
|
-
end
|
162
|
-
it 'should return valid xml' do
|
163
|
-
xml.should =~ /xml\sversion="1.0"/
|
164
|
-
xml.should =~ /medicalInformations/
|
165
|
-
xml.should =~ /content/
|
166
|
-
end
|
167
|
-
it 'should clean up current directory' do
|
168
|
-
expect { xml }.not_to raise_error
|
169
|
-
File.exist?('swissmedic_info.zip').should eq(false)
|
231
|
+
after(:each) do common_after end
|
232
|
+
context 'download_by for package xls' do
|
233
|
+
let(:bin) {
|
234
|
+
@downloader.download
|
235
|
+
}
|
236
|
+
it 'should return valid Binary-String' do
|
237
|
+
bin.should be_a String
|
238
|
+
bin.bytes.should_not nil
|
239
|
+
end
|
170
240
|
end
|
171
241
|
end
|
172
242
|
end
|
173
243
|
|
174
244
|
describe Oddb2xml::EphaDownloader do
|
175
245
|
include ServerMockHelper
|
246
|
+
before(:all) do VCR.eject_cassette end
|
176
247
|
before(:each) do
|
177
|
-
|
248
|
+
VCR.configure do |c|
|
249
|
+
c.before_record(:epha) do |i|
|
250
|
+
if /epha/.match(i.request.uri)
|
251
|
+
puts "#{Time.now}: #{__LINE__}: URI was #{i.request.uri}"
|
252
|
+
lines = i.response.body.split("\n")
|
253
|
+
to_add = lines[0..5]
|
254
|
+
iksnrs = []; Oddb2xml::GTINS_DRUGS.each{ |x| iksnrs << x[4..9] }
|
255
|
+
iksnrs.each{ |iksnr| to_add << lines.find{ |x| x.index(','+iksnr.to_s+',') } }
|
256
|
+
i.response.body = to_add.compact.join("\n")
|
257
|
+
i.response.body = i.response.body.split("\n")[0..5].join("\n")
|
258
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
VCR.insert_cassette('oddb2xml', :tag => :epha)
|
178
263
|
@downloader = Oddb2xml::EphaDownloader.new
|
179
264
|
common_before
|
180
265
|
end
|
181
|
-
after(:each) do
|
266
|
+
after(:each) do
|
267
|
+
common_after
|
268
|
+
end
|
182
269
|
it_behaves_like 'any downloader'
|
270
|
+
|
183
271
|
context 'when download is called' do
|
184
|
-
let(:csv) {
|
272
|
+
let(:csv) {
|
273
|
+
Oddb2xml.add_epha_changes_for_ATC(1, 3)
|
274
|
+
@downloader.download
|
275
|
+
}
|
185
276
|
it 'should read csv as String' do
|
186
277
|
csv.should be_a String
|
187
278
|
csv.bytes.should_not nil
|
@@ -195,16 +286,21 @@ end
|
|
195
286
|
|
196
287
|
describe Oddb2xml::BMUpdateDownloader do
|
197
288
|
include ServerMockHelper
|
289
|
+
before(:all) do VCR.eject_cassette end
|
198
290
|
before(:each) do
|
199
|
-
setup_bm_update_server_mock
|
200
291
|
@downloader = Oddb2xml::BMUpdateDownloader.new
|
292
|
+
VCR.insert_cassette('oddb2xml', :tag => :BMUpdate)
|
201
293
|
common_before
|
202
294
|
end
|
203
|
-
after(:each) do
|
204
|
-
|
295
|
+
after(:each) do
|
296
|
+
common_after
|
297
|
+
end
|
298
|
+
|
205
299
|
it_behaves_like 'any downloader'
|
206
300
|
context 'when download is called' do
|
207
|
-
let(:txt) {
|
301
|
+
let(:txt) {
|
302
|
+
@downloader.download
|
303
|
+
}
|
208
304
|
it 'should read txt as String' do
|
209
305
|
txt.should be_a String
|
210
306
|
txt.bytes.should_not nil
|
@@ -216,40 +312,96 @@ describe Oddb2xml::BMUpdateDownloader do
|
|
216
312
|
end
|
217
313
|
end
|
218
314
|
|
315
|
+
describe Oddb2xml::BagXmlDownloader do
|
316
|
+
include ServerMockHelper
|
317
|
+
before(:all) do VCR.eject_cassette end
|
318
|
+
before(:all) {
|
319
|
+
VCR.configure do |c|
|
320
|
+
c.before_record(:bag_xml) do |i|
|
321
|
+
if i.response.headers['Content-Disposition'] and /XMLPublications.zip/.match(i.request.uri)
|
322
|
+
bag_dir = File.join(Oddb2xml::WorkDir, 'bag')
|
323
|
+
FileUtils.makedirs(Oddb2xml::WorkDir)
|
324
|
+
tmp_zip = File.join(Oddb2xml::WorkDir, 'XMLPublications.zip')
|
325
|
+
File.open(tmp_zip, 'wb+') { |f| f.write(i.response.body) }
|
326
|
+
unzip_files(tmp_zip, bag_dir)
|
327
|
+
bag_tmp = File.join(bag_dir, PREP_XML)
|
328
|
+
puts "#{Time.now}: #{__LINE__}: Parsing #{File.size(bag_tmp)} (#{File.size(bag_tmp)} bytes) will take some time. URI was #{i.request.uri}"
|
329
|
+
doc = REXML::Document.new(File.read(bag_tmp))
|
330
|
+
items = doc.root.elements
|
331
|
+
puts "#{Time.now}: Removing most of the #{items.size} items will take some time"
|
332
|
+
items.each{ |x| items.delete x unless Oddb2xml::GTINS_DRUGS.index(x.elements['Packs/Pack/GTIN'].text); }
|
333
|
+
File.open(bag_tmp, 'wb+') { |f| f.write(doc.to_s.gsub(/\n\s+\n/, "\n")) }
|
334
|
+
puts "Saved #{bag_tmp} (#{File.size(tmp_zip)} bytes)"
|
335
|
+
zip_files(tmp_zip, Dir.glob("#{bag_dir}/*"))
|
336
|
+
puts "Saved #{tmp_zip} (#{File.size(tmp_zip)} bytes)"
|
337
|
+
i.response.body = IO.binread(tmp_zip)
|
338
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
339
|
+
puts "#{Time.now}: response.body is now #{i.response.body.size} bytes long. #{tmp_zip} was #{File.size(tmp_zip)}"
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
VCR.eject_cassette
|
344
|
+
VCR.use_cassette('oddb2xml', :tag => :bag_xml) do
|
345
|
+
@downloader = Oddb2xml::BagXmlDownloader.new
|
346
|
+
end
|
347
|
+
common_before
|
348
|
+
}
|
349
|
+
after(:each) do
|
350
|
+
common_after
|
351
|
+
end
|
352
|
+
|
353
|
+
it_behaves_like 'any downloader'
|
354
|
+
context 'when download is called' do
|
355
|
+
let(:xml) {
|
356
|
+
VCR.use_cassette('oddb2xml', :tag => :bag_xml) do
|
357
|
+
@downloader.download
|
358
|
+
end
|
359
|
+
}
|
360
|
+
it 'should parse zip to string' do
|
361
|
+
xml.should be_a String
|
362
|
+
xml.length.should_not == 0
|
363
|
+
end
|
364
|
+
it 'should return valid xml' do
|
365
|
+
xml.should =~ XML_VERSION_1_0
|
366
|
+
xml.should =~ /Preparations/
|
367
|
+
xml.should =~ /DescriptionDe/
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
219
372
|
describe Oddb2xml::LppvDownloader do
|
220
373
|
include ServerMockHelper
|
221
|
-
before(:
|
222
|
-
|
223
|
-
|
374
|
+
before(:all) do VCR.eject_cassette end
|
375
|
+
before(:all) do
|
376
|
+
VCR.insert_cassette('oddb2xml', :tag => :lppv)
|
224
377
|
common_before
|
378
|
+
@downloader = Oddb2xml::LppvDownloader.new
|
379
|
+
@text = @downloader.download
|
225
380
|
end
|
226
381
|
after(:each) do common_after end
|
227
|
-
|
382
|
+
|
228
383
|
it_behaves_like 'any downloader'
|
229
384
|
context 'when download is called' do
|
230
385
|
let(:txt) { @downloader.download }
|
231
386
|
it 'should read txt as String' do
|
232
|
-
|
233
|
-
|
234
|
-
end
|
235
|
-
it 'should clean up current directory' do
|
236
|
-
expect { txt }.not_to raise_error
|
237
|
-
# File.exist?('oddb2xml_files_lppv.txt').should eq(false)
|
387
|
+
@text.should be_a String
|
388
|
+
@text.bytes.should_not nil
|
238
389
|
end
|
239
390
|
end
|
240
391
|
end
|
241
392
|
|
242
393
|
describe Oddb2xml::MigelDownloader do
|
243
394
|
include ServerMockHelper
|
395
|
+
before(:all) do VCR.eject_cassette end
|
244
396
|
before(:each) do
|
245
|
-
setup_migel_server_mock
|
246
397
|
@downloader = Oddb2xml::MigelDownloader.new
|
398
|
+
VCR.insert_cassette('oddb2xml', :tag => :migel)
|
247
399
|
common_before
|
248
400
|
end
|
249
401
|
after(:each) do common_after end
|
250
|
-
|
402
|
+
|
251
403
|
it_behaves_like 'any downloader'
|
252
|
-
|
404
|
+
context 'when download is called' do
|
253
405
|
let(:bin) { @downloader.download }
|
254
406
|
it 'should read xls as Binary-String' do
|
255
407
|
bin.should be_a String
|
@@ -262,18 +414,85 @@ describe Oddb2xml::MigelDownloader do
|
|
262
414
|
end
|
263
415
|
end
|
264
416
|
|
417
|
+
describe Oddb2xml::ZurroseDownloader do
|
418
|
+
include ServerMockHelper
|
419
|
+
before(:all) do VCR.eject_cassette end
|
420
|
+
before(:each) do
|
421
|
+
VCR.configure do |c|
|
422
|
+
c.before_record(:zurrose) do |i|
|
423
|
+
if /zurrose/i.match(i.request.uri)
|
424
|
+
puts "#{Time.now}: #{__LINE__}: URI was #{i.request.uri}"
|
425
|
+
lines = i.response.body.clone.split("\n")
|
426
|
+
to_add = lines[0..5]
|
427
|
+
Oddb2xml::GTINS_DRUGS.each{ |ean| to_add << lines.find{ |x| x.index(ean.to_s) } }
|
428
|
+
i.response.body = to_add.compact.join("\n")
|
429
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
VCR.insert_cassette('oddb2xml', :tag => :zurrose)
|
434
|
+
@downloader = Oddb2xml::ZurroseDownloader.new
|
435
|
+
common_before
|
436
|
+
end
|
437
|
+
after(:each) do common_after end
|
438
|
+
|
439
|
+
it_behaves_like 'any downloader'
|
440
|
+
context 'when download is called' do
|
441
|
+
let(:dat) { @downloader.download }
|
442
|
+
it 'should read dat as String' do
|
443
|
+
dat.should be_a String
|
444
|
+
dat.bytes.should_not nil
|
445
|
+
end
|
446
|
+
it 'should clean up current directory' do
|
447
|
+
expect { dat }.not_to raise_error
|
448
|
+
File.exist?('oddb2xml_zurrose_transfer.dat').should eq(false)
|
449
|
+
end
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
265
453
|
describe Oddb2xml::MedregbmDownloader do
|
266
454
|
include ServerMockHelper
|
455
|
+
before(:all) do VCR.eject_cassette end
|
267
456
|
before(:each) do
|
457
|
+
VCR.configure do |c|
|
458
|
+
c.before_record(:medreg) do |i|
|
459
|
+
if /medregbm.admin.ch/i.match(i.request.uri)
|
460
|
+
puts "#{Time.now}: #{__LINE__}: URI was #{i.request.uri} containing #{i.response.body.size} bytes"
|
461
|
+
medreg_dir = File.join(Oddb2xml::WorkDir, 'medreg')
|
462
|
+
FileUtils.makedirs(medreg_dir)
|
463
|
+
xlsx_name = File.join(medreg_dir, /ListBetrieb/.match(i.request.uri) ? 'Betriebe.xlsx' : 'Personen.xlsx')
|
464
|
+
File.open(xlsx_name, 'wb+') { |f| f.write(i.response.body) }
|
465
|
+
puts "#{Time.now}: Openening saved #{xlsx_name} (#{File.size(xlsx_name)} bytes) will take some time. URI was #{i.request.uri}"
|
466
|
+
workbook = RubyXL::Parser.parse(xlsx_name)
|
467
|
+
worksheet = workbook[0]
|
468
|
+
idx = 1; to_delete = []
|
469
|
+
while (worksheet.sheet_data[idx])
|
470
|
+
idx += 1
|
471
|
+
next unless worksheet.sheet_data[idx-1][0]
|
472
|
+
to_delete << (idx-1) unless Oddb2xml::GTINS_MEDREG.index(worksheet.sheet_data[idx-1][0].value.to_i)
|
473
|
+
end
|
474
|
+
if to_delete.size > 0
|
475
|
+
puts "#{Time.now}: Deleting #{to_delete.size} of the #{idx} items will take some time"
|
476
|
+
to_delete.reverse.each{ |row_id| worksheet.delete_row(row_id) }
|
477
|
+
workbook.write(xlsx_name)
|
478
|
+
i.response.body = IO.binread(xlsx_name)
|
479
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
480
|
+
puts "#{Time.now}: response.body is now #{i.response.body.size} bytes long. #{xlsx_name} was #{File.size(xlsx_name)}"
|
481
|
+
end
|
482
|
+
end
|
483
|
+
end
|
484
|
+
end
|
268
485
|
common_before
|
269
486
|
end
|
270
487
|
after(:each) do common_after end
|
271
488
|
|
272
489
|
context 'betrieb' do
|
273
490
|
before(:each) do
|
274
|
-
|
491
|
+
VCR.eject_cassette
|
492
|
+
VCR.insert_cassette('oddb2xml', :tag => :medreg)
|
275
493
|
@downloader = Oddb2xml::MedregbmDownloader.new(:company)
|
276
494
|
end
|
495
|
+
after(:each) do common_after end
|
277
496
|
it_behaves_like 'any downloader'
|
278
497
|
context 'download betrieb txt' do
|
279
498
|
let(:txt) { @downloader.download }
|
@@ -287,13 +506,19 @@ describe Oddb2xml::MedregbmDownloader do
|
|
287
506
|
end
|
288
507
|
end
|
289
508
|
end
|
509
|
+
|
290
510
|
context 'person' do
|
291
511
|
before(:each) do
|
292
|
-
|
512
|
+
VCR.eject_cassette
|
513
|
+
VCR.insert_cassette('oddb2xml', :tag => :medreg)
|
293
514
|
@downloader = Oddb2xml::MedregbmDownloader.new(:person)
|
294
515
|
end
|
516
|
+
after(:each) do common_after end
|
295
517
|
context 'download person txt' do
|
296
|
-
let(:txt) {
|
518
|
+
let(:txt) {
|
519
|
+
# this downloads a xlsx file (2.5MB), where we should keep only the first few lines
|
520
|
+
@downloader.download
|
521
|
+
}
|
297
522
|
it 'should return valid String' do
|
298
523
|
txt.should be_a String
|
299
524
|
txt.bytes.should_not nil
|
@@ -306,25 +531,57 @@ describe Oddb2xml::MedregbmDownloader do
|
|
306
531
|
end
|
307
532
|
end
|
308
533
|
|
309
|
-
describe Oddb2xml::
|
534
|
+
describe Oddb2xml::SwissmedicInfoDownloader do
|
310
535
|
include ServerMockHelper
|
536
|
+
before(:all) do VCR.eject_cassette end
|
311
537
|
before(:each) do
|
312
|
-
|
313
|
-
|
538
|
+
VCR.configure do |c|
|
539
|
+
c.before_record(:swissmedicInfo) do |i|
|
540
|
+
puts "#{Time.now}: #{__LINE__}: URI was #{i.request.uri} returning #{i.response.body.size} bytes"
|
541
|
+
if i.response.headers['Content-Disposition']
|
542
|
+
m = /filename=([^\d]+)/.match(i.response.headers['Content-Disposition'][0])
|
543
|
+
if m
|
544
|
+
name = m[1].chomp('_')
|
545
|
+
if /AipsDownload/i.match(name)
|
546
|
+
swissmedic_dir = File.join(Oddb2xml::WorkDir, 'swissmedicInfo')
|
547
|
+
# as reading the unzipped xml takes over 15 minutes using rexml,
|
548
|
+
# we read the xml from the spec/data
|
549
|
+
spec_xml = Dir.glob("#{Oddb2xml::SpecData}/AipsDownload.xml")[0]
|
550
|
+
tmp_zip = File.join(Oddb2xml::WorkDir, 'AipsDownload.zip')
|
551
|
+
File.open(tmp_zip, 'wb+') { |f| f.write(i.response.body) }
|
552
|
+
unzip_files(tmp_zip, swissmedic_dir)
|
553
|
+
FileUtils.cp(spec_xml, Dir.glob("#{swissmedic_dir}/*.xml")[0], :verbose => true)
|
554
|
+
zip_files(tmp_zip, Dir.glob("#{swissmedic_dir}/*.x??"))
|
555
|
+
i.response.body = IO.binread(tmp_zip)
|
556
|
+
i.response.headers['Content-Length'] = i.response.body.size
|
557
|
+
puts "#{Time.now}: #{__LINE__}: response.body is now #{i.response.body.size} bytes long. #{tmp_zip} was #{File.size(tmp_zip)}"
|
558
|
+
end
|
559
|
+
end
|
560
|
+
end
|
561
|
+
end
|
562
|
+
end
|
563
|
+
VCR.eject_cassette
|
564
|
+
VCR.insert_cassette('oddb2xml', :tag => :swissmedicInfo)
|
314
565
|
common_before
|
566
|
+
@downloader = Oddb2xml::SwissmedicInfoDownloader.new
|
315
567
|
end
|
316
568
|
after(:each) do common_after end
|
317
|
-
|
318
569
|
it_behaves_like 'any downloader'
|
319
570
|
context 'when download is called' do
|
320
|
-
let(:
|
321
|
-
it 'should
|
322
|
-
|
323
|
-
|
571
|
+
let(:xml) { @downloader.download }
|
572
|
+
it 'should parse zip to String' do
|
573
|
+
xml.should be_a String
|
574
|
+
xml.length.should_not == 0
|
575
|
+
end
|
576
|
+
it 'should return valid xml' do
|
577
|
+
xml.should =~ XML_VERSION_1_0
|
578
|
+
xml.should =~ /medicalInformations/
|
579
|
+
xml.should =~ /content/
|
324
580
|
end
|
325
581
|
it 'should clean up current directory' do
|
326
|
-
expect {
|
327
|
-
File.exist?('
|
582
|
+
expect { xml }.not_to raise_error
|
583
|
+
File.exist?('swissmedic_info.zip').should eq(false)
|
328
584
|
end
|
329
585
|
end
|
330
586
|
end
|
587
|
+
end
|