Class: Zabbirc::Irc::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbirc/irc/base_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops, message, cmd) ⇒ BaseCommand

Returns a new instance of BaseCommand.



15
16
17
18
19
20
21
# File 'lib/zabbirc/irc/base_command.rb', line 15

def initialize ops, message, cmd
  @ops = ops
  @message = message
  @op = get_op @message
  @cmd = cmd.to_s.strip.gsub(/\s{2,}/," ").freeze
  @args = parse_arguments @cmd
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



14
15
16
# File 'lib/zabbirc/irc/base_command.rb', line 14

def op
  @op
end

Class Method Details

.register_help(command, description) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/zabbirc/irc/base_command.rb', line 5

def self.register_help command, description
  raise ArgumentError, "command `#{command}` already registered" if HELP_FEATURES.key? command
  HELP_FEATURES[command] = description
end

Instance Method Details

#help_featuresObject



10
11
12
# File 'lib/zabbirc/irc/base_command.rb', line 10

def help_features
  HELP_FEATURES
end

#parse_arguments(cmd) ⇒ Object



23
24
25
26
27
# File 'lib/zabbirc/irc/base_command.rb', line 23

def parse_arguments cmd
  cmd.scan(/'[a-zA-Z0-9_\-,# ]+'|[a-zA-Z0-9_\-,#]+/).collect do |x|
    x.match(/[a-zA-Z0-9_\-,# ]+/)[0]
  end
end

#runObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/zabbirc/irc/base_command.rb', line 29

def run
  return unless authenticated?
  begin
    perform # perform method should be implemented in subclass
  rescue Zabbix::NotConnected => e
    reply "#{e.to_s}"
  rescue UserInputError => e
    reply e.reply_messages
  end
end