Class: Keiyaku::HTTPAdapter

Inherits:
Object
  • Object
show all
Includes:
_Adapter
Defined in:
lib/keiyaku/adapters/http.rb,
sig/keiyaku.rbs

Overview

Loaded only by require "keiyaku/adapters/http". Wraps the http.rb gem.

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, timeout: 15) ⇒ HTTPAdapter

Returns a new instance of HTTPAdapter.

Parameters:

  • client (Object) (defaults to: nil)
  • timeout: (timeout) (defaults to: 15)


20
21
22
23
# File 'lib/keiyaku/adapters/http.rb', line 20

def initialize(client = nil, timeout: 15)
  open, read = Keiyaku.timeouts(timeout)
  @client = client || HTTP.timeout(connect: open, read:)
end

Instance Method Details

#call(verb, uri, headers, body) ⇒ Object



25
26
27
28
29
30
# File 'lib/keiyaku/adapters/http.rb', line 25

def call(verb, uri, headers, body)
  response = @client.headers(headers).request(verb, uri, body.nil? ? {} : { body: })
  [response.code, response.headers.to_h, response.body.to_s]
rescue HTTP::ConnectionError, HTTP::TimeoutError, *TRANSPORT_ERRORS => e
  raise ConnectionError, "#{verb.to_s.upcase} #{uri}: #{e.message}"
end