Module: TelegramBot::EventHandler

Included in:
TelegramBot
Defined in:
lib/telegram_bot/handler.rb

Defined Under Namespace

Modules: PrependMethods Classes: Handler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(clazz) ⇒ Object



40
41
42
# File 'lib/telegram_bot/handler.rb', line 40

def self.included(clazz)
  clazz.prepend PrependMethods
end

Instance Method Details

#handle(msg) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/telegram_bot/handler.rb', line 53

def handle(msg)
  env = BlankSlate.new
  self.extend_env(env)
  msg.extend_env(env)

  @handlers.each do |hndlr|
    next unless hndlr === msg

    hndlr.matcher.extend_env(env, msg)

    env.extend do
      define_method :handler do
        hndlr
      end
    end

    env.call(*hndlr.arguments(msg),
             &hndlr.action)
  end
end

#on(type, *args, pass: false, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/telegram_bot/handler.rb', line 45

def on(type, *args, pass: false, &block)
  matcher_class_name = "telegram_bot/#{type}_matcher".classify
  matcher_class = matcher_class_name.constantize
  matcher = matcher_class.new(*args)
  handler = Handler.new(matcher, block, pass)
  @handlers << handler
end