Class: GDK::HTTPHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/gdk/http_helper.rb

Constant Summary collapse

HTTP_SUCCESS_CODES =
%w[200 301 302].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, read_timeout: 5, open_timeout: 5, cache_response: true) ⇒ HTTPHelper

Returns a new instance of HTTPHelper.



11
12
13
14
15
16
17
18
# File 'lib/gdk/http_helper.rb', line 11

def initialize(uri, read_timeout: 5, open_timeout: 5, cache_response: true)
  raise 'uri needs to be an instance of URI' unless uri.is_a?(URI)

  @uri = uri
  @read_timeout = read_timeout
  @open_timeout = open_timeout
  @cache_response = cache_response
end

Instance Attribute Details

#last_response_reasonObject (readonly)

Returns the value of attribute last_response_reason.



7
8
9
# File 'lib/gdk/http_helper.rb', line 7

def last_response_reason
  @last_response_reason
end

Instance Method Details

#get_up?(codes_to_consider_up: HTTP_SUCCESS_CODES) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/gdk/http_helper.rb', line 31

def get_up?(codes_to_consider_up: HTTP_SUCCESS_CODES)
  response_to_process = cache_response ? cached_http_get_response : http_get_response
  return false unless response_to_process

  codes_to_consider_up.include?(response_to_process.code)
end

#head_up?(codes_to_consider_up: HTTP_SUCCESS_CODES) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/gdk/http_helper.rb', line 24

def head_up?(codes_to_consider_up: HTTP_SUCCESS_CODES)
  response_to_process = cache_response ? cached_http_head_response : http_head_response
  return false unless response_to_process

  codes_to_consider_up.include?(response_to_process.code)
end

#up?(codes_to_consider_up: HTTP_SUCCESS_CODES) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/gdk/http_helper.rb', line 20

def up?(codes_to_consider_up: HTTP_SUCCESS_CODES)
  get_up?(codes_to_consider_up: codes_to_consider_up)
end