Class: Embiggen::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/embiggen/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ HttpClient

Returns a new instance of HttpClient.



14
15
16
17
18
# File 'lib/embiggen/http_client.rb', line 14

def initialize(uri)
  @uri = uri
  @http = ::Net::HTTP.new(uri.host, uri.port)
  @http.use_ssl = true if uri.scheme == 'https'
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



12
13
14
# File 'lib/embiggen/http_client.rb', line 12

def http
  @http
end

#uriObject (readonly)

Returns the value of attribute uri.



12
13
14
# File 'lib/embiggen/http_client.rb', line 12

def uri
  @uri
end

Instance Method Details

#follow(timeout) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/embiggen/http_client.rb', line 20

def follow(timeout)
  response = request(timeout)
  return unless response.is_a?(::Net::HTTPRedirection)

  response.fetch('Location')
rescue ::Timeout::Error => e
  raise NetworkError.new(
    "Timeout::Error: could not follow #{uri}: #{e.message}", uri
  )
rescue StandardError => e
  raise NetworkError.new(
    "StandardError: could not follow #{uri}: #{e.message}", uri
  )
end