class QbertBot::Robot

Attributes

config[R]
listeners[R]
plugins[R]
router[R]
scheduler[R]
slack[R]

Public Class Methods

new() click to toggle source
# File lib/qbert_bot/robot.rb, line 26
def initialize
  @plugins = []
  @listeners = []
end

Public Instance Methods

handle_slack(params) click to toggle source
# File lib/qbert_bot/robot.rb, line 53
def handle_slack(params)
  msg = Message.new(self, params)
  match = find_listener(msg)
  msg.matches = match.matches
  match.proc.call(msg)
  ''
end
handle_web(method, request, params, response) click to toggle source
# File lib/qbert_bot/robot.rb, line 45
def handle_web(method, request, params, response)
  if method == :post && request.path_info == @router.endpoint
    return handle_slack(params)
  end

  @router.exec_route(method, request, params, response)
end
hear(pattern, &block) click to toggle source
# File lib/qbert_bot/robot.rb, line 41
def hear(pattern, &block)
  @listeners << Listener.new(pattern, block)
end
load_config() click to toggle source
# File lib/qbert_bot/robot.rb, line 31
def load_config
  @config = YAML.load_file('qbert.yml')
end
run!() click to toggle source
# File lib/qbert_bot/robot.rb, line 35
def run!
  prepare_workers
  load_plugins
  start_workers
end
say(*args) click to toggle source
# File lib/qbert_bot/robot.rb, line 61
def say(*args)
  @slack.say(*args)
end

Private Instance Methods

find_listener(msg) click to toggle source
# File lib/qbert_bot/robot.rb, line 92
def find_listener(msg)
  @listeners.each do |l|
    if match = msg.text.match(l.pattern)
      return Match.new(l, match)
    end
  end
  return Match.new(BlankListener, [])
end
load_plugins() click to toggle source
# File lib/qbert_bot/robot.rb, line 72
def load_plugins
  Plugin.plugins.each do |klass|
    puts("Loading #{klass}")
    p = klass.new
    p.bot = self
    p.router = router
    p.slack = slack
    p.scheduler = scheduler
    p.register
    @plugins << p
  end
end
prepare_workers() click to toggle source
# File lib/qbert_bot/robot.rb, line 66
def prepare_workers
  @scheduler = Rufus::Scheduler.new
  @router = Router.new(config['http'])
  @slack = Slack.new(config['slack'])
end
start_workers() click to toggle source
# File lib/qbert_bot/robot.rb, line 85
def start_workers
  Thread.new {
    @scheduler.join
  }
  Web.run!
end