Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Config#service_class to override the SlackRubyBotServer::Service.instance singleton. #97

Merged
merged 1 commit into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### 0.10.0 (Next)

* [#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).
* [#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).
* Your contribution here.

Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ instance.on :creating do |team, error, options|
end
```


#### Server Class

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

#### Service Class

You can override the service class to handle additional methods.

```ruby
class MyService < SlackRubyBotServer::Service
def url
'https://www.example.com'
end
end

SlackRubyBotServer.configure do |config|
config.service_class = MyService
end

SlackRubyBotServer::Service.instance # MyService
SlackRubyBotServer::Service.instance.url # https://www.example.com
```

### Access Tokens

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.
Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot-server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'grape-swagger'
require 'slack-ruby-bot'
require 'slack-ruby-bot-server/service'
require 'slack-ruby-bot-server/server'
require 'slack-ruby-bot-server/config'

Expand All @@ -13,4 +14,3 @@

require 'slack-ruby-bot-server/api'
require 'slack-ruby-bot-server/app'
require 'slack-ruby-bot-server/service'
2 changes: 2 additions & 0 deletions lib/slack-ruby-bot-server/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module Config
extend self

attr_accessor :server_class
attr_accessor :service_class
attr_accessor :database_adapter

def reset!
self.server_class = SlackRubyBotServer::Server
self.service_class = SlackRubyBotServer::Service
self.database_adapter = if defined?(::Mongoid)
:mongoid
elsif defined?(::ActiveRecord)
Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot-server/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.start!
end

def self.instance
@instance ||= new
@instance ||= SlackRubyBotServer::Config.service_class.new
end

def initialize
Expand Down
19 changes: 19 additions & 0 deletions spec/slack-ruby-bot-server/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,23 @@ def initialize(options = {})
expect(@events).to eq %w[starting started stopping stopped]
end
end
context 'overriding service_class' do
let(:service_class) do
Class.new(SlackRubyBotServer::Service) do
def url
'https://www.example.com'
end
end
end
after do
SlackRubyBotServer.config.reset!
end
it 'creates an instance of service class' do
SlackRubyBotServer.configure do |config|
config.service_class = service_class
end
expect(SlackRubyBotServer::Service.instance).to be_a service_class
expect(SlackRubyBotServer::Service.instance.url).to eq 'https://www.example.com'
end
end
end