Class: Issola::Command::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/issola/command/handler.rb

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



4
5
6
# File 'lib/issola/command/handler.rb', line 4

def initialize
  @commands = {}
end

Instance Method Details

#handle(event) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/issola/command/handler.rb', line 18

def handle(event)
  tokens = event.message.content.split(' ')
  puts "Checking for command in message: #{ tokens.inspect }"

  # Get rid of command prefix
  key = tokens.first[1..-1]
  cmd = @commands[key]
  if cmd
    puts "Found command!"
    cmd.action.call(event)
  end
end

#register {|builder| ... } ⇒ Object

Yields:

  • (builder)

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/issola/command/handler.rb', line 8

def register
  builder = Builder.new

  yield builder
  raise ArgumentError, 'Unable to register command, required attribute(s) missing.' unless builder.valid?

  cmd = builder.command
  @commands[cmd.key] = cmd
end