bot_mob 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83ed3ccf1a952e7cf93cdd80b6494ef87cac8c2b
4
- data.tar.gz: 87ebbd02f253d73b6f7f4deb50d16eea869f3405
3
+ metadata.gz: d0769d0dc57a2e5348d3cd9ee7490d05e09c01c9
4
+ data.tar.gz: ddd3ffc84ed4fc254a024a9773bc1dadde5d9924
5
5
  SHA512:
6
- metadata.gz: 2252550d04e0dab068f2b6c3af6ea1bb240aafc4929ef53e6cfd250ded4c5775ae7e55f43794bd1a861fcceefe679de2cea9d3fd92e5e2279d51fa6624ef7946
7
- data.tar.gz: ecc324ba9be6ebc2a68ba86d3e1e8e16b580161b7216fd0b2e27449f5a5a2acf60017b95373c770c22f6bc25db2786a34a665bc4a6165eae3b9926a34a3a1c37
6
+ metadata.gz: 58c88902f282c873274b4865b3ed03d2161d315ba44e177aaa0297221c7b39c3672294b5205cf15bd2ad352ffc4ac62d1595f00e98ce92642e065de56e2fee6c
7
+ data.tar.gz: 0a0e3d6796e0081cd58b1795f667c708a157ca8cc439a754df78efed94038c6cb046c41b36f186e25b612aceccc66919710b2e478e8bb1359e4dc9865ef7d087
@@ -0,0 +1,16 @@
1
+ module BotMob
2
+ class Ambassador
3
+ NETWORKS = [:slack]
4
+
5
+ def self.setup(network, code)
6
+ raise BotMob::InvalidNetworkError unless NETWORKS.include?(network.to_s.to_sym)
7
+ send("setup_#{network}", code)
8
+ end
9
+
10
+ private
11
+
12
+ def self.setup_slack(code)
13
+ BotMob::Ambassador::Slack.new(code)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ module BotMob
2
+ module Ambassadors
3
+ class Slack
4
+ def initialize(code)
5
+ @code = code
6
+ end
7
+
8
+ def token
9
+ oauth_response && oauth_response.bot_access_token
10
+ end
11
+
12
+ def external_id
13
+ oauth_response && oauth_response.bot_user_id
14
+ end
15
+
16
+ def success?
17
+ external_id && token
18
+ end
19
+
20
+ private
21
+
22
+ def oauth_response
23
+ return @oauth_response if defined?(@oauth_response)
24
+
25
+ response = client.oauth_access({
26
+ code: @code,
27
+ client_id: ENV['CLIENT_ID'],
28
+ client_secret: ENV['CLIENT_SECRET']
29
+ })
30
+
31
+ @oauth_response = response.bot
32
+ rescue Slack::Web::Api::Error => e
33
+ puts e.message
34
+ nil
35
+ end
36
+
37
+ def client
38
+ @client ||= Slack::Web::Client.new
39
+ end
40
+ end
41
+ end
42
+ end
@@ -3,12 +3,12 @@ module BotMob
3
3
  #
4
4
  # This is the base class for BotMob.
5
5
  class Application
6
- attr_reader :bot_class, :bots, :wire
6
+ attr_reader :bot_class, :bots
7
7
 
8
8
  def initialize(bot_class)
9
9
  @bot_class = bot_class
10
10
  @bots = {}
11
- @wire = BotMob::Wire.new
11
+
12
12
  Install.all.each { |install| register(install) }
13
13
  end
14
14
 
@@ -34,5 +34,11 @@ module BotMob
34
34
  @bots[install.user_id] ||= bot
35
35
  @bots[install.user_id].refresh
36
36
  end
37
+
38
+ private
39
+
40
+ def wire
41
+ @wire ||= BotMob::Wire.new
42
+ end
37
43
  end
38
44
  end
@@ -4,38 +4,24 @@ 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 :code
7
+ attr_accessor :network, :ambassador
8
8
 
9
- def initialize(code)
10
- @code = code
9
+ def initialize(network, code)
10
+ @ambassador = BotMob::Ambassador.setup(network, code)
11
11
  end
12
12
 
13
13
  def process
14
- return nil unless code
15
- response = request_access.bot
14
+ return unless ambassador.success?
16
15
 
17
- Install.where(user_id: response.bot_user_id).first_or_initialize.tap do |install|
18
- install.token = response.bot_access_token
19
- install.save!
20
- BotMob::Wire.new.publish(user_id: install.user_id)
21
- end
22
- rescue Slack::Web::Api::Error => e
23
- puts e.message
24
- nil
16
+ install = Install.create_with_ambassador(ambassador)
17
+ # check for install's success
18
+ wire.publish(external_id: install.external_id)
25
19
  end
26
20
 
27
21
  private
28
22
 
29
- def request_access
30
- client.oauth_access(auth_params)
31
- end
32
-
33
- def auth_params
34
- { client_id: ENV['CLIENT_ID'], client_secret: ENV['CLIENT_SECRET'], code: code }
35
- end
36
-
37
- def client
38
- @client ||= Slack::Web::Client.new
23
+ def wire
24
+ @wire ||= BotMob::Wire.new
39
25
  end
40
26
  end
41
27
  end
data/lib/bot_mob/bot.rb CHANGED
@@ -4,7 +4,7 @@ module BotMob
4
4
  # This is the base for all bots that will connect
5
5
  # to Slack.
6
6
  class Bot
7
- attr_reader :user_id, :state, :message
7
+ attr_reader :user_id, :state
8
8
 
9
9
  def initialize(user_id, token)
10
10
  @user_id = user_id
@@ -21,17 +21,16 @@ module BotMob
21
21
  client.start_async
22
22
  rescue Slack::Web::Api::Error => e
23
23
  logger.info e.message
24
- logger.info "State[#{user_id}] - disabled"
25
- @state = :inactive
24
+ set_state :inactive
26
25
  end
27
26
 
28
27
  def connect
29
- logger.info "State[#{user_id}] - connecting"
28
+ set_state :connecting
30
29
  establish_connection
31
30
  end
32
31
 
33
32
  def reconnect
34
- logger.info "State[#{user_id}] - reconnecting"
33
+ set_state :reconnecting
35
34
  @client = Slack::RealTime::Client.new(token: @token)
36
35
  setup
37
36
  establish_connection
@@ -93,11 +92,9 @@ module BotMob
93
92
  def setup
94
93
  @state = :waiting
95
94
 
96
- client.on :hello do
97
- logger.info "Successfully connected, '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
98
- @state = :connected
99
- logger.info "State[#{user_id}] - #{state}"
100
- end
95
+ client.on :hello { set_state(:connected) }
96
+ client.on :close { set_state(:disconnecting) }
97
+ client.on :closed { set_state(:closed) }
101
98
 
102
99
  client.on :message do |data|
103
100
  message = BotMob::InboundMessage.new(:websocket, data)
@@ -108,15 +105,15 @@ module BotMob
108
105
  handle(message)
109
106
  end
110
107
  end
108
+ end
111
109
 
112
- client.on :close do |_data|
113
- logger.info "State[#{user_id}] - about to disconnect"
110
+ def set_state(state)
111
+ if state == :connected
112
+ logger.info "Successfully connected, '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
114
113
  end
115
114
 
116
- client.on :closed do |_data|
117
- logger.info "State[#{user_id}] - disconnected"
118
- @state = :closed
119
- end
115
+ logger.info "State[#{user_id}] - #{state}"
116
+ @state = state
120
117
  end
121
118
 
122
119
  def client
@@ -1,3 +1,3 @@
1
1
  module BotMob
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/lib/bot_mob.rb CHANGED
@@ -9,12 +9,17 @@ require 'bot_mob/version'
9
9
  # BotMob provides all the tools you need to easily
10
10
  # connect to chat networks
11
11
  module BotMob
12
+ autoload :Ambassador, 'bot_mob/ambassador'
12
13
  autoload :Application, 'bot_mob/application'
13
14
  autoload :Authority, 'bot_mob/authority'
14
15
  autoload :Bot, 'bot_mob/bot'
15
16
  autoload :InboundMessage, 'bot_mob/inbound_message'
16
17
  autoload :Wire, 'bot_mob/wire'
17
18
 
19
+ module Ambassadors
20
+ autoload :Slack, 'bot_mob/ambassadors/slack'
21
+ end
22
+
18
23
  def self.root
19
24
  File.expand_path('../..', __FILE__)
20
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bot_mob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Werner
@@ -181,6 +181,8 @@ files:
181
181
  - bin/setup
182
182
  - bot_mob.gemspec
183
183
  - lib/bot_mob.rb
184
+ - lib/bot_mob/ambassador.rb
185
+ - lib/bot_mob/ambassadors/slack.rb
184
186
  - lib/bot_mob/application.rb
185
187
  - lib/bot_mob/authority.rb
186
188
  - lib/bot_mob/bot.rb
@@ -206,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
208
  version: '0'
207
209
  requirements: []
208
210
  rubyforge_project:
209
- rubygems_version: 2.4.6
211
+ rubygems_version: 2.5.1
210
212
  signing_key:
211
213
  specification_version: 4
212
214
  summary: Build your bots