Class: Oddb2xml::SwissmedicDownloader

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

Constant Summary collapse

BASE_URL =
"https://www.swissmedic.ch"

Instance Attribute Summary

Attributes inherited from Downloader

#agent, #file2save, #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.



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/oddb2xml/downloader.rb', line 266

def initialize(type = :orphan, options = {})
  url = BASE_URL + "/swissmedic/de/home/services/listen_neu.html"
  doc = Nokogiri::HTML(Oddb2xml.uri_open(url))
  @type = type
  @options = options
  case @type
  when :orphan
    @direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Humanarzneimittel mit Status Orphan Drug/.match(x.children.text) }.attributes["href"].value
  when :package
    @direct_url_link = BASE_URL + doc.xpath("//a").find { |x| /Zugelassene Packungen/.match(x.children.text) }.attributes["href"].value
  end
end

Instance Method Details

#downloadObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/oddb2xml/downloader.rb', line 279

def download
  @file2save = File.join(DOWNLOADS, "swissmedic_#{@type}.xlsx")
  report_download(@url, @file2save)
  if @options[:calc] && @options[:skip_download] && File.exist?(@file2save) && ((Time.now - File.ctime(@file2save)).to_i < 24 * 60 * 60) && Oddb2xml.valid_zip?(@file2save)
    Oddb2xml.log "SwissmedicDownloader #{__LINE__}: Skip downloading #{@file2save} #{File.size(@file2save)} bytes"
    return File.expand_path(@file2save)
  end
  begin
    @url = @direct_url_link
    download_as(@file2save, "w+")
    # The Swissmedic file is an .xlsx (a ZIP). Downloads through scanning
    # proxies are sometimes truncated (valid header, missing EOCD), which
    # would later crash RubyXL with a cryptic rubyzip error. Verify the
    # archive is complete and just fetch it again if not. (issue #121)
    unless Oddb2xml.valid_zip?(@file2save)
      raise Oddb2xml::IncompleteDownloadError,
        "Swissmedic #{@type} xlsx is empty or truncated (#{File.size(@file2save)} bytes)"
    end
    if @options[:artikelstamm]
      # ssconvert is in the package gnumeric (Debian)
      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, Oddb2xml::IncompleteDownloadError => error
    if retrievable?
      Oddb2xml.log("Retrying Swissmedic #{@type} download: #{error.message}")
      retry
    end
    raise
  ensure
    Oddb2xml.download_finished(@file2save, false)
  end
  File.expand_path(@file2save)
end