class Lita::RouteValidator

Determines if an incoming message should trigger a route. @api private

Attributes

handler[R]

The handler class the route belongs to.

message[R]

The incoming message.

robot[R]

The currently running robot.

route[R]

The route being checked.

Public Class Methods

new(handler, route, message, robot) click to toggle source

@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

call() click to toggle source

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

authorized?(robot, user, required_groups) click to toggle source

User must be in auth group if route is restricted.

# File lib/lita/route_validator.rb, line 73
def authorized?(robot, user, required_groups)
  required_groups.nil? || required_groups.any? do |group|
    if Lita.version_3_compatibility_mode?
      Lita::Authorization.user_in_group?(user, group)
    else
      robot.auth.user_in_group?(user, group)
    end
  end
end
command_satisfied?(route, message) click to toggle source

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
from_self?(message, robot) click to toggle source

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
matches_pattern?(route, message) click to toggle source

Message must match the pattern

# File lib/lita/route_validator.rb, line 61
def matches_pattern?(route, message)
  route.pattern === message.body
end
passes_route_hooks?(route, message, robot) click to toggle source

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