Class: HumanTone::Http

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

Constant Summary collapse

NETWORK_EXCEPTIONS =
[
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  SocketError,
  OpenSSL::SSL::SSLError,
  EOFError,
  IOError
].freeze
TIMEOUT_EXCEPTIONS =
[
  Net::OpenTimeout,
  Net::ReadTimeout,
  Net::WriteTimeout
].freeze
REDACTED_AUTH =
'Bearer [REDACTED]'

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key:, timeout: 120, user_agent_suffix: nil, logger: nil) ⇒ Http

Returns a new instance of Http.



33
34
35
36
37
38
39
# File 'lib/humantone/http.rb', line 33

def initialize(base_url:, api_key:, timeout: 120, user_agent_suffix: nil, logger: nil)
  @base_url = base_url
  @api_key = api_key
  @timeout = timeout
  @user_agent = UserAgent.build(suffix: user_agent_suffix)
  @logger = logger || Logger.new(IO::NULL)
end

Instance Method Details

#request(method:, path:, body: nil) ⇒ Hash{Symbol=>Object}

Returns { status:, headers:, body_string: }.

Returns:

  • (Hash{Symbol=>Object})

    { status:, headers:, body_string: }



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/humantone/http.rb', line 42

def request(method:, path:, body: nil)
  uri = URI.parse("#{@base_url}#{path}")
  req = build_request(method: method, path: path, body: body)

  log_request(method: method, path: path, host: uri.host)
  response = perform(uri: uri, req: req)
  log_response(response: response)

  {
    status: response.code.to_i,
    headers: normalize_headers(response),
    body_string: response.body.to_s
  }
end