Class: Oddb2xml::SwissmedicDownloader

Inherits:
Downloader
  • Object
show all
Includes:
DownloadMethod
Defined in:
lib/oddb2xml/downloader.rb

Instance Attribute Summary

Attributes inherited from Downloader

#agent, #type, #url

Instance Method Summary collapse

Methods inherited from Downloader

#init, #report_download

Constructor Details

#initialize(type = :orphan, options = {}) ⇒ SwissmedicDownloader

Returns a new instance of SwissmedicDownloader.



283
284
285
286
287
288
289
290
291
292
# File 'lib/oddb2xml/downloader.rb', line 283

def initialize(type=:orphan, options = {})
  @type = type
  @options = options
  case @type
  when :orphan
    @direct_url_link = "https://www.swissmedic.ch/dam/swissmedic/de/dokumente/listen/humanarzneimittel.orphan.xlsx.download.xlsx/humanarzneimittel.xlsx"
  when :package
    @direct_url_link = "https://www.swissmedic.ch/dam/swissmedic/de/dokumente/listen/excel-version_zugelasseneverpackungen.xlsx.download.xlsx/excel-version_zugelasseneverpackungen.xlsx"
  end
end

Instance Method Details

#downloadObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/oddb2xml/downloader.rb', line 293

def download
  @file2save = File.join(Oddb2xml::WorkDir, "swissmedic_#{@type}.xlsx")
  report_download(@url, @file2save)
  if  @options[:calc] and @options[:skip_download] and File.exists?(@file2save) and (Time.now-File.ctime(@file2save)).to_i < 24*60*60
    Oddb2xml.log "SwissmedicDownloader #{__LINE__}: Skip downloading #{@file2save} #{File.size(@file2save)} bytes"
      return File.expand_path(@file2save)
  end
  begin
    FileUtils.rm(File.expand_path(@file2save), :verbose => !defined?(RSpec)) if File.exists?(File.expand_path(@file2save))
    @url = @direct_url_link
    download_as(@file2save, 'w+')
    if @options[:artikelstamm]
      cmd = "ssconvert '#{@file2save}' '#{File.join(Downloads, File.basename(@file2save).sub(/\.xls.*/, '.csv'))}' 2> /dev/null"
      Oddb2xml.log(cmd)
      system(cmd)
    end
    return File.expand_path(@file2save)
  rescue Timeout::Error, Errno::ETIMEDOUT
    retrievable? ? retry : raise
  ensure
    Oddb2xml.download_finished(@file2save, false)
  end
  return File.expand_path(@file2save)
end