Class: Bot::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/bot/handler.rb

Direct Known Subclasses

ApplicationHandler

Constant Summary collapse

@@responder_chain =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle(messages) ⇒ Object



9
10
11
# File 'lib/bot/handler.rb', line 9

def self.handle(messages)
  new.handle(messages)
end

.responder_chainObject



36
37
38
# File 'lib/bot/handler.rb', line 36

def self.responder_chain
  @@responder_chain ||= Bot::ResponderChain.new
end

.use(responder) ⇒ Object



5
6
7
# File 'lib/bot/handler.rb', line 5

def self.use(responder)
  responder_chain.add(responder)
end

Instance Method Details

#execute_chain(message, responses, user) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bot/handler.rb', line 17

def execute_chain(message, responses, user)
  analytics = Bot::Analytics.new(user.username, Bot.configuration.mixpanel)
  user_context = Bot::Context.new(user.id, Bot.configuration.redis)
  chat_context = Bot::Context.new(message["chatId"].to_i, Bot.configuration.redis)

  responder_chain.responders.each do |responder|
    responder = responder.new(message, user, responses, self, analytics, user_context, chat_context)
    if responder.can_respond_to_type?(message['type']) && responder.can_handle?
      responses << responder.handle
      break
    end
  end
  responses
end

#handle(messages) ⇒ Object



13
14
15
# File 'lib/bot/handler.rb', line 13

def handle(messages)
  messages.flat_map { |m| handle_message(m) }
end

#responder_chainObject



32
33
34
# File 'lib/bot/handler.rb', line 32

def responder_chain
  @@responder_chain ||= Bot::ResponderChain.new
end