Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
@@ -1,6 +1,7 @@
### 0.18.0 (Next)

* Your contribution here.
* [#382](https://github.com/slack-ruby/slack-ruby-client/pull/382): Add option to extract OAuth scopes from Web response headers - [@jmanian](https://github.com/jmanian).
* [#380](https://github.com/slack-ruby/slack-ruby-client/pull/380): Updates to server error classes and hierarchy - [@jmanian](https://github.com/jmanian).

### 0.17.0 (2021/03/07)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ open_timeout | Optional connection open timeout in seconds.
default_page_size | Optional page size for paginated requests, default is _100_.
default_max_retries | Optional number of retries for paginated requests, default is _100_.
adapter | Optional HTTP adapter to use, defaults to `Faraday.default_adapter`.
store_scopes | Option to populate `oauth_scopes` on the client from the response headers after a request.

You can also pass request options, including `timeout` and `open_timeout` into individual calls.

Expand Down
1 change: 1 addition & 0 deletions lib/slack-ruby-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require_relative 'slack/web/api/errors/server_error'
require_relative 'slack/web/faraday/response/raise_error'
require_relative 'slack/web/faraday/response/wrap_error'
require_relative 'slack/web/faraday/response/store_scopes'
require_relative 'slack/web/faraday/connection'
require_relative 'slack/web/faraday/request'
require_relative 'slack/web/api/mixins'
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/web/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Client
include Faraday::Request
include Api::Endpoints

attr_accessor(*Config::ATTRIBUTES)
attr_accessor(*Config::ATTRIBUTES, :oauth_scopes)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about making this attr_reader :oauth_scopes so that it wouldn't appear that you can set the scopes here. But then it would need to use instance_variable_set from the middleware to set them. Not sure which is better.


def initialize(options = {})
Slack::Web::Config::ATTRIBUTES.each do |key|
Expand Down
2 changes: 2 additions & 0 deletions lib/slack/web/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Config
default_page_size
default_max_retries
adapter
store_scopes
].freeze

attr_accessor(*Config::ATTRIBUTES)
Expand All @@ -34,6 +35,7 @@ def reset
self.default_page_size = 100
self.default_max_retries = 100
self.adapter = ::Faraday.default_adapter
self.store_scopes = nil
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/slack/web/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def connection
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
connection.use ::FaradayMiddleware::ParseJson
connection.use ::Slack::Web::Faraday::Response::WrapError
connection.use ::Slack::Web::Faraday::Response::StoreScopes, client: self if store_scopes
connection.response :logger, logger if logger
connection.adapter adapter
end
Expand Down
22 changes: 22 additions & 0 deletions lib/slack/web/faraday/response/store_scopes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
module Slack
module Web
module Faraday
module Response
class StoreScopes < ::Faraday::Response::Middleware
attr_accessor :client

def initialize(app = nil, options = {})
super(app)
self.client = options[:client]
end

def on_complete(env)
raw_scopes = env.response_headers[:'x-oauth-scopes']
client.oauth_scopes = raw_scopes&.split(',')
end
end
end
end
end
end
42 changes: 42 additions & 0 deletions spec/lib/slack/web/faraday/response/store_scopes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Slack::Web::Faraday::Response::StoreScopes do
let(:store_scopes_obj) { described_class.new(app, client: client) }
let(:app) { proc {} }
let(:client) { Slack::Web::Client.new }
let(:env) { instance_double(Faraday::Env, response_headers: response_headers) }

describe '#on_complete' do
subject(:on_complete) { store_scopes_obj.on_complete(env) }

context 'with no x-oauth-scopes header' do
let(:response_headers) { Faraday::Utils::Headers.new }

it 'does not set oauth_scopes on the client' do
on_complete
expect(client.oauth_scopes).to be_nil
end
end

context 'with x-oauth-scopes in an unexpected format' do
let(:raw_scopes) { 'this is not what scopes look like' }
let(:response_headers) { Faraday::Utils::Headers.new('x-oauth-scopes': raw_scopes) }

it 'does not crash' do
on_complete
expect(client.oauth_scopes).to contain_exactly(raw_scopes)
end
end

context 'with x-oauth-scopes in an expected format' do
let(:scopes) { %w[chat:write chat:write.public chat:write:user commands identity.basic] }
let(:response_headers) { Faraday::Utils::Headers.new('x-oauth-scopes': scopes.join(',')) }

it 'parses the scopes into an array' do
on_complete
expect(client.oauth_scopes).to match_array(scopes)
end
end
end
end