Module: Lita::Handler::ChatRouter
- Included in:
- Lita::Handler, Lita::Handlers::Users
- Defined in:
- lib/lita/handler/chat_router.rb
Overview
A handler mixin that provides the methods necessary for responding to chat messages.
Defined Under Namespace
Classes: Route
Class Method Summary collapse
-
.extended(klass) ⇒ Object
Includes common handler methods in any class that includes ChatRouter.
Instance Method Summary collapse
-
#dispatch(robot, message) ⇒ Boolean
The main entry point for the handler at runtime.
-
#dispatch_to_route(route, robot, message) ⇒ void
Dispatch directly to a Route, ignoring route conditions.
- #route(pattern, method_name = nil, **options, &block) ⇒ Object
-
#routes ⇒ Array<Lita::Handler::Route>
A list of chat routes defined by the handler.
Class Method Details
.extended(klass) ⇒ Object
Includes common handler methods in any class that includes Lita::Handler::ChatRouter.
7 8 9 |
# File 'lib/lita/handler/chat_router.rb', line 7 def self.extended(klass) klass.send(:include, Common) end |
Instance Method Details
#dispatch(robot, message) ⇒ Boolean
The main entry point for the handler at runtime. Checks if the message matches any of the routes and invokes the route’s method if it does. Called by Robot#receive.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lita/handler/chat_router.rb', line 71 def dispatch(robot, ) routes.map do |route| next unless route_applies?(route, , robot) log_dispatch(route) robot.trigger( :message_dispatched, handler: self, route: route, message: , robot: robot ) dispatch_to_route(route, robot, ) true end.any? end |
#dispatch_to_route(route, robot, message) ⇒ void
This method returns an undefined value.
Dispatch directly to a Route, ignoring route conditions.
93 94 95 96 97 98 99 100 |
# File 'lib/lita/handler/chat_router.rb', line 93 def dispatch_to_route(route, robot, ) response = Response.new(, route.pattern) robot.hooks[:trigger_route].each { |hook| hook.call(response: response, route: route) } handler = new(robot) route.callback.call(handler, response) rescue => error log_error(robot, error) end |
#route(pattern, method_name, **options) ⇒ void #route(pattern, **options) { ... } ⇒ void
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lita/handler/chat_router.rb', line 46 def route(pattern, method_name = nil, **, &block) = .merge() [:restrict_to] = [:restrict_to].nil? ? nil : Array([:restrict_to]) routes << Route.new( pattern, Callback.new(method_name || block), .delete(:command), .delete(:restrict_to), .delete(:help), ) end |
#routes ⇒ Array<Lita::Handler::Route>
A list of chat routes defined by the handler.
61 62 63 |
# File 'lib/lita/handler/chat_router.rb', line 61 def routes @routes ||= [] end |