Class: SardonyxRing::Handlers::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sardonyx_ring/handlers/message_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, callback) ⇒ MessageHandler

Returns a new instance of MessageHandler.



6
7
8
9
# File 'lib/sardonyx_ring/handlers/message_handler.rb', line 6

def initialize(pattern, callback)
  @pattern = pattern
  @callback = callback
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



11
12
13
# File 'lib/sardonyx_ring/handlers/message_handler.rb', line 11

def pattern
  @pattern
end

Instance Method Details

#match(message) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/sardonyx_ring/handlers/message_handler.rb', line 18

def match(message)
  case @pattern
  when String
    @pattern if message.text == @pattern
  when Regexp
    message.text&.match(@pattern)
  end
end

#run(app, message_event, match = nil) ⇒ Object



13
14
15
16
# File 'lib/sardonyx_ring/handlers/message_handler.rb', line 13

def run(app, message_event, match = nil)
  args = [message_event, match].slice(0, @callback.arity)
  @callback.bind(app).call(*args)
end