lib/hipbot/reaction.rb in hipbot-0.0.3 vs lib/hipbot/reaction.rb in hipbot-0.0.5

- old
+ new

@@ -1,46 +1,54 @@ module Hipbot - class Reaction < Struct.new(:robot, :regexp, :options, :block) + class Reaction < Struct.new(:robot, :regexps, :options, :block) def invoke sender, room, message message = message_for(message, sender) arguments = arguments_for(message) Response.new(robot, self, room, message).invoke(arguments) end def match? sender, room, message message = message_for(message, sender) - matches?(message) && matches_scope?(message) && matches_sender?(message) + matches_regexp?(message) && matches_scope?(message) && matches_sender?(message) && matches_room?(room) end private + # TODO: this is pointless since we can get actual message object from xmpp4r def message_for message, sender Message.new(message, sender) end def arguments_for message - message.body.match(regexp)[1..-1] + message.body.match(matching_regexp(message))[1..-1] end - def matches?(message) - regexp =~ message.body + def matches_regexp?(message) + matching_regexp(message).present? end + def matches_room?(room) + !options[:room] || Array(options[:room]).include?(room.name) + end + def matches_scope?(message) global? || message.for?(robot) end def matches_sender?(message) from_all? || Array(options[:from]).include?(message.sender) end + def matching_regexp(message) + regexps.find { |regexp| regexp =~ message.body } + end + def global? options[:global] end def from_all? !options[:from] end - end end