class Lita::RouteValidator
Determines if an incoming message should trigger a route. @api private
Attributes
The handler class the route belongs to.
The incoming message.
The currently running robot.
The route being checked.
Public Class Methods
@param handler [Lita::Handler] The handler the route belongs to. @param route [Lita::Handler::ChatRouter::Route] The route being validated. @param message [Lita::Message] The incoming message. @param robot [Lita::Robot] The currently running robot.
# File lib/lita/route_validator.rb, line 21 def initialize(handler, route, message, robot) @handler = handler @route = route @message = message @robot = robot end
Public Instance Methods
Returns a boolean indicating whether or not the route should be triggered. @return [Boolean] Whether or not the route should be triggered.
# File lib/lita/route_validator.rb, line 30 def call return unless command_satisfied?(route, message) return if from_self?(message, robot) return unless matches_pattern?(route, message) unless authorized?(robot, message.user, route.required_groups) robot.trigger( :route_authorization_failed, message: message, robot: robot, route: route, ) return end return unless passes_route_hooks?(route, message, robot) true end
Private Instance Methods
Message
must be a command if the route requires a command
# File lib/lita/route_validator.rb, line 51 def command_satisfied?(route, message) !route.command? || message.command? end
Messages from self should be ignored to prevent infinite loops
# File lib/lita/route_validator.rb, line 56 def from_self?(message, robot) message.user.name == robot.name end
Message
must match the pattern
# File lib/lita/route_validator.rb, line 61 def matches_pattern?(route, message) route.pattern === message.body end
Allow custom route hooks to reject the route
# File lib/lita/route_validator.rb, line 66 def passes_route_hooks?(route, message, robot) robot.hooks[:validate_route].all? do |hook| hook.call(handler: handler, route: route, message: message, robot: robot) end end