class Cognition::Matcher

Attributes

action[R]
help[R]
match_data[R]
response[R]
trigger[R]

Public Class Methods

new(trigger, options = {}, &action) click to toggle source
# File lib/cognition/matcher.rb, line 5
def initialize(trigger, options = {}, &action)
  fail ArgumentError, "matcher must have a trigger" unless trigger
  fail ArgumentError, "matcher must have an action" unless action
  @trigger = trigger
  @help = options[:help] ||= {}
  @action = action
end

Public Instance Methods

attempt(msg) click to toggle source
# File lib/cognition/matcher.rb, line 19
def attempt(msg)
  return false unless matches?(msg)
  run(msg)
end
matches?(msg) click to toggle source
# File lib/cognition/matcher.rb, line 30
def matches?(msg)
  case trigger
  when String
    trigger == msg.command
  when Regexp
    @match_data = trigger.match msg.command
  end
end
run(msg) click to toggle source
# File lib/cognition/matcher.rb, line 24
def run(msg)
  @response = action.call(msg, match_data).to_s
rescue => e
  @response = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
end