Sha256: 7f4b223854130dac05df7b71713d7499a1f2bbc47574ee25a8ea0378c6c34394

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module Timber
  module Integrations
    module Rack
      # Reponsible for adding the user context.
      class UserContext
        def initialize(app)
          @app = app
        end

        def call(env)
          user = get_user(env)
          if user
            context = Contexts::User.new(
              id: user_id(user),
              name: user_name(user),
              email: user_email(user)
            )
            CurrentContext.with(context) do
              @app.call(env)
            end
          else
            @app.call(env)
          end
        end

        private
          def get_user(env)
            if env['warden']
              env['warden'].user
            else
              nil
            end
          end

          def user_id(user)
            user.respond_to?(:id) ? user.id : nil
          end

          def user_name(user)
            if user.respond_to?(:name) && user.name.is_a?(String)
              user.name
            elsif user.respond_to?(:first_name) && user.first_name.is_a?(String) && user.respond_to?(:last_name) && user.last_name.is_a?(String)
              "#{user.first_name} #{user.last_name}"
            else
              nil
            end
          end

          def user_email(user)
            if user.respond_to?(:email) && user.email.is_a?(String)
              user.email
            else
              nil
            end
          end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
timber-2.0.1 lib/timber/integrations/rack/user_context.rb
timber-2.0.0 lib/timber/integrations/rack/user_context.rb