otvorenesudy/otvorenesudy-api

View on GitHub
lib/downloader.rb

Summary

Maintainability
A
0 mins
Test Coverage
F
25%
class Downloader
  def self.get(url)
    params = extract_params(url)

    10.times do |n|
      begin
        return Curl.get(url, params)
      rescue Exception => e
        warn "Retrying download (#{e.message}) ... ##{n + 1}"

        sleep 5
      end
    end

    raise StandardError.new("Failed to download #{url}")
  end

  def self.extract_params(url)
    match = url.match(/\?(.*)\z/)

    return {} unless match

    Hash[match[1].split('&').map { |s| s.split('=') }]
  end
end