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



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

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
# File 'lib/bot/handler.rb', line 17

def execute_chain(message, responses, user)
  context = Bot::Context.new(user, Bot.configuration.redis)
  analytics = Bot::Analytics.new(user.username, Bot.configuration.mixpanel)

  responder_chain.responders.each do |responder|
    responder = responder.new(message, user, responses, self, context, analytics)
    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



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

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