Class: Spandx::Core::Http
- Inherits:
-
Object
- Object
- Spandx::Core::Http
- Defined in:
- lib/spandx/core/http.rb
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
Class Method Summary collapse
Instance Method Summary collapse
- #get(uri, default: nil, escape: true) ⇒ Object
-
#initialize(driver: Http.default_driver, retries: 3) ⇒ Http
constructor
A new instance of Http.
- #ok?(response) ⇒ Boolean
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
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
6 7 8 |
# File 'lib/spandx/core/http.rb', line 6 def driver @driver end |
#retries ⇒ Object (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
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
34 35 36 |
# File 'lib/spandx/core/http.rb', line 34 def ok?(response) response.is_a?(Net::HTTPSuccess) end |