Class: Oddb2xml::SwissmedicDownloader

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

Instance Method Summary collapse

Methods inherited from Downloader

#init

Constructor Details

#initialize(type = :orphan) ⇒ SwissmedicDownloader

Returns a new instance of SwissmedicDownloader.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/oddb2xml/downloader.rb', line 220

def initialize(type=:orphan)
  @type = type
  case @type
  when :orphan
    action = "daten/00081/index.html?lang=de"
    @xpath = "//div[@id='sprungmarke0_4']//a[@title='Humanarzneimittel']"
  when :fridge
    action = "daten/00080/00254/index.html?lang=de"
    @xpath = "//table[@class='swmTableFlex']//a[@title='B3.1.35-d.xls']"
  when :package
    action = "daten/00080/00251/index.html?lang=de"
    @xpath = "//div[@id='sprungmarke0_7']//a[@title='Excel-Version Zugelassene Verpackungen*']"
  end
  url = "http://www.swissmedic.ch/#{action}"
  super({}, url)
end

Instance Method Details

#downloadObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/oddb2xml/downloader.rb', line 236

def download
  file = "swissmedic_#{@type}.xls"
  begin
    page = @agent.get(@url)
    if link_node = page.search(@xpath).first
      link = Mechanize::Page::Link.new(link_node, @agent, page)
      response = link.click
      response.save_as(file)
      response = nil # win
    end
    io = File.open(file, 'rb')
    return io.read
  rescue Timeout::Error, Errno::ETIMEDOUT
    retrievable? ? retry : raise
  ensure
    io.close if io and !io.closed?
    if File.exists?(file)
      File.unlink(file)
    end
  end
end