Class: Del::DefaultRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/del/default_router.rb

Overview

This class is the default router used to route chat messages to chat routes.

Instance Method Summary collapse

Constructor Details

#initialize(routes = []) ⇒ DefaultRouter

Returns a new instance of DefaultRouter.



7
8
9
# File 'lib/del/default_router.rb', line 7

def initialize(routes = [])
  @routes = routes
end

Instance Method Details

#register(pattern, &block) ⇒ Object



11
12
13
# File 'lib/del/default_router.rb', line 11

def register(pattern, &block)
  @routes.push(pattern: pattern, command: block)
end

#route(message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/del/default_router.rb', line 15

def route(message)
  @routes.each do |route|
    next unless (matches = route[:pattern].match(message.text))

    begin
      route[:command].call(message, matches)
    rescue StandardError => error
      Del.logger.error(error)
    end
  end
end