Class: HTTPClient

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/get/commons/http_client.rb

Overview

Client HTTP which allows to perform GET request and follow redirections.

Constant Summary collapse

MAX_ATTEMPTS =

Number of attempts to try when following a redirection link.

10

Instance Method Summary collapse

Instance Method Details

#http_get_request(address) ⇒ Object

Perform a get request to an address, following the redirections at most MAX_ATTEMPTS times.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/get/commons/http_client.rb', line 31

def http_get_request(address)
  uri = URI.parse(address)

  # Code based on https://shadow-file.blogspot.com/2009/03/handling-http-redirection-in-ruby.html
  attempts = 0
  until attempts >= MAX_ATTEMPTS
    attempts += 1

    resp = build_http(uri)
           .request(Net::HTTP::Get.new(uri.path == '' ? '/' : uri.path))
    return resp if resp.is_a?(Net::HTTPSuccess) || resp.header['location'].nil?

    uri = updated_uri(uri, resp.header['location'])
  end
  nil
end

#response_error_message(response) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/get/commons/http_client.rb', line 48

def response_error_message(response)
  if response.nil?
    'too many redirections'
  else
    "#{response.code} #{response.message}"
  end
end