module Airbrake::Rails::ControllerMethods

Constants

SLASH

Public Instance Methods

airbrake_request_data() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 7
def airbrake_request_data
  {
    :parameters       => airbrake_filter_if_filtering(to_hash(params)),
    :session_data     => airbrake_filter_if_filtering(airbrake_session_data),
    :controller       => params[:controller],
    :action           => params[:action],
    :url              => airbrake_request_url,
    :cgi_data         => airbrake_filter_if_filtering(request.env),
    :user             => airbrake_current_user
  }
end

Private Instance Methods

airbrake_current_user() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 110
def airbrake_current_user
  user = fetch_user

  if user
    Airbrake.configuration.user_attributes.map(&:to_sym).inject({}) do |hsh, attr|
      begin
        hsh[attr] = user.send(attr) if user.respond_to? attr
        hsh
      rescue
        hsh
      end
    end
  else
    {}
  end
end
airbrake_filter_if_filtering(hash) click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 52
def airbrake_filter_if_filtering(hash)
  return hash if ! hash.is_a?(Hash)

  if respond_to?(:filter_parameters) # Rails 2
    filter_parameters(hash)
  elsif rails_3_or_4?
    filter_rails3_parameters(hash)
  else
    hash
  end
end
airbrake_local_request?() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 37
def airbrake_local_request?
  if defined?(::Rails.application.config)
    ::Rails.application.config.consider_all_requests_local || (request.local? && (!request.env["HTTP_X_FORWARDED_FOR"]))
  else
    consider_all_requests_local || (local_request? && (!request.env["HTTP_X_FORWARDED_FOR"]))
  end
end
airbrake_request_url() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 96
def airbrake_request_url
  url = "#{request.protocol}#{request.host}"

  unless [80, 443].include?(request.port)
    url << ":#{request.port}"
  end

  unless request.fullpath[0] == SLASH
    url << SLASH
  end

  url << request.fullpath
end
airbrake_session_data() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 84
def airbrake_session_data
  if session
    if session.respond_to?(:to_hash)
      session.to_hash
    else
      session.data
    end
  else
    {:session => 'no session found'}
  end
end
fetch_user() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 127
def fetch_user
  if defined?(current_user)
    current_user
  elsif defined?(current_member)
    current_member
  else
    nil
  end
rescue
  nil
ensure
  # The Airbrake middleware is first in the chain, before ActiveRecord::ConnectionAdapters::ConnectionManagement
  # kicks in to do its thing. This can cause the connection pool to run out of connections.
  if defined?(ActiveRecord::Base) && ActiveRecord::Base.respond_to?(:clear_active_connections!)
    ActiveRecord::Base.clear_active_connections!
  end
end
filter_rails3_parameters(hash) click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 68
def filter_rails3_parameters(hash)
  ActionDispatch::Http::ParameterFilter.new(
    ::Rails.application.config.filter_parameters
  ).filter(recursive_stringify_keys(hash))
end
notify_airbrake(hash_or_exception) click to toggle source

This method should be used for sending manual notifications while you are still inside the controller. Otherwise it works like Airbrake.notify.

# File lib/airbrake/rails/controller_methods.rb, line 31
def notify_airbrake(hash_or_exception)
  unless airbrake_local_request?
    Airbrake.notify_or_ignore(hash_or_exception, airbrake_request_data)
  end
end
rails_3_or_4?() click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 64
def rails_3_or_4?
  defined?(::Rails.version) && ::Rails.version =~ /\A[34]/
end
recursive_stringify_keys(hash) click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 74
def recursive_stringify_keys(hash)
  hash = hash.stringify_keys
  hash.each do |k, v|
    if v.is_a?(Hash)
      hash[k] = v.respond_to?(:stringify_keys) ? recursive_stringify_keys(v) : nil # Rack::Session::Abstract::SessionHash has a stringify_keys method we should not call
    end
  end
  hash
end
to_hash(params) click to toggle source
# File lib/airbrake/rails/controller_methods.rb, line 21
def to_hash(params)
  # Rails <= 4
  return params.to_hash if params.respond_to?(:to_hash)

  # Rails >= 5
  params.to_unsafe_h
end