bot_mob 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5120b6a0a96763c9153d61f8f008b8a62972468
4
- data.tar.gz: 8a918da1057d9d4831725d7d024df39e20f01cbf
3
+ metadata.gz: dd3908ac2430717821fdeaea597de6a9e9352825
4
+ data.tar.gz: 015641eeafc749d5add8796838553f75a129f65c
5
5
  SHA512:
6
- metadata.gz: 6c6ed0090f5b97e2fac901c4682f085f2009a854e071b28985cade0db02f8c84e0f585981cddf6e7ac478d2753062394461b63fdbcab716ddb3431f9702aae1e
7
- data.tar.gz: a5a0392283862e3f4fc4f2b31dbe9f919bd4e883cece9119e975ffeacde8127a8b45edcd1d6d60d2c3fa7defd6b085514ae0ff7a75853f78b348ff15061d46c4
6
+ metadata.gz: db17341fa132c62760e7866dd9d558b3a8f190f41eb0b5d6b3ccdf0608ef0d154bf1df0b51bd0867d521c4da71ddd80a81a54959358bc2493d713ba6f90982c2
7
+ data.tar.gz: 4503c2784258e2f5cac51016b775fad1019a30c87d36cf34e6ac45ec86d25d01f771a898b43fb3bfa0b18cf744ce19e66e1e49ece5ffb87ffccfa2cef55dd09b
@@ -1,50 +1,33 @@
1
+ require 'forwardable'
2
+
1
3
  module BotMob
2
4
  # ## BotMob::Application
3
5
  #
4
6
  # This is the base class for BotMob.
5
7
  class Application
6
- attr_reader :bot_class, :bots
8
+ extend Forwardable
7
9
 
8
- def initialize(bot_class)
9
- @bot_class = bot_class
10
- @bots = {}
11
- end
10
+ def_delegators :roster, :register
12
11
 
13
12
  def start!
14
13
  BotMob.logger.info("=> BotMob #{BotMob::VERSION} application starting")
15
- load_registry
16
- wire.connect { |auth| connect(auth) }
14
+
15
+ roster.load
16
+ wire.listen do |bot|
17
+ roster.connect(bot)
18
+ end
17
19
 
18
20
  loop do
19
21
  sleep(10)
20
22
  # Avoid polling at all for the time being
21
- # load_registry if registry_available? && !wire.available?
23
+ # roster.load if roster.available? && !wire.available?
22
24
  end
23
25
  end
24
26
 
25
- def connect(auth)
26
- external_id, token = auth['external_id'], auth['token']
27
- BotMob.logger.info "Register bot: #{external_id}"
28
- bot = bot_class.new(external_id, token)
29
- @bots[external_id] ||= bot
30
- @bots[external_id].refresh
31
- end
32
-
33
- def registry_available?
34
- @registry_available.nil? || @registry_available
35
- end
36
-
37
27
  private
38
28
 
39
- def load_registry
40
- begin
41
- BotMob::Install.all.each { |install| connect(install.auth) }
42
- BotMob.logger.info '=> Registry loaded'
43
- @registry_available = true
44
- rescue ActiveRecord::ConnectionNotEstablished => e
45
- @registry_available = false
46
- BotMob.logger.info "=> Database unavailable, skipping registry"
47
- end
29
+ def roster
30
+ @roster ||= BotMob::Roster.new
48
31
  end
49
32
 
50
33
  def wire
@@ -4,17 +4,16 @@ module BotMob
4
4
  # The Authority class will process an access code
5
5
  # and retrieve an access token from Slack
6
6
  class Authority
7
- attr_accessor :network, :ambassador
7
+ attr_reader :network, :ambassador
8
8
 
9
9
  def initialize(network, code)
10
10
  @ambassador = BotMob::Ambassador.setup(network, code)
11
11
  end
12
12
 
13
- def process
13
+ def process(bot_class)
14
14
  return unless ambassador.success?
15
15
 
16
- install = BotMob::Install.create_with_auth(ambassador.auth)
17
- wire.publish(external_id: install.external_id, token: install.token)
16
+ wire.publish(ambassador.auth.merge(bot_class: bot_class.to_s))
18
17
  end
19
18
 
20
19
  private
data/lib/bot_mob/bot.rb CHANGED
@@ -26,7 +26,7 @@ module BotMob
26
26
  client.message(args)
27
27
  end
28
28
 
29
- def handle(message)
29
+ def respond(message)
30
30
  # noop
31
31
  end
32
32
 
@@ -77,7 +77,7 @@ module BotMob
77
77
  logger.info "Successfully connected, '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
78
78
  end
79
79
 
80
- logger.info "State[#{bot.external_id}] - #{state}"
80
+ logger.info "State[#{bot.class.to_s}##{bot.external_id}] - #{state}"
81
81
  @state = state
82
82
  end
83
83
 
@@ -93,7 +93,7 @@ module BotMob
93
93
  if bot.respond?(message)
94
94
  logger.info("Received message at #{Time.now}")
95
95
  logger.info(" Parameters: #{message.data}")
96
- bot.handle(message)
96
+ bot.respond(message)
97
97
  end
98
98
  end
99
99
  end
@@ -2,9 +2,13 @@ module BotMob
2
2
  class Install < ActiveRecord::Base
3
3
  self.table_name = "botmob_installs"
4
4
 
5
+ def self.registered_bot(auth)
6
+ where(bot_class: auth['bot_class'], external_id: auth['external_id'])
7
+ end
8
+
5
9
  def self.create_with_auth(auth)
6
- where(external_id: auth[:external_id]).first_or_initialize.tap do |i|
7
- i.token = auth[:token]
10
+ registered_bot(auth).first_or_initialize.tap do |i|
11
+ i.token = auth['token']
8
12
  i.save!
9
13
  end
10
14
  end
@@ -0,0 +1,55 @@
1
+ module BotMob
2
+ class Roster
3
+ attr_reader :registry
4
+
5
+ def initialize
6
+ @registry = {}
7
+ @bots = {}
8
+ end
9
+
10
+ def connect(auth)
11
+ class_name = auth['bot_class'].to_s
12
+ bot_class = registry[class_name]
13
+ external_id, token = auth['external_id'], auth['token']
14
+ BotMob.logger.info "Initializing #{bot_class}: #{external_id}"
15
+
16
+ puts "Registry: #{registry.inspect}"
17
+ puts "Using registered bot: #{bot_class.inspect}"
18
+ unless auth.is_a?(BotMob::Install)
19
+ BotMob.logger.info "Updating #{bot_class} bot record..."
20
+ BotMob::Install.create_with_auth(auth)
21
+ end
22
+
23
+ puts "Setting up a new bot with:\n #{auth.inspect}\n #{external_id}\n #{token}"
24
+ bot = bot_class.new(external_id, token)
25
+ @bots["#{bot_class}##{external_id}"] ||= bot
26
+ @bots["#{bot_class}##{external_id}"].refresh
27
+ end
28
+
29
+ def load
30
+ begin
31
+ BotMob::Install.all.each do |install|
32
+ connect(install)
33
+ end
34
+
35
+ BotMob.logger.info '=> Roster loaded'
36
+ @available = true
37
+ rescue ActiveRecord::ConnectionNotEstablished => e
38
+ @available = false
39
+ BotMob.logger.info "=> Database unavailable, skipping roster load"
40
+ end
41
+ end
42
+
43
+ def [](key)
44
+ registry[key.to_s]
45
+ end
46
+
47
+ def register(bot_class)
48
+ @registry[bot_class.to_s] = bot_class
49
+ end
50
+
51
+ def available?
52
+ @available.nil? || @available
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module BotMob
2
- VERSION = '0.1.14'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/bot_mob/wire.rb CHANGED
@@ -10,7 +10,7 @@ module BotMob
10
10
  queue.publish(message.to_json, routing_key: queue.name)
11
11
  end
12
12
 
13
- def connect
13
+ def listen
14
14
  return if !block_given? || queue.nil?
15
15
 
16
16
  queue.subscribe do |_info, _props, body|
data/lib/bot_mob.rb CHANGED
@@ -15,6 +15,7 @@ module BotMob
15
15
  autoload :Connection, 'bot_mob/connection'
16
16
  autoload :InboundMessage, 'bot_mob/inbound_message'
17
17
  autoload :Install, 'bot_mob/install'
18
+ autoload :Roster, 'bot_mob/roster'
18
19
  autoload :Wire, 'bot_mob/wire'
19
20
 
20
21
  module Slack
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bot_mob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Werner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-16 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -231,6 +231,7 @@ files:
231
231
  - lib/bot_mob/connection.rb
232
232
  - lib/bot_mob/inbound_message.rb
233
233
  - lib/bot_mob/install.rb
234
+ - lib/bot_mob/roster.rb
234
235
  - lib/bot_mob/slack/ambassador.rb
235
236
  - lib/bot_mob/version.rb
236
237
  - lib/bot_mob/wire.rb