class Cognition::Bot
Attributes
matchers[RW]
plugins[RW]
Public Class Methods
new()
click to toggle source
# File lib/cognition/bot.rb, line 11 def initialize # Default plugin, responds to "ping" with "PONG" and provides help text register(Cognition::Plugins::Default) end
Public Instance Methods
help()
click to toggle source
# File lib/cognition/bot.rb, line 39 def help matchers.flat_map(&:help) end
plugin_names()
click to toggle source
# File lib/cognition/bot.rb, line 35 def plugin_names plugins.map { |p| p.class.name } end
process(msg, metadata = {})
click to toggle source
# File lib/cognition/bot.rb, line 16 def process(msg, metadata = {}) if msg.respond_to? :command process_msg(msg) else process_string(msg.to_s, metadata) end end
register(klass)
click to toggle source
# File lib/cognition/bot.rb, line 24 def register(klass) return false if plugin_names.include? klass.to_s plugins << klass.new(self) end
reset()
click to toggle source
# File lib/cognition/bot.rb, line 29 def reset @matchers = [] @plugins = [] register(Cognition::Plugins::Default) end
Private Instance Methods
not_found(message)
click to toggle source
# File lib/cognition/bot.rb, line 68 def not_found(message) "No such command: #{message}\nUse 'help' for available commands!" end
process_msg(msg)
click to toggle source
# File lib/cognition/bot.rb, line 45 def process_msg(msg) response = false matchers.each do |matcher| if matcher.attempt(msg) != false response = matcher.response break end end response ? response : not_found(msg.command) end
process_string(message, metadata = {})
click to toggle source
# File lib/cognition/bot.rb, line 56 def process_string(message, metadata = {}) process_msg(Cognition::Message.new(message.strip, metadata)) end