Skip to content

Commit 08bc318

Browse files
authored
Merge pull request #97 from dblock/service-class
Added Config#service_class to override the SlackRubyBotServer::Service.instance singleton.
2 parents 71601dd + 9f6a9b3 commit 08bc318

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### 0.10.0 (Next)
44

5+
* [#97](https://github.com/slack-ruby/slack-ruby-bot-server/pull/97): Added `Config#service_class` to override the `SlackRubyBotServer::Service.instance` singleton - [@dblock](https://github.com/dblock).
56
* [#96](https://github.com/slack-ruby/slack-ruby-bot-server/pull/96): Added `Team#bot_user_id`, `activated_user_id` and `activated_user_access_token` - [@dblock](https://github.com/dblock).
67
* Your contribution here.
78

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ instance.on :creating do |team, error, options|
147147
end
148148
```
149149

150-
151150
#### Server Class
152151

153152
You can override the server class to handle additional events, and configure the service to use it.
@@ -168,6 +167,25 @@ SlackRubyBotServer.configure do |config|
168167
end
169168
```
170169

170+
#### Service Class
171+
172+
You can override the service class to handle additional methods.
173+
174+
```ruby
175+
class MyService < SlackRubyBotServer::Service
176+
def url
177+
'https://www.example.com'
178+
end
179+
end
180+
181+
SlackRubyBotServer.configure do |config|
182+
config.service_class = MyService
183+
end
184+
185+
SlackRubyBotServer::Service.instance # MyService
186+
SlackRubyBotServer::Service.instance.url # https://www.example.com
187+
```
188+
171189
### Access Tokens
172190

173191
By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores a `bot_access_token` as `token` that grants a certain amount of privileges to the bot user as described in [Slack OAuth Docs](https://api.slack.com/docs/oauth) along with `activated_user_access_token` that represents the token of the installing user. You may not want a bot user at all, or may require different auth scopes, such as `users.profile:read` to access user profile information via `Slack::Web::Client#users_profile_get`. To change required scopes make the following changes.

lib/slack-ruby-bot-server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'grape-swagger'
44
require 'slack-ruby-bot'
5+
require 'slack-ruby-bot-server/service'
56
require 'slack-ruby-bot-server/server'
67
require 'slack-ruby-bot-server/config'
78

@@ -13,4 +14,3 @@
1314

1415
require 'slack-ruby-bot-server/api'
1516
require 'slack-ruby-bot-server/app'
16-
require 'slack-ruby-bot-server/service'

lib/slack-ruby-bot-server/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ module Config
33
extend self
44

55
attr_accessor :server_class
6+
attr_accessor :service_class
67
attr_accessor :database_adapter
78

89
def reset!
910
self.server_class = SlackRubyBotServer::Server
11+
self.service_class = SlackRubyBotServer::Service
1012
self.database_adapter = if defined?(::Mongoid)
1113
:mongoid
1214
elsif defined?(::ActiveRecord)

lib/slack-ruby-bot-server/service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def self.start!
1010
end
1111

1212
def self.instance
13-
@instance ||= new
13+
@instance ||= SlackRubyBotServer::Config.service_class.new
1414
end
1515

1616
def initialize

spec/slack-ruby-bot-server/service_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ def initialize(options = {})
8787
expect(@events).to eq %w[starting started stopping stopped]
8888
end
8989
end
90+
context 'overriding service_class' do
91+
let(:service_class) do
92+
Class.new(SlackRubyBotServer::Service) do
93+
def url
94+
'https://www.example.com'
95+
end
96+
end
97+
end
98+
after do
99+
SlackRubyBotServer.config.reset!
100+
end
101+
it 'creates an instance of service class' do
102+
SlackRubyBotServer.configure do |config|
103+
config.service_class = service_class
104+
end
105+
expect(SlackRubyBotServer::Service.instance).to be_a service_class
106+
expect(SlackRubyBotServer::Service.instance.url).to eq 'https://www.example.com'
107+
end
108+
end
90109
end

0 commit comments

Comments
 (0)