class Gtin2atc::Swissmedic

Public Class Methods

get_latest() click to toggle source
# File lib/gtin2atc/builder.rb, line 446
def Swissmedic.get_latest
  Util.debug_msg 'test'
  @index_url = 'https://www.swissmedic.ch/arzneimittel/00156/00221/00222/00230/index.html?lang=de'
  Util.debug_msg("swissmedicPlugin @index_url #{@index_url}")
  latest_name, target =  Util.get_latest_and_dated_name('Packungen', '.xlsx')
  if File.exist?(target)
    Util.debug_msg "#{__FILE__}: #{__LINE__} skip writing #{target} as it already exists and is #{File.size(target)} bytes."
    return target
  end
  Util.debug_msg "target #{target} #{latest_name}"
  latest = ''
  if(File.exist? latest_name)
    latest = File.read latest_name
    return latest_name
  end

  agent=Mechanize.new
  page = agent.get @index_url
  links = page.links.select do |link|
    /Packungen/iu.match link.attributes['title']
  end
  link = links.first or raise "could not identify url to Packungen.xlsx"
  file = agent.get(link.href)
  download = file.body

  if(download[-1] != ?\n)
    download << "\n"
  end
  if(!File.exist?(latest_name) or download.size != File.size(latest_name))
    File.open(target, 'w') { |fh| fh.puts(download) }
    msg = "#{__FILE__}: #{__LINE__} updated download.size is #{download.size} -> #{target} #{File.size(target)}"
    msg += "#{target} now #{File.size(target)} bytes != #{latest_name} #{File.size(latest_name)}" if File.exists?(latest_name)
    Util.debug_msg(msg)
    target
  else
    Util.debug_msg "#{__FILE__}: #{__LINE__} skip writing #{target} as #{latest_name} is #{File.size(latest_name)} bytes. Returning latest"
    nil
  end
end