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.


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

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.


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

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.


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

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.


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

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
# File 'lib/honeybadger/util/request_hash.rb', line 27

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

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

  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