class ResponderBot::Handler

Attributes

matcher_list[RW]
response_handler[RW]
body[RW]
respondable[RW]

Public Class Methods

handle_response(&block) click to toggle source
# File lib/responder_bot/handler.rb, line 41
def self.handle_response(&block)
  self.response_handler = block
end
inherited(child) click to toggle source
# File lib/responder_bot/handler.rb, line 3
def self.inherited(child)
  child.class_eval do
    class << self
      attr_accessor :matcher_list, :response_handler
    end
    self.matcher_list = MatcherList.new
  end
end
matcher(new_matchers) click to toggle source
# File lib/responder_bot/handler.rb, line 45
def self.matcher(new_matchers)
  new_matchers.each_pair do |name, match|
    matcher_list.define_matcher(name, match)
  end
end
new(respondable) click to toggle source
# File lib/responder_bot/handler.rb, line 14
def initialize(respondable)
  self.respondable = respondable
  self.body = if respondable.is_a?(String)
                respondable
              else
                respondable.body
              end
  instance_eval(&self.class.response_handler)
end

Public Instance Methods

handle_response!() click to toggle source
# File lib/responder_bot/handler.rb, line 28
def handle_response!
  if understands_reply?
    matcher_list.action_for(body).call
  else
    raise ResponderBot::UnmatchedResponse,
          "No matcher caught input '#{body}'."
  end
end
matcher_list() click to toggle source
# File lib/responder_bot/handler.rb, line 37
def matcher_list
  self.class.matcher_list
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/responder_bot/handler.rb, line 51
def method_missing(method_name, *args, &block)
  if self.class.matcher_list.respond_to?(method_name)
    self.class.matcher_list.send(method_name, *args, &block)
  elsif respondable.respond_to?(method_name)
    respondable.send(method_name, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name) click to toggle source
# File lib/responder_bot/handler.rb, line 61
def respond_to_missing?(method_name)
  self.class.matcher_list.respond_to?(method_name)
end
understands_reply?() click to toggle source
# File lib/responder_bot/handler.rb, line 24
def understands_reply?
  matcher_list.matches?(body)
end