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



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, message)
  routes.map do |route|
    next unless route_applies?(route, message, robot)
    log_dispatch(route)
    robot.trigger(
      :message_dispatched,
      handler: self,
      route: route,
      message: message,
      robot: robot
    )
    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



93
94
95
96
97
98
99
100
# File 'lib/lita/handler/chat_router.rb', line 93

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 => error
  log_error(robot, error)
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



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, **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



61
62
63
# File 'lib/lita/handler/chat_router.rb', line 61

def routes
  @routes ||= []
end