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
-
#connect(auth) ⇒ Object
connect. -
#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
62 63 64 |
# File 'lib/bot_mob/roster.rb', line 62 def [](key) registry[key.to_s] end |
#connect(auth) ⇒ Object
connect
connect receives an auth hash that contains a bot_class along with any accompanying metadata that the bot will need to connect to the specified network. Provided by BotMob::Authority delivered over the wire.
Example auth param:
{
bot_class: 'FooBot',
network: 'slack',
external_id: 'USER1',
data: {
token: 'abc123'
}
}
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bot_mob/roster.rb', line 28 def connect(auth) # { network: :dev, external_id: 'foo', bot_class: 'FooBot', data: {} } class_name = auth['bot_class'].to_s bot_class = registry[class_name] raise BotMob::UnregisteredBotError, 'You must explicitly register your bot class with the roster before connecting that bot type' if bot_class.nil? BotMob.logger.info "Selecting #{class_name.inspect} from registry: #{bot_class.inspect}" unless auth.is_a?(BotMob::Install) begin BotMob.logger.info "Updating #{class_name} database record for #{auth['external_id']}..." BotMob::Install.create_with_auth(auth) rescue ActiveRecord::StatementInvalid => e BotMob.logger.info "Unable to save record: #{e.}" end end network = auth['network'] allocate(bot_class, network, auth) end |
#load ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bot_mob/roster.rb', line 48 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
66 67 68 |
# File 'lib/bot_mob/roster.rb', line 66 def register(bot_class) @registry[bot_class.to_s] = bot_class end |