Class: Lita::External::Robot

Inherits:
Robot
  • Object
show all
Defined in:
lib/lita/external/robot.rb

Defined Under Namespace

Classes: Ballot

Instance Method Summary collapse

Instance Method Details

#process_outbound_command(payload) ⇒ Object



55
56
57
58
59
# File 'lib/lita/external/robot.rb', line 55

def process_outbound_command(payload)
  command, args = Marshal.load(payload)
  Lita.logger.debug("Triggering #{command}")
  adapter.public_send(command, *args)
end

#receive(message) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/lita/external/robot.rb', line 12

def receive(message)
  ballot = Ballot.new
  trigger(:master_receive, ballot: ballot)
  if ballot.veto
    Lita.logger.debug("Ignoring vetoed message")
  else
    Lita.logger.debug("Put inbound message from #{message.user.mention_name} into the queue")
    Lita.redis.rpush('messages:inbound', External.dump_message(message))
  end
end

#runObject



23
24
25
26
27
28
29
30
# File 'lib/lita/external/robot.rb', line 23

def run
  @stopping = false

  trigger(:master_loaded)

  watch_outbound_queue
  super
end

#shut_downObject



32
33
34
35
# File 'lib/lita/external/robot.rb', line 32

def shut_down
  @stopping = true
  super
end

#watch_outbound_queueObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lita/external/robot.rb', line 37

def watch_outbound_queue
  Thread.start do
    Lita.logger.info("Watching outbound queue")
    until @stopping
      begin
        if command = External.blocking_redis.blpop('messages:outbound', timeout: 1)
          process_outbound_command(command.last)
        end
      rescue => error
        Lita.logger.error("Outbound message failed: #{error.class}: #{error.message}")
        if Lita.config.robot.error_handler
          Lita.config.robot.error_handler.call(error)
        end
      end
    end
  end
end