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.

Since:

  • 4.0.0

Defined Under Namespace

Classes: Route

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

Includes common handler methods in any class that includes Lita::Handler::ChatRouter.

Since:

  • 4.0.0


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.

Parameters:

Returns:

  • (Boolean)

    Whether or not the message matched any routes.

Since:

  • 4.0.0


69
70
71
72
73
74
75
76
# File 'lib/lita/handler/chat_router.rb', line 69

def dispatch(robot, message)
  routes.map do |route|
    next unless route_applies?(route, message, robot)
    log_dispatch(route)
    dispatch_to_route(route, robot, message)
    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.

Parameters:

Since:

  • 3.3.0


84
85
86
87
88
89
90
91
92
# File 'lib/lita/handler/chat_router.rb', line 84

def dispatch_to_route(route, robot, message)
  response = Response.new(message, route.pattern)
  robot.hooks[:trigger_route].each { |hook| hook.call(response: response, route: route) }
  handler = new(robot)
  route.callback.call(handler, response)
rescue Exception => e
  log_dispatch_error(e)
  raise e if Lita.test_mode?
end

#route(pattern, method_name, **options) ⇒ void #route(pattern, **options) { ... } ⇒ void

Overloads:

  • #route(pattern, method_name, **options) ⇒ void

    This method returns an undefined value.

    Creates a chat route.

    Parameters:

    • pattern (Regexp)

      A regular expression to match incoming messages against.

    • method_name (Symbol, String)

      The name of the instance method to trigger.

    • command (Boolean)

      Whether or not the message must be directed at the robot.

    • restrict_to (Array<Symbol, String>, nil)

      An optional list of authorization groups the user must be in to trigger the route.

    • help (Hash)

      An optional map of example invocations to descriptions.

    • options (Hash)

      Aribtrary additional data that can be used by Lita extensions.

  • #route(pattern, **options) { ... } ⇒ void

    This method returns an undefined value.

    Creates a chat route.

    Parameters:

    • pattern (Regexp)

      A regular expression to match incoming messages against.

    • command (Boolean)

      Whether or not the message must be directed at the robot.

    • restrict_to (Array<Symbol, String>, nil)

      An optional list of authorization groups the user must be in to trigger the route.

    • help (Hash)

      An optional map of example invocations to descriptions.

    • options (Hash)

      Aribtrary additional data that can be used by Lita extensions.

    Yields:

    • The body of the route's callback.

    Since:

    • 4.0.0

Since:

  • 4.0.0


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lita/handler/chat_router.rb', line 44

def route(pattern, method_name = nil, **options, &block)
  options = default_route_options.merge(options)
  options[:restrict_to] = options[:restrict_to].nil? ? nil : Array(options[:restrict_to])
  routes << Route.new(
    pattern,
    Callback.new(method_name || block),
    options.delete(:command),
    options.delete(:restrict_to),
    options.delete(:help),
    options
  )
end

#routesArray<Lita::Handler::Route>

A list of chat routes defined by the handler.

Returns:

Since:

  • 4.0.0


59
60
61
# File 'lib/lita/handler/chat_router.rb', line 59

def routes
  @routes ||= []
end