Class: Airbrake::Rack::User

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake/rack/user.rb

Overview

Represents an authenticated user, which can be converted to Airbrake’s payload format. Supports Warden and Omniauth authentication frameworks.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ User

Returns a new instance of User.



40
41
42
# File 'lib/airbrake/rack/user.rb', line 40

def initialize(user)
  @user = user
end

Class Method Details

.extract(rack_env) ⇒ Airbrake::Rack::User?

Finds the user in the Rack environment and creates a new user wrapper.

Parameters:

  • rack_env (Hash{String=>Object})

    The Rack environment

Returns:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/airbrake/rack/user.rb', line 12

def self.extract(rack_env)
  # Warden support (including Devise).
  if (warden = rack_env['warden'])
    user = warden.user(run_callbacks: false)
    # Early return to prevent unwanted possible authentication via
    # calling the `current_user` method later.
    # See: https://github.com/airbrake/airbrake/issues/641
    return user ? new(user) : nil
  end

  # Fallback mode (OmniAuth support included). Works only for Rails.
  user = try_current_user(rack_env)
  new(user) if user
end

Instance Method Details

#as_jsonObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/airbrake/rack/user.rb', line 44

def as_json
  user = {}

  user[:id] = try_to_get(:id)
  user[:name] = full_name
  user[:username] = try_to_get(:username)
  user[:email] = try_to_get(:email)

  user = user.delete_if { |_key, val| val.nil? }
  user.empty? ? user : { user: user }
end