Class: ChalkRuby::Http::HttpRequesterChalk

Inherits:
Object
  • Object
show all
Includes:
ChalkRuby::Helpers
Defined in:
lib/chalk_ruby/http/http_requester_chalk.rb

Instance Method Summary collapse

Methods included from ChalkRuby::Helpers

#get_option, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json

Constructor Details

#initialize(requester:) ⇒ HttpRequesterChalk

Returns a new instance of HttpRequesterChalk.

Parameters:



14
15
16
# File 'lib/chalk_ruby/http/http_requester_chalk.rb', line 14

def initialize(requester:)
  @http_requester = requester
end

Instance Method Details

#send_request(method:, host:, path:, timeout:, connect_timeout:, headers:, body:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chalk_ruby/http/http_requester_chalk.rb', line 18

def send_request(method:, host:, path:, timeout:, connect_timeout:, headers:, body:)
  response = @http_requester.send_request(
    host: host,
    method: method,
    path: path,
    body: body && to_json(body: body),
    headers: headers,
    timeout: timeout,
    connect_timeout: connect_timeout
  )

  if response.has_timed_out
    raise ChalkHttpError.new(code: Defaults::ERROR_TIMED_OUT, message: 'Request timed out')
  end

  if response.network_failure
    raise ChalkHttpError.new(code: 502, message: 'Network failure')
  end

  unless success?(http_response_code: response.status)
    begin
      decoded_error = json_to_hash(json: response.error, symbolize_keys: true)
      message = get_option(hash: decoded_error, key: 'description')
    rescue MultiJson::ParseError
      message = response.error
    ensure
      raise ChalkHttpError.new(
        code: response.status,
        message: message
      )
    end
  end
  json_to_hash(json: response.body, symbolize_keys: true)
end