Class: SlackRubyBotAuthorization::Commands

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

Overview

Tracks all Command instances and maintains the global default denial handler.

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

#initializeCommands

Returns a new instance of Commands.



16
17
18
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 16

def initialize
  reset!
end

Instance Attribute Details

#default_denial_handlerObject

Returns the value of attribute default_denial_handler.



14
15
16
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 14

def default_denial_handler
  @default_denial_handler
end

Instance Method Details

#add_default_denial_handler(aproc) ⇒ Object

Default denial proc called when a Command doesn’t have a specific denial handler set.

When the specific Command has its own handler, that handler may return true to allow the default handler to also run. To prevent the default handler from running, the Command-specific denial handler should return false.



61
62
63
64
65
66
67
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 61

def add_default_denial_handler(aproc)
  unless aproc.arity == 3
    raise(DenialProcArityException, 'Must accept 3 arguments')
  end

  @default_denial_handler = aproc
end

#call_denial_handler(client, data, match) ⇒ Object



81
82
83
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 81

def call_denial_handler(client, data, match)
  @default_denial_handler.call(client, data, match)
end

#command_namesObject

Returns list of all commands that were ever registered. This will even return commands that are no longer associated with a role, so the output from this command will be a superset of the Roles#command_names method.



50
51
52
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 50

def command_names
  @commands.values.map(&:name).sort
end

#find(command_string, default = true) ⇒ Object



41
42
43
44
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 41

def find(command_string, default = true)
  command = @commands[normalize_string(command_string)]
  default ? (command || DefaultCommand) : command
end

#find_or_make(*command_strings) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 30

def find_or_make(*command_strings)
  set = Set.new
  command_strings.each do |command_string|
    command = find(command_string, false)

    # when not found, make a new Command instance
    set << (command || new_command(command_string))
  end
  set.to_a
end

#new_command(command_name) ⇒ Object



25
26
27
28
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 25

def new_command(command_name)
  command_name = normalize_string(command_name)
  @commands[command_name] = Command.new(command_name)
end

#remove_denial_handler_from_command(command_string) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 69

def remove_denial_handler_from_command(command_string)
  command = find(command_string)
  unless command
    raise(
      UnknownCommandException,
      "No command named '#{command_string}'"
    )
  end

  command.remove_denial_handler
end

#reset!Object



20
21
22
23
# File 'lib/slack_ruby_bot_authorization/commands.rb', line 20

def reset!
  @commands = {}
  @default_denial_handler = EmptyDenialProc
end