Class: Nomius::Detector::Util::HTTPRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/nomius/detector/util/http_requester.rb

Overview

Encapsulates HTTP request handling. Using Faraday gem. But could be easily replaced with any other HTTP client.

Defined Under Namespace

Classes: NotFound, OK, Unresolved

Constant Summary collapse

HEADERS =

rubocop:enable Lint/EmptyClass

{
  "User-Agent" => "Nomius/#{Nomius::VERSION}"
}.freeze
FALLBACK_STATUS =
Unresolved
RESPONSE_STATUS_RESOLVER =
{
  200 => OK,
  404 => NotFound
}.freeze
RETRY_STATUSES =
[
  400, 401, 403, 408, 409, 418, 425, 429,
  500, 502, 503, 504
].freeze
RETRY_OPTIONS =
{
  max: 5,
  interval: 1,
  interval_randomness: 0.5,
  backoff_factor: 2,
  retry_statuses: RETRY_STATUSES
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, logger:) ⇒ HTTPRequester

Returns a new instance of HTTPRequester.



51
52
53
54
# File 'lib/nomius/detector/util/http_requester.rb', line 51

def initialize(uri:, logger:)
  @uri = uri.to_s
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



45
46
47
# File 'lib/nomius/detector/util/http_requester.rb', line 45

def logger
  @logger
end

#uriObject (readonly)

Returns the value of attribute uri.



45
46
47
# File 'lib/nomius/detector/util/http_requester.rb', line 45

def uri
  @uri
end

Class Method Details

.response_status(uri:, logger:) ⇒ Object



47
48
49
# File 'lib/nomius/detector/util/http_requester.rb', line 47

def self.response_status(uri:, logger:)
  new(uri: uri, logger: logger).response_status
end

Instance Method Details

#response_statusObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/nomius/detector/util/http_requester.rb', line 56

def response_status
  # HEAD request is used to avoid downloading the whole page.
  response = connection.head

  unless RESPONSE_STATUS_RESOLVER.key?(response.status)
    logger.log_error(message: uri, details: response.to_hash.to_json)
  end

  RESPONSE_STATUS_RESOLVER.fetch(response.status, FALLBACK_STATUS)
end