Class: Spandx::Core::Http

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver: Http.default_driver, retries: 3) ⇒ Http

Returns a new instance of Http.



8
9
10
11
12
13
14
15
16
17
# File 'lib/spandx/core/http.rb', line 8

def initialize(driver: Http.default_driver, retries: 3)
  @driver = driver
  @retries = retries
  semaphore = Mutex.new
  @circuits = Hash.new do |hash, key|
    semaphore.synchronize do
      hash[key] = Circuit.new(key)
    end
  end
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



6
7
8
# File 'lib/spandx/core/http.rb', line 6

def driver
  @driver
end

#retriesObject (readonly)

Returns the value of attribute retries.



6
7
8
# File 'lib/spandx/core/http.rb', line 6

def retries
  @retries
end

Class Method Details

.default_driverObject



38
39
40
41
42
43
44
45
# File 'lib/spandx/core/http.rb', line 38

def self.default_driver
  @default_driver ||= Net::Hippie::Client.new(
    follow_redirects: 3,
    logger: Spandx.logger,
    open_timeout: 1,
    read_timeout: 5
  )
end

Instance Method Details

#get(uri, default: nil, escape: true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spandx/core/http.rb', line 19

def get(uri, default: nil, escape: true)
  return default if Spandx.airgap?

  circuit = circuit_for(uri)
  return default if circuit.open?

  circuit.attempt do
    driver.with_retry(retries: retries) do |client|
      client.get(escape ? Addressable::URI.escape(uri) : uri)
    end
  end
rescue *Net::Hippie::CONNECTION_ERRORS, URI::InvalidURIError
  default
end

#ok?(response) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/spandx/core/http.rb', line 34

def ok?(response)
  response.is_a?(Net::HTTPSuccess)
end