Class: NativeExtFetcher::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/native_ext_fetcher/http.rb

Constant Summary collapse

MAX_REDIRECTS =
5
MAX_RETRIES =
3
MaxRedirect =
Class.new(StandardError)
MaxRetries =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Http

Returns a new instance of Http.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/native_ext_fetcher/http.rb', line 14

def initialize(options = {})
  @max_redirects = options[:max_redirects] || MAX_REDIRECTS
  @max_retries   = options[:max_retries] || MAX_RETRIES

  if http_proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
    uri = URI.parse(http_proxy)

    @proxy_host, @proxy_port = uri.host, uri.port
    @proxy_user, @proxy_pass = uri.userinfo.split(/:/) if uri.userinfo
  end
end

Instance Method Details

#get_file(out, uri) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/native_ext_fetcher/http.rb', line 26

def get_file(out, uri)
  remaining_attempts = @max_retries

  @max_redirects.times do |i|
    begin
      host, port, path = deconstruct_uri(uri)
      status, result = http_get(out, host, port, path)

      case status
      when :success
        return result

      else uri = result
      end

    rescue => e
      remaining_attempts -= 1

      unless remaining_attempts.zero?
        sleep 2
        retry
      end

      raise MaxRetries, "max retries; #{e.message}"
    end
  end

  raise MaxRedirect, 'max redirects hit'
end