Sha256: cfaac0ff4684c97a931cd7932454790bf5a4ce7f2dba896d0172a622ea8a848a

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module VelocityAudited
  class Sweeper
    STORED_DATA = {
      current_remote_address: :remote_ip,
      current_request_uuid: :request_uuid,
      current_user: :current_user,
      origin: :origin,
      user_agent: :user_agent,
      ip: :ip,
      uid: :uid,
      client: :client
    }

    delegate :store, to: ::VelocityAudited

    def around(controller)
      self.controller = controller
      STORED_DATA.each { |k, m| store[k] = send(m) }
      yield
    ensure
      self.controller = nil
      STORED_DATA.keys.each { |k| store.delete(k) }
    end

    def current_user
      lambda { controller.send(VelocityAudited.current_user_method) if controller.respond_to?(VelocityAudited.current_user_method, true) }
    end

    def remote_ip
      controller.try(:request).try(:remote_ip)
    end

    def request_uuid
      controller.try(:request).try(:uuid)
    end

    def origin
      controller.try(:request).try(:headers)['Origin']
    end

    def user_agent
      controller.try(:request).try(:headers)['User-Agent']
    end

    def ip
      controller.try(:request).try(:headers)['HTTP_X_FORWARDED_FOR']
    end

    def uid
      controller.try(:request).try(:headers)['uid']
    end

    def client
      controller.try(:request).try(:headers)['client']
    end

    def controller
      store[:current_controller]
    end

    def controller=(value)
      store[:current_controller] = value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
velocity_audited-6.0.1 lib/audited/sweeper.rb
velocity_audited-5.1.6 lib/audited/sweeper.rb
velocity_audited-5.1.5 lib/audited/sweeper.rb