Class: BotMob::Roster
- Inherits:
-
Object
- Object
- BotMob::Roster
- Defined in:
- lib/bot_mob/roster.rb
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #available? ⇒ Boolean
- #connect(auth) ⇒ Object
-
#initialize ⇒ Roster
constructor
A new instance of Roster.
- #load ⇒ Object
- #register(bot_class) ⇒ Object
Constructor Details
#initialize ⇒ Roster
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
#registry ⇒ Object (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
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 |
#load ⇒ Object
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 |