Top Level Namespace

Defined Under Namespace

Modules: TreRegex

Instance Method Summary collapse

Instance Method Details

#download_file(url, limit = 10) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'ext/tre_regex/extconf.rb', line 23

def download_file(url, limit = 10)
  raise 'Too many redirects' if limit.zero?

  uri = URI(url)
  response = Net::HTTP.get_response(uri)

  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPRedirection
    location = response['location']
    puts "Following redirect to #{location}..."
    download_file(location, limit - 1)
  else
    raise "Download failed: #{response.code} #{response.message}"
  end
end