class Slnky::Command::Base

Attributes

commands[R]

Public Class Methods

command(name, banner, desc) click to toggle source
# File lib/slnky/command.rb, line 74
def command(name, banner, desc)
  @commands ||= [Slnky::Command::Processor.new('help', 'print help', 'help [options]')]
  @commands << Slnky::Command::Processor.new(name, banner, desc)
end
new() click to toggle source
# File lib/slnky/command.rb, line 9
def initialize
  @commands = self.class.commands
end

Public Instance Methods

config() click to toggle source
# File lib/slnky/command.rb, line 63
def config
  Slnky.config
end
handle(event, data, response=nil) click to toggle source
# File lib/slnky/command.rb, line 13
def handle(event, data, response=nil)
  begin
    req = Slnky::Command::Request.new(data)
    res = response || Slnky::Command::Response.new(data.response, name)
    log.response = res
    res.start!

    if event == 'slnky.help.command'
      handle_help(req, res)
    else
      handle_command(req, res)
    end

  rescue => e
    puts "ERROR: #{e.message}"
    log.error "failed to run command: #{name}: #{data.command}: #{e.message} at #{e.backtrace.first}"
  ensure
    log.response = false
    res.done! if res
  end
end
handle_command(req, res) click to toggle source
# File lib/slnky/command.rb, line 45
def handle_command(req, res)
  begin
    processor = @commands.select { |c| c.name == req.command }.first
    if processor
      options = processor.process(req.args)
      self.send("handle_#{processor.name}", req, res, options)
    else
      log.error "unknown command: #{req.command}"
    end
  rescue Docopt::Exit => e
    log.info e.message
  end
end
handle_help(req, res, opts={}) click to toggle source
# File lib/slnky/command.rb, line 35
def handle_help(req, res, opts={})
  if @commands && @commands.count > 0
    @commands.each do |command|
      log.info "#{name} #{command.name}: #{command.banner}"
    end
  else
    log.info "#{name} has no additional commands"
  end
end
log() click to toggle source
# File lib/slnky/command.rb, line 67
def log
  Slnky.log
end
name() click to toggle source
# File lib/slnky/command.rb, line 59
def name
  @name ||= self.class.name.split('::')[1].downcase
end