Class: SlackRubyBotAuthorization::CommandAudit

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/slack_ruby_bot_authorization/command_audit.rb

Overview

Tracks the number of times per user that a command has been allowed to run or denied to run.

Pretty prints the structures so it’s easy to read.

Constant Summary

Constants included from Utils

Utils::EmptyDenialProc

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#extract_command, #extract_details, #extract_user, #normalize_string

Constructor Details

#initialize(command_name) ⇒ CommandAudit

Returns a new instance of CommandAudit.



10
11
12
13
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 10

def initialize(command_name)
  @name = command_name
  @map = Hash.new { |h, k| h[k] = OpenStruct.new(allowed: 0, denied: 0) }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 8

def name
  @name
end

Instance Method Details

#allow(user) ⇒ Object



15
16
17
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 15

def allow(user)
  stats_for(user).allowed += 1
end

#deny(user) ⇒ Object



19
20
21
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 19

def deny(user)
  stats_for(user).denied += 1
end

#inspectObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 27

def inspect
  len = 15
  string = "#{name}\n"
  @map.keys.each do |user|
    string << user.to_s.ljust(len)
    string << ":allowed => #{stats_for(user).allowed}\n"
    string << ' ' * len
    string << ":denied  => #{stats_for(user).denied}\n"
  end
  string
end

#stats_for(user) ⇒ Object



23
24
25
# File 'lib/slack_ruby_bot_authorization/command_audit.rb', line 23

def stats_for(user)
  @map[normalize_string(user)]
end