Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Add support for passing a Proc to routes
Browse files Browse the repository at this point in the history
Add support for passing a Proc to routes instead of a regular
expression for matching - this proc will then be called with the message
in the context of the handler object and should return a boolean
indicating whether the route matches or not.

This allows for dynamically defined routing, plus support for
configuring things like command names and prefixes, which is useful as
plugin configuration is only available on a handler instance, not the
singleton.

Probably addresses most of what #100 is asking for
  • Loading branch information
glittershark committed Feb 1, 2016
1 parent c4ee169 commit c4e0be7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/lita/handler/chat_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class Route

# @overload route(pattern, method_name, **options)
# Creates a chat route.
# @param pattern [Regexp] A regular expression to match incoming messages against.
# @param pattern [Regexp, Proc] Either a regular expression to match incoming messages against, or a
# Proc that will be called in the context of the handler with the message, and should return a
# boolean indicating whether that message matches the route
# @param method_name [Symbol, String] The name of the instance method to trigger.
# @param command [Boolean] Whether or not the message must be directed at the robot.
# @param restrict_to [Array<Symbol, String>, nil] An optional list of authorization
Expand All @@ -41,7 +43,9 @@ class Route
# @return [void]
# @overload route(pattern, **options)
# Creates a chat route.
# @param pattern [Regexp] A regular expression to match incoming messages against.
# @param pattern [Regexp, Proc] Either a regular expression to match incoming messages against, or a
# Proc that will be called in the context of the handler with the message, and should return a
# boolean indicating whether that message matches the route
# @param command [Boolean] Whether or not the message must be directed at the robot.
# @param restrict_to [Array<Symbol, String>, nil] An optional list of authorization
# groups the user must be in to trigger the route.
Expand Down
6 changes: 5 additions & 1 deletion lib/lita/route_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def from_self?(message, robot)

# Message must match the pattern
def matches_pattern?(route, message)
route.pattern === message.body
if route.pattern.is_a?(Regexp)
route.pattern === message.body
else
handler.instance_exec(message, &route.pattern)
end
end

# Allow custom route hooks to reject the route
Expand Down
14 changes: 14 additions & 0 deletions spec/lita/handler/chat_router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.name
route(/command/, :command, command: true)
route(/admin/, :admin, restrict_to: :admins)
route(/error/, :error)
route(:proc_route?, :trigger_proc_route)
route(/validate route hook/, :validate_route_hook, code_word: true)
route(/trigger route hook/, :trigger_route_hook, custom_data: "trigger route hook")

Expand All @@ -30,6 +31,14 @@ def error(_response)
raise
end

def proc_route?(message)
message == "proc route"
end

def trigger_proc_route
response.reply("trigger proc route")
end

def validate_route_hook(response)
response.reply("validate route hook")
end
Expand All @@ -50,6 +59,11 @@ def trigger_route_hook(response)
expect(replies.last).to eq("message")
end

it "routes messages satisfying a proc to the supplied method" do
send_message("proc route")
expect(replies.last).to eq("proc route")
end

it "routes a matching message even if addressed to the robot" do
send_command("message")
expect(replies.last).to eq("message")
Expand Down

0 comments on commit c4e0be7

Please sign in to comment.