Class: Rack::DevInsight::ApiRecorder

Inherits:
BaseRecorder show all
Defined in:
lib/rack/dev_insight/recorder/api_recorder.rb

Instance Method Summary collapse

Instance Method Details

#record(net_http:, request:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/dev_insight/recorder/api_recorder.rb', line 8

def record(net_http:, request:)
  return yield if Context.current.nil?

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  response = yield
  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
  Context.current.result.add_api(
    method: request.method,
    url: request.uri || "#{net_http.address}:#{net_http.port}#{request.path}",
    request_headers: request.each_header.map { |field, value| Result.build_header(field, value) },
    request_body: request.body,
    status: response.code.to_i,
    response_headers: response.each_header.map { |field, value| Result.build_header(field, value) },
    response_body: response.body,
    backtrace: get_backtrace,
    duration: format('%.2f', duration * 1000).to_f,
  )
  response
end