Class: BotMob::Roster

Inherits:
Object
  • Object
show all
Defined in:
lib/bot_mob/roster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoster

Returns a new instance of Roster.



5
6
7
8
# File 'lib/bot_mob/roster.rb', line 5

def initialize
  @registry = {}
  @bots = {}
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



3
4
5
# File 'lib/bot_mob/roster.rb', line 3

def registry
  @registry
end

Instance Method Details

#[](key) ⇒ Object



43
44
45
# File 'lib/bot_mob/roster.rb', line 43

def [](key)
  registry[key.to_s]
end

#available?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/bot_mob/roster.rb', line 51

def available?
  @available.nil? || @available
end

#connect(auth) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bot_mob/roster.rb', line 10

def connect(auth)
  class_name = auth['bot_class'].to_s
  bot_class = registry[class_name]
  external_id, token = auth['external_id'], auth['token']
  BotMob.logger.info "Initializing #{bot_class}: #{external_id}"

  puts "Registry: #{registry.inspect}"
  puts "Using registered bot: #{bot_class.inspect}"
  unless auth.is_a?(BotMob::Install)
    BotMob.logger.info "Updating #{bot_class} bot record..."
    BotMob::Install.create_with_auth(auth)
  end

  puts "Setting up a new bot with:\n  #{auth.inspect}\n  #{external_id}\n  #{token}"
  bot = bot_class.new(external_id, token)
  @bots["#{bot_class}##{external_id}"] ||= bot
  @bots["#{bot_class}##{external_id}"].refresh
end

#loadObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bot_mob/roster.rb', line 29

def load
  begin
    BotMob::Install.all.each do |install|
      connect(install)
    end

    BotMob.logger.info '=> Roster loaded'
    @available = true
  rescue ActiveRecord::ConnectionNotEstablished => e
    @available = false
    BotMob.logger.info "=> Database unavailable, skipping roster load"
  end
end

#register(bot_class) ⇒ Object



47
48
49
# File 'lib/bot_mob/roster.rb', line 47

def register(bot_class)
  @registry[bot_class.to_s] = bot_class
end