Module: Honeybadger::Util::RequestHash Private

Defined in:
lib/honeybadger/util/request_hash.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constructs a request hash from a Rack::Request matching the /v1/notices API specification.

Constant Summary collapse

HTTP_HEADER_PREFIX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'HTTP_'.freeze
CGI_WHITELIST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w(
  AUTH_TYPE
  CONTENT_LENGTH
  CONTENT_TYPE
  GATEWAY_INTERFACE
  HTTPS
  REMOTE_ADDR
  REMOTE_HOST
  REMOTE_IDENT
  REMOTE_USER
  REQUEST_METHOD
  SERVER_NAME
  SERVER_PORT
  SERVER_PROTOCOL
  SERVER_SOFTWARE
).freeze

Class Method Summary collapse

Class Method Details

.extract_cgi_data(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
67
68
69
70
# File 'lib/honeybadger/util/request_hash.rb', line 64

def self.extract_cgi_data(request)
  request.env.each_with_object({}) do |(k,v), env|
    next unless k.is_a?(String)
    next unless k.start_with?(HTTP_HEADER_PREFIX) || CGI_WHITELIST.include?(k)
    env[k] = v
  end
end

.extract_params(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
52
53
# File 'lib/honeybadger/util/request_hash.rb', line 49

def self.extract_params(request)
  (request.env['action_dispatch.request.parameters'] || request.params).to_hash || {}
rescue => e
  { error: "Failed to access params -- #{e}" }
end

.extract_session(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
58
59
60
61
62
# File 'lib/honeybadger/util/request_hash.rb', line 55

def self.extract_session(request)
  request.session.to_hash
rescue => e
  # Rails raises ArgumentError when `config.secret_token` is missing, and
  # ActionDispatch::Session::SessionRestoreError when the session can't be
  # restored.
  { error: "Failed to access session data -- #{e}" }
end

.extract_url(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
# File 'lib/honeybadger/util/request_hash.rb', line 43

def self.extract_url(request)
  request.env['honeybadger.request.url'] || request.url
rescue => e
  "Failed to access URL -- #{e}"
end

.from_env(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/honeybadger/util/request_hash.rb', line 27

def self.from_env(env)
  return {} unless defined?(::Rack::Request)
  return {} unless env

  hash, request = {}, ::Rack::Request.new(env.dup)

  hash[:url] = extract_url(request)
  hash[:params] = extract_params(request)
  hash[:component] = hash[:params]['controller']
  hash[:action] = hash[:params]['action']
  hash[:session] = extract_session(request)
  hash[:cgi_data] = extract_cgi_data(request)

  hash
end