slack-bot-manager 0.1.0pre1 → 0.1.0pre2

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.gitmodules +0 -3
  4. data/.rubocop.yml +6 -0
  5. data/.rubocop_todo.yml +81 -0
  6. data/.travis.yml +26 -0
  7. data/CHANGELOG.md +1 -1
  8. data/Gemfile +6 -0
  9. data/README.md +62 -11
  10. data/Rakefile +17 -0
  11. data/examples/dm-bot/Gemfile +5 -0
  12. data/examples/dm-bot/Gemfile.lock +40 -0
  13. data/examples/dm-bot/README.md +23 -0
  14. data/examples/dm-bot/dm-bot.rb +98 -0
  15. data/examples/dm-bot/tokens.yml.sample +2 -0
  16. data/lib/slack-bot-manager/client/base.rb +50 -14
  17. data/lib/slack-bot-manager/client/commands.rb +5 -7
  18. data/lib/slack-bot-manager/config.rb +41 -13
  19. data/lib/slack-bot-manager/errors.rb +10 -13
  20. data/lib/slack-bot-manager/extend.rb +13 -2
  21. data/lib/slack-bot-manager/logger.rb +12 -8
  22. data/lib/slack-bot-manager/manager/base.rb +5 -3
  23. data/lib/slack-bot-manager/manager/connection.rb +88 -87
  24. data/lib/slack-bot-manager/manager/storage/dalli.rb +63 -0
  25. data/lib/slack-bot-manager/manager/storage/redis.rb +55 -0
  26. data/lib/slack-bot-manager/manager/storage.rb +6 -0
  27. data/lib/slack-bot-manager/manager/tokens.rb +26 -20
  28. data/lib/slack-bot-manager/version.rb +1 -1
  29. data/lib/slack-bot-manager.rb +2 -0
  30. data/slack-bot-manager.gemspec +10 -9
  31. data/spec/fixtures/slack-bot-manager/web/rtm_start.yml +59 -0
  32. data/spec/integration/client_spec.rb +62 -0
  33. data/spec/integration/manager_spec.rb +121 -0
  34. data/spec/slack-bot-manager/client/base_spec.rb +96 -0
  35. data/spec/slack-bot-manager/config_spec.rb +19 -0
  36. data/spec/slack-bot-manager/errors_spec.rb +5 -0
  37. data/spec/slack-bot-manager/extend_spec.rb +5 -0
  38. data/spec/slack-bot-manager/logger_spec.rb +5 -0
  39. data/spec/slack-bot-manager/version_spec.rb +7 -0
  40. data/spec/spec_helper.rb +10 -0
  41. data/spec/support/vcr.rb +8 -0
  42. metadata +153 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 211a6a7bd9fb7a2557d3c946bbf495ae562a186d
4
- data.tar.gz: e2fc191df9ff401007c4f8e8e35bd2cf18302a8a
3
+ metadata.gz: e99eff3580607b9be5f2a6693e549ec0f4e8b111
4
+ data.tar.gz: f70a660184a3dc53ebcf2f8c575854d29813a615
5
5
  SHA512:
6
- metadata.gz: 0349d9425e90b8b68ea9b890b2edcf255ef2896f1a7e5606c5d1fc9c8146111c0698b88ab7416ee132399b96dfbd02369e5f84bde88a181b1c69e932e08de48d
7
- data.tar.gz: 128445cb7a39b80058bddd14fa89e0abda4db055fddb345ce330643a0c970c1fefbad3ec9978ebc6288226275bf1ed27625bf46b526618b5d0e7c66fbc92429f
6
+ metadata.gz: 926df5bbfeee60eaf86a51baff44679ea524f27f9b85cde4ff6078fea5f986556cb3c9ea714db877c57cef434d4b32564e337ce380bd7996aef750d0c561083d
7
+ data.tar.gz: 7165e5e5dc98dbf1745cdeec70257252c41f8f478bb0403a43bbb6012de02708e07a1deda7370a2b592cd9ac2eb5892c382624171330075ccad4339d3d591f30
data/.gitignore CHANGED
@@ -4,3 +4,4 @@
4
4
  pkg
5
5
  Gemfile.lock
6
6
  tokens.yml
7
+ slack-bot-manager-*.gem
data/.gitmodules CHANGED
@@ -7,6 +7,3 @@
7
7
  [submodule "examples/botspotting"]
8
8
  path = examples/botspotting
9
9
  url = git@github.com:goosey/botspotting.git
10
- [submodule "examples/dm-bot"]
11
- path = examples/dm-bot
12
- url = git@github.com:gleuch/dm-bot.git
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+ - examples/**/*
5
+
6
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,81 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-02-07 01:40:37 -0500 using RuboCop version 0.36.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Lint/UselessAssignment:
11
+ Exclude:
12
+ - 'spec/integration/manager_spec.rb'
13
+
14
+ # Offense count: 4
15
+ Metrics/AbcSize:
16
+ Max: 90
17
+
18
+ # Offense count: 2
19
+ Metrics/CyclomaticComplexity:
20
+ Max: 13
21
+
22
+ # Offense count: 13
23
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
24
+ # URISchemes: http, https
25
+ Metrics/LineLength:
26
+ Max: 120
27
+
28
+ # Offense count: 6
29
+ # Configuration parameters: CountComments.
30
+ Metrics/MethodLength:
31
+ Max: 43
32
+
33
+ # Offense count: 1
34
+ # Configuration parameters: CountComments.
35
+ Metrics/ModuleLength:
36
+ Max: 113
37
+
38
+ # Offense count: 2
39
+ Metrics/PerceivedComplexity:
40
+ Max: 14
41
+
42
+ # Offense count: 16
43
+ Style/Documentation:
44
+ Exclude:
45
+ - 'spec/**/*'
46
+ - 'test/**/*'
47
+ - 'lib/slack-bot-manager/client/base.rb'
48
+ - 'lib/slack-bot-manager/client/commands.rb'
49
+ - 'lib/slack-bot-manager/config.rb'
50
+ - 'lib/slack-bot-manager/errors.rb'
51
+ - 'lib/slack-bot-manager/extend.rb'
52
+ - 'lib/slack-bot-manager/logger.rb'
53
+ - 'lib/slack-bot-manager/manager/base.rb'
54
+ - 'lib/slack-bot-manager/manager/connection.rb'
55
+ - 'lib/slack-bot-manager/manager/storage.rb'
56
+ - 'lib/slack-bot-manager/manager/storage/dalli.rb'
57
+ - 'lib/slack-bot-manager/manager/storage/redis.rb'
58
+ - 'lib/slack-bot-manager/manager/tokens.rb'
59
+
60
+ # Offense count: 1
61
+ # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
62
+ Style/FileName:
63
+ Exclude:
64
+ - 'lib/slack-bot-manager.rb'
65
+
66
+ # Offense count: 1
67
+ Style/ModuleFunction:
68
+ Exclude:
69
+ - 'lib/slack-bot-manager/config.rb'
70
+
71
+ # Offense count: 3
72
+ # Cop supports --auto-correct.
73
+ Style/RedundantSelf:
74
+ Exclude:
75
+ - 'lib/slack-bot-manager/config.rb'
76
+
77
+ # Offense count: 2
78
+ # Cop supports --auto-correct.
79
+ Style/RescueModifier:
80
+ Exclude:
81
+ - 'lib/slack-bot-manager/manager/connection.rb'
data/.travis.yml ADDED
@@ -0,0 +1,26 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.2
7
+ - 2.1
8
+ - 2.0
9
+ - rbx-2
10
+ - jruby-19mode
11
+ - ruby-head
12
+ - jruby-head
13
+
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: ruby-head
17
+ - rvm: jruby-head
18
+ - rvm: jruby-19mode
19
+ - rvm: rbx-2
20
+
21
+ git:
22
+ submodules: false
23
+
24
+ env:
25
+ #- CONCURRENCY=celluloid-io
26
+ - CONCURRENCY=faye-websocket
data/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  ### Version 0.1.0 (20 Jan 2016)
3
3
 
4
- __**NOT YET RELEASED**__
4
+ __**Pre-release at 0.1.0pre2**__
5
5
 
6
6
  This is the first version of Slack Bot Manager, and includes
7
7
 
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem ENV['CONCURRENCY'], require: false if ENV.key?('CONCURRENCY')
6
+ gem 'picky' unless RUBY_PLATFORM == 'java'
data/README.md CHANGED
@@ -1,23 +1,43 @@
1
1
  # Slack Bot Manager
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/slack-bot-manager.svg)](http://badge.fury.io/rb/slack-bot-manager)
4
+ [![Build Status](https://travis-ci.org/betaworks/slack-bot-manager.svg?branch=master)](https://travis-ci.org/betaworks/slack-bot-manager)
5
+ [![Code Climate](https://codeclimate.com/github/betaworks/slack-bot-manager/badges/gpa.svg)](https://codeclimate.com/github/betaworks/slack-bot-manager)
6
+
3
7
  Slack Bot Manager is a Ruby gem that allows for the management of multiple Slack RTM connections based on tokens. With only a few configuration changes, you can run a system for handling hundreds of simulatenous RTM connections for your Slack app.
4
8
 
9
+ _This is in pre-release and may change before release of version 0.1.0._
5
10
 
6
11
 
7
- ## Installation
12
+ __How to tell if you need this:__
8
13
 
9
- While this has yet to be compiled to a Ruby gem, it can be installed from this repository within your Gemfile:
14
+ * You are making a Slack app requiring Real-time Messaging
15
+ * You want to be able to handle multiple RTM connections
16
+ * You don't want to make your own RTM (websocket) connection manager
10
17
 
11
- `gem 'slack-bot-manager', github: 'betaworks/slack-bot-manager', branch: 'master'`
12
18
 
13
- __**This gem requires `redis` for tracking the status of tokens.**__
14
- You will need to have `redis` running for this gem to work.
19
+ ## Installation
15
20
 
21
+ `gem 'slack-bot-manager'`
22
+
23
+ This gem requires a key-value storage system for managing tokens and connection statuses. Currently, this gem supports `redis` and `dalli` (memcached).
24
+
25
+ ```
26
+ gem 'redis'
27
+ gem 'dalli'
28
+ ```
16
29
 
17
30
 
18
31
  ## Getting Started
19
32
 
20
- (TODO)
33
+ To get started, get a token (or few) and start your script.
34
+
35
+ ```
36
+ botmanager = SlackBotManager::Manager.new
37
+ botmanager.add_token('token1', 'token2', 'token3')
38
+ botmanager.start
39
+ botmanager.monitor
40
+ ```
21
41
 
22
42
 
23
43
 
@@ -49,7 +69,7 @@ methods | description
49
69
 
50
70
  ### Token Management Methods
51
71
 
52
- Tokens are managed within Redis. SlackBotManager will manage and monitor these redis keys for additions, updates, and remvoals. New connections must be added into the redis `teams_key`, like so:
72
+ Tokens are managed using key storage, currently only supporting Redis. SlackBotManager will manage and monitor these keys for additions, updates, and removals. New connections will be added into the key `teams_key`, like so:
53
73
 
54
74
  ```
55
75
  botmanager = SlackBotManager::Manager.new
@@ -76,7 +96,7 @@ The following instance variables are accessible by Client and the included Comma
76
96
  variable | description
77
97
  --------------|----------------------------------------------------------------------------------------
78
98
  `connection` | `Slack::RealTime::Client` connection
79
- `id` | Team's Slack ID (ex. `T123ABC`)
99
+ `id` | Team's Slack ID (ex. `T123ABC`) _(set after successful connection)_
80
100
  `token` | Team's Slack access token (ex. `xoxb-123abc456def`)
81
101
  `status` | Known connection status. (`connected`, `disconnected`, `rate_limited`, `token_revoked`)
82
102
 
@@ -114,7 +134,8 @@ setting | description
114
134
  `tokens_key` | Redis key name for where tokens' status are stored. _(default: tokens:statuses)_
115
135
  `teams_key` | Redis key name for where teams' tokens are stored. _(default: tokens:teams)_
116
136
  `check_interval` | Interval (in seconds) for checking connections and tokens status. _(default: 5)_
117
- `redis` | Define Redis connection. _(default: Redis.new)_
137
+ `storage_method` | Token storage method. _(default: nil)_
138
+ `storage_options` | Token storage method options. _(default: {})_
118
139
  `logger` | Define the logger to use. _(default: Rails.logger or ::Logger.new(STDOUT))_
119
140
  `log_level` | Explicity define the logger level. _(default: ::Logger::WARN)_
120
141
  `verbose` | When true, set `log_level` to ::Logger::DEBUG. _(default: false)_
@@ -123,18 +144,47 @@ You can define these configuration options as:
123
144
 
124
145
  ```
125
146
  SlackBotManager::Manager.configure do |config|
126
- config.redis = Redis.new(host: '0.0.0.0', port: 6379)
147
+ config.storage_method = SlackBotManager::Storage::Redis
148
+ config.storage_options = {host: '0.0.0.0', port: 6379}
127
149
  config.check_interval = 10 # in seconds
128
150
  end
129
151
  ```
130
152
 
153
+ You can additionally send an existing storage method as the `storage_option`, such as:
154
+
155
+ ```
156
+ SlackBotManager::Manager.configure do |config|
157
+ config.storage_options = $redis # Existing Redis connection, where $redis = Redis.new
158
+ end
159
+ ```
160
+
161
+ ### Client configuration options
162
+
163
+ setting | description
164
+ ------------------|-----------------------------------------------------------------------------------
165
+ `logger` | Define the logger to use. _(default: Rails.logger or ::Logger.new(STDOUT))_
166
+ `log_level` | Explicity define the logger level. _(default: ::Logger::WARN)_
167
+ `verbose` | When true, set `log_level` to ::Logger::DEBUG. _(default: false)_
168
+
169
+ You can define these configuration options as:
170
+
171
+ ```
172
+ SlackBotManager::Client.configure do |config|
173
+ config.check_interval = 10 # in seconds
174
+ config.log_level = ::Logger::INFO
175
+ end
176
+ ```
177
+
178
+
179
+ ### Additional configuration options
180
+
131
181
  For customization of Slack connections, including proxy, websocket ping, endpoint, user-agent, and more, check out the [slack-ruby-client README](https://github.com/dblock/slack-ruby-client/blob/master/README.md).
132
182
 
133
183
 
134
184
 
135
185
  ## Examples
136
186
 
137
- (TODO)
187
+ You can check a few creative examples in the [examples](examples/) folder.
138
188
 
139
189
 
140
190
 
@@ -150,6 +200,7 @@ Also thanks to [slack-ruby-client](https://github.com/dblock/slack-ruby-client).
150
200
 
151
201
  See [CONTRIBUTING](CONTRIBUTING.md).
152
202
 
203
+ Thanks to our contributors [Greg Leuch](https://gleu.ch) and [Alex Baldwin](http://goose.im).
153
204
 
154
205
 
155
206
  ## Copyright and License
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/gem_tasks'
4
+
5
+ Bundler.setup :default, :development
6
+
7
+ require 'rspec/core'
8
+ require 'rspec/core/rake_task'
9
+
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.pattern = FileList['spec/**/*_spec.rb']
12
+ end
13
+
14
+ require 'rubocop/rake_task'
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: [:rubocop, :spec]
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.2.3'
3
+
4
+ gem 'slack-bot-manager'
5
+ gem 'redis'
@@ -0,0 +1,40 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ eventmachine (1.0.9.1)
5
+ faraday (0.9.2)
6
+ multipart-post (>= 1.2, < 3)
7
+ faraday_middleware (0.10.0)
8
+ faraday (>= 0.7.4, < 0.10)
9
+ faye-websocket (0.10.2)
10
+ eventmachine (>= 0.12.0)
11
+ websocket-driver (>= 0.5.1)
12
+ gli (2.13.4)
13
+ hashie (3.4.3)
14
+ json (1.8.3)
15
+ multipart-post (2.0.0)
16
+ redis (3.2.2)
17
+ slack-bot-manager (0.1.0pre1)
18
+ faye-websocket (>= 0.10.0)
19
+ redis (>= 3.2.2)
20
+ slack-ruby-client (>= 0.5.1)
21
+ slack-ruby-client (0.6.0)
22
+ faraday
23
+ faraday_middleware
24
+ gli
25
+ hashie
26
+ json
27
+ websocket-driver
28
+ websocket-driver (0.6.3)
29
+ websocket-extensions (>= 0.1.0)
30
+ websocket-extensions (0.1.2)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ redis
37
+ slack-bot-manager
38
+
39
+ BUNDLED WITH
40
+ 1.10.6
@@ -0,0 +1,23 @@
1
+ # DM-Bot
2
+
3
+ An exmaple bot for handling direct messages with users using [slack-bot-manager](https://github.com/betaworks/slack-bot-manager).
4
+
5
+ ## Installation
6
+
7
+
8
+ Install `slack-bot-manager` and move over your `tokens.yml` file.
9
+
10
+ ```
11
+ bundle install
12
+ mv tokens.yml.sample tokens.yml
13
+ ```
14
+
15
+ Add your token(s) into `tokens.yml`.
16
+
17
+ Run it! `ruby dm-bot.rb`
18
+
19
+
20
+ ## License & Copyright
21
+
22
+ Copyright 2016 Greg Leuch & betaworks.
23
+ Licensed under MIT
@@ -0,0 +1,98 @@
1
+ #
2
+ # dm-bot.rb
3
+ # -------------------------------------------------
4
+ #
5
+ # Example created for slack-bot-manager by @gleuch.
6
+ # License: MIT
7
+ # https://github.com/betaworks/slack-bot-manager
8
+ #
9
+ # -------------------------------------------------
10
+ #
11
+ # To get started:
12
+ # 1. Run: `mv tokens.yml.sample tokens.yml`
13
+ # 2. Add your token(s) to tokens.yml
14
+ # 3. Run: `ruby dm-bot.rb`
15
+ #
16
+
17
+
18
+ # Require necessary gems
19
+ require 'yaml'
20
+
21
+ require 'rubygems'
22
+ require 'bundler/setup'
23
+ Bundler.require
24
+
25
+ # Use Redis and configure slack-bot-manager
26
+ $redis = Redis.new
27
+ SlackBotManager.configure do |config|
28
+ config.storage_options = $redis # Set entire instance as option
29
+ end
30
+
31
+ # Extend commands to handle what we want done
32
+ module SlackBotManager
33
+ module Commands
34
+
35
+ def on_message(data)
36
+ # Only support DMs with bot
37
+ return unless data['channel'].start_with?('D')
38
+
39
+ # Get user info
40
+ user_key = ['users', self.id, data['user']].join(':')
41
+ $redis.del user_key
42
+ unless $redis.exists user_key
43
+ # Get user info from Slack
44
+ user_info = self.connection.web_client.users_info(user: data['user'])['user']
45
+
46
+ # Store the basics into redis for quicker lookups
47
+ $redis.hmset user_key, *{ id: user_info['id'], name: user_info['name'], real_name: user_info['real_name'], is_bot: user_info['is_bot'] ? 1 : 0 }.flatten
48
+ # Expire this redis key weekly so that we do regular updates
49
+ $redis.expire user_key, 604800 # 1 week in seconds
50
+ end
51
+ user_info = $redis.hgetall user_key
52
+
53
+ return if [1,'1','true'].include?(user_info['is_bot'])
54
+
55
+ # Get IM info, send hello message if first time
56
+ im_key = ['ims', self.id, data['channel']].join(':')
57
+ $redis.del im_key
58
+ unless $redis.exists im_key
59
+ $redis.hmset im_key, *{ id: data['channel'], user: user_info['id'], started: Time.now.to_i, messages_sent: 0, messages_received: 0 }.flatten
60
+ self.send_message(data['channel'], "Hello there <@#{user_info['id']}>! I'm happy to listen to what you have to say. :simple_smile:")
61
+ $redis.hincrby im_key, 'messages_sent', 1
62
+ end
63
+ im_info = $redis.hgetall im_key
64
+
65
+ # Increment message count
66
+ $redis.hincrby im_key, 'messages_received', 1
67
+
68
+ # Parse message contents
69
+ message = 'If i was smarter, I would respond with something witty.'
70
+
71
+ self.send_message(data['channel'], message)
72
+ $redis.hincrby im_key, 'messages_sent', 1
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ # Initialize SlackBotManager
79
+ @bot_manager = SlackBotManager::Manager.new
80
+
81
+ # Load tokens from YAML list
82
+ tokens = YAML.load_file('tokens.yml')['tokens']
83
+ @bot_manager.add_token(*tokens) # Add tokens
84
+
85
+ # Close connections on sig interrupts
86
+ ['INT','TERM'].each do |s|
87
+ Signal.trap(s) do
88
+ @bot_manager.stop
89
+ @bot_manager.clear_tokens
90
+ exit
91
+ end
92
+ end
93
+
94
+ # Start the RTM connections
95
+ @bot_manager.start
96
+
97
+ # Lets monitor the connection
98
+ @bot_manager.monitor
@@ -0,0 +1,2 @@
1
+ tokens:
2
+ - 'FIRST TOKEN HERE'
@@ -1,6 +1,5 @@
1
1
  module SlackBotManager
2
2
  class Client
3
-
4
3
  include Commands
5
4
  include Errors
6
5
  include Logger
@@ -8,9 +7,13 @@ module SlackBotManager
8
7
  attr_accessor :commands, :connection, :id, :token, :status
9
8
  attr_accessor(*Config::CLIENT_ATTRIBUTES)
10
9
 
11
- def initialize(id, token, *args)
10
+ def initialize(token, *args)
12
11
  options = args.extract_options!
13
- @id, @token, @events, @status = id, token, options[:events] || {}, :disconnected
12
+
13
+ # Option values
14
+ @token = token
15
+ @id = options[:id]
16
+ @status = :disconnected
14
17
 
15
18
  # Setup client and assign commands
16
19
  @connection = Slack::RealTime::Client.new(token: @token)
@@ -21,28 +24,34 @@ module SlackBotManager
21
24
  end
22
25
 
23
26
  # Assign commands
24
- self.methods.each do |n|
27
+ methods.each do |n|
25
28
  # Require methods to include on_*
26
- next unless n.match(/^on_/) && self.respond_to?(n)
29
+ next unless n.match(/^on_/) && respond_to?(n)
27
30
  assign_event(n.to_s.gsub(/^on_/, ''), n)
28
31
  end
32
+ end
29
33
 
30
- connect
34
+ # Pull info from slack-ruby-client gem
35
+ SlackBotManager::Config::RTM_CLIENT_METHODS.each do |attr|
36
+ define_method "client_#{attr}" do
37
+ connection.send(attr) if connected?
38
+ end
31
39
  end
32
40
 
33
41
  def connect
34
42
  connection.start_async
43
+ @id ||= client_team['id']
35
44
  @status = :connected
36
45
  rescue => err
37
46
  handle_error(err)
38
47
  end
39
48
 
40
- def disconnect(reason=:disconnected)
49
+ def disconnect(reason = nil)
41
50
  connection && connection.stop!
42
51
  rescue => err
43
52
  handle_error(err)
44
53
  ensure
45
- @status = reason if @status == :connected
54
+ @status = reason || :disconnected # if @status == :connected
46
55
  remove_instance_variable(:@connection) if @connection
47
56
  end
48
57
 
@@ -54,8 +63,15 @@ module SlackBotManager
54
63
  !connected?
55
64
  end
56
65
 
66
+ def on(evt, &block)
67
+ self.class.send(:define_method, "on_#{evt}", &block)
68
+ assign_event(evt, "on_#{evt}")
69
+ end
57
70
 
58
- protected
71
+ def off(evt)
72
+ self.class.send(:remove_method, "on_#{evt}")
73
+ unassign_event(evt)
74
+ end
59
75
 
60
76
  def send_message(channel, text, *args)
61
77
  options = args.extract_options!
@@ -65,6 +81,8 @@ module SlackBotManager
65
81
  connection.message(options)
66
82
  end
67
83
 
84
+ protected
85
+
68
86
  def assign_event(evt, evt_name)
69
87
  connection.on(evt) do |data|
70
88
  begin
@@ -75,15 +93,33 @@ module SlackBotManager
75
93
  end
76
94
  end
77
95
 
96
+ def unassign_event(evt)
97
+ connection.off(evt) if connection
98
+ end
99
+
78
100
  # Handle different error cases
79
- def handle_error(err, data=nil)
101
+ def handle_error(err, data = nil)
80
102
  case determine_error_type(err)
81
- when :token_revoked; on_revoke(data)
82
- when :rate_limited; on_rate_limit(data)
83
- when :closed; on_close(data)
84
- else; on_error(err, data)
103
+ when :token_revoked
104
+ on_revoke(data)
105
+ when :rate_limited
106
+ on_rate_limit(data)
107
+ when :closed
108
+ on_close(data)
109
+ else
110
+ on_error(err, data)
85
111
  end
86
112
  end
87
113
 
114
+ # Include config helpers
115
+ class << self
116
+ def configure
117
+ block_given? ? yield(config) : config
118
+ end
119
+
120
+ def config
121
+ Config
122
+ end
123
+ end
88
124
  end
89
125
  end
@@ -1,26 +1,24 @@
1
1
  module SlackBotManager
2
2
  module Commands
3
-
4
3
  # Handle when connection gets closed
5
4
  def on_close(data, *args)
6
5
  options = args.extract_options!
7
6
  options[:code] ||= (data && data.code) || '1000'
8
7
 
9
8
  disconnect
10
- raise SlackBotManager::ConnectionRateLimited if ['1008','429'].include?(options[:code].to_s)
9
+ fail SlackBotManager::ConnectionRateLimited if %w(1008 429).include?(options[:code].to_s)
11
10
  end
12
11
 
13
12
  # Handle rate limit errors coming from web API
14
- def on_revoke(data)
13
+ def on_revoke(*)
15
14
  disconnect(:token_revoked)
16
- raise SlackBotManager::TokenRevoked
15
+ fail SlackBotManager::TokenRevoked
17
16
  end
18
17
 
19
18
  # Handle rate limit errors coming from web API
20
- def on_rate_limit(data)
19
+ def on_rate_limit(*)
21
20
  disconnect(:rate_limited)
22
- raise SlackBotManager::ConnectionRateLimited
21
+ fail SlackBotManager::ConnectionRateLimited
23
22
  end
24
-
25
23
  end
26
24
  end