Class: Marvin::CommandHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/marvin/command_handler.rb

Overview

A Simple Marvin handler based on processing commands, similar in design to MatzBot.

Instance Attribute Summary

Attributes inherited from Base

#client, #from, #logger, #options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#addressed?, #ctcp, event_handlers_for, #from_channel?, #from_user?, #halt!, #handle, #handle_incoming_numeric, #initialize, on_event, on_numeric, #pm, register!, #reply, #say, #setup_defaults, uses_datastore

Constructor Details

This class inherits a constructor from Marvin::Base

Class Method Details

.exposes(*args) ⇒ Object



14
15
16
17
# File 'lib/marvin/command_handler.rb', line 14

def exposes(*args)
  self.exposed_methods ||= []
  self.exposed_methods += args.map { |a| a.to_sym }.flatten
end

Instance Method Details

#check_for_commandsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/marvin/command_handler.rb', line 26

def check_for_commands
  data, command = nil, nil
  if self.from_channel?
    logger.debug "Processing command in channel"
    split_message = options.message.split(" ", 3)
    prefix = split_message.shift
    # Return if in channel and it isn't address to the user.
    return unless prefix == "#{self.client.nickname}:"
    command, data = split_message # Set remaining.
  else
    command, data = options.message.split(" ", 2)
  end
  # Double check for sanity
  return if command.blank?
  command_name = extract_command_name(command)
  unless command_name.nil?
    logger.debug "Command Exists - processing"
    # Dispatch the command.
    self.send(command_name, data.to_a) if self.respond_to?(command_name)
  end
end

#extract_command_name(command) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/marvin/command_handler.rb', line 48

def extract_command_name(command)
  prefix_length = self.command_prefix.to_s.length
  has_prefix = command[0...prefix_length] == self.command_prefix.to_s
  logger.debug "Debugging, prefix is #{prefix_length} characters, has prefix? = #{has_prefix}"
  if has_prefix
    # Normalize the method name
    method_name = command[prefix_length..-1].to_s.underscore.to_sym
    logger.debug "Computed method name is #{method_name.inspect}"
    return method_name if self.exposed_methods.to_a.include?(method_name)
  end
end