Module: LoggedRequest

Included in:
RestClient::Request
Defined in:
lib/rest_client/core_ext/logged_request.rb

Constant Summary collapse

PATTERN =
'rest_client.request'.freeze
DEFAULT_CONTENT_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Instance Method Details

#logged_request(opts = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rest_client/core_ext/logged_request.rb', line 5

def logged_request(opts = {}, &block)
  # We intend on using the following variables in this method's
  # ensure block. This means that we must take care to ensure
  # that we check for nil against these variables whenever they
  # are accessed.
  response, exception = nil, nil
  started = Time.now
  response = execute(opts, &block)
rescue StandardError => ex
  exception = ex.class.to_s
  response = ex.respond_to?(:response) && ex.response
  raise ex # Re-raise the exception, we just wanted to capture it
ensure
  time = (Time.now - started).round(2)
  content_type = content_type_from_headers(opts[:headers])
  payload = filter(content_type).new(data: opts[:payload]).filter
  opts[:headers].reject! { |k, _| k.to_s.casecmp('authorization').zero? } if opts[:headers]
  params = opts.except(:user, :password, :payload).merge(payload: payload)
  ActiveSupport::Notifications.instrument PATTERN,
    log_data(params, response, exception, time)
end