Skip to content

Commit aa4d1e4

Browse files
authored
Merge pull request #537 from dblock/2.5.0-connection-options
Refactor pure text connection.
2 parents 2995885 + 1098255 commit aa4d1e4

File tree

12 files changed

+50
-74
lines changed

12 files changed

+50
-74
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-02-04 16:47:31 UTC using RuboCop version 1.26.1.
3+
# on 2025-02-05 16:58:54 UTC using RuboCop version 1.26.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -188,6 +188,11 @@ Style/GlobalStdStream:
188188
- 'spec/integration/integration_spec.rb'
189189
- 'spec/slack/web/client_spec.rb'
190190

191+
# Offense count: 1
192+
Style/MultilineBlockChain:
193+
Exclude:
194+
- 'lib/slack/web/api/helpers/files.rb'
195+
191196
# Offense count: 1
192197
# This cop supports safe auto-correction (--auto-correct).
193198
Style/MultilineTernaryOperator:

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
### 2.4.1 (Next)
1+
### 2.5.0 (Next)
22

33
* [#525](https://github.com/slack-ruby/slack-ruby-client/pull/525): Exclude spec files from gem package - [@amatsuda](https://github.com/amatsuda).
44
* [#527](https://github.com/slack-ruby/slack-ruby-client/pull/527): Explicitly require `racc` and `ostruct` - [@dblock](https://github.com/dblock).
55
* [#528](https://github.com/slack-ruby/slack-ruby-client/pull/528): Don't treat asterisks in list items as italic in markdown - [@rspeicher](https://github.com/rspeicher).
6-
* [#530](https://github.com/slack-ruby/slack-ruby-client/pull/530): Specify dependency on logger - [@rwstauner](https://github.com/rwstauner).
7-
* [#533](https://github.com/slack-ruby/slack-ruby-client/pull/533): Added `files_upload_external` - [@gregsaab](https://github.com/gregsaab).
8-
* [#536](https://github.com/slack-ruby/slack-ruby-client/pull/536): Rename `files_upload_external` to `files_upload_v2` - [@gregsaab](https://github.com/gregsaab).
6+
* [#530](https://github.com/slack-ruby/slack-ruby-client/pull/530): Specify dependency on `logger` - [@rwstauner](https://github.com/rwstauner).
7+
* [#533](https://github.com/slack-ruby/slack-ruby-client/pull/533), [#536](https://github.com/slack-ruby/slack-ruby-client/pull/536): Added `files_upload_v2` - [@gregsaab](https://github.com/gregsaab).
98
* Your contribution here.
109

1110
### 2.4.0 (2024/07/14)

lib/slack-ruby-client.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
require_relative 'slack/web/api/options'
3434
require_relative 'slack/web/faraday/response/raise_error'
3535
require_relative 'slack/web/faraday/response/wrap_error'
36-
require_relative 'slack/web/faraday/connection_options'
36+
require_relative 'slack/web/faraday/options'
3737
require_relative 'slack/web/faraday/connection'
38-
require_relative 'slack/web/faraday/plain_text_connection'
3938
require_relative 'slack/web/faraday/request'
4039
require_relative 'slack/web/api/mixins'
4140
require_relative 'slack/web/api/endpoints'

lib/slack/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# frozen_string_literal: true
22
module Slack
3-
VERSION = '2.4.1'
3+
VERSION = '2.5.0'
44
end

lib/slack/web/api/helpers/files.rb

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ module Files
99
# @see https://api.slack.com/changelog/2024-04-a-better-way-to-upload-files-is-here-to-stay
1010
# @see https://api.slack.com/messaging/files#uploading_files
1111
#
12-
# @option options [string] :filename
12+
# @option params [string] :filename
1313
# Name of the file being uploaded.
14-
# @option options [string] :content
14+
# @option params [string] :content
1515
# File contents via a POST variable.
16-
# @option options [string] :alt_txt
16+
# @option params [string] :alt_txt
1717
# Description of image for screen-reader.
18-
# @option options [string] :snippet_type
18+
# @option params [string] :snippet_type
1919
# Syntax type of the snippet being uploaded.
20-
# @option options [string] :title
20+
# @option params [string] :title
2121
# Title of file.
22-
# @option options [string] :channels
22+
# @option params [string] :channels
2323
# Comma-separated string of channel IDs where the file will be shared. If not specified the file will be private.
24-
# @option options [string] :initial_comment
24+
# @option params [string] :initial_comment
2525
# The message text introducing the file in specified channels.
26-
# @option options [string] :thread_ts
26+
# @option params [string] :thread_ts
2727
# Provide another message's ts value to upload this file as a reply.
2828
# Never use a reply's ts value; use its parent instead.
2929
# Also make sure to provide only one channel when using 'thread_ts'.
30-
def files_upload_v2(options = {})
30+
def files_upload_v2(params = {})
3131
%i[filename content channels].each do |param|
32-
raise ArgumentError, "Required argument :#{param} missing" if options[param].nil?
32+
raise ArgumentError, "Required argument :#{param} missing" if params[param].nil?
3333
end
3434

35-
content = options[:content]
36-
title = options[:title] || options[:filename]
35+
content = params[:content]
36+
title = params[:title] || params[:filename]
3737

38-
upload_url_request_params = options.slice(:filename, :alt_txt, :snippet_type)
38+
upload_url_request_params = params.slice(:filename, :alt_txt, :snippet_type)
3939
upload_url_request_params[:length] = content.bytesize
4040

4141
# Get the upload url.
@@ -44,13 +44,18 @@ def files_upload_v2(options = {})
4444
file_id = get_upload_url_response[:file_id]
4545

4646
# Upload the file.
47-
plain_text_connection.post do |request|
48-
request.url upload_url
47+
::Faraday::Connection.new(upload_url, options) do |connection|
48+
connection.request :multipart
49+
connection.request :url_encoded
50+
connection.use ::Slack::Web::Faraday::Response::WrapError
51+
connection.response :logger, logger if logger
52+
connection.adapter adapter
53+
end.post do |request|
4954
request.body = content
5055
end
5156

5257
# Complete the upload.
53-
complete_upload_request_params = options.slice(:channels, :initial_comment, :thread_ts)
58+
complete_upload_request_params = params.slice(:channels, :initial_comment, :thread_ts)
5459
complete_upload_request_params[:files] = [{ id: file_id, title: title }].to_json
5560

5661
files_completeUploadExternal(complete_upload_request_params)

lib/slack/web/client.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Slack
33
module Web
44
class Client
55
include Faraday::Connection
6-
include Faraday::PlainTextConnection
76
include Faraday::Request
87
include Api::Endpoints
98
include Api::Helpers

lib/slack/web/faraday/connection.rb

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,22 @@ module Slack
33
module Web
44
module Faraday
55
module Connection
6-
include ConnectionOptions
6+
include Slack::Web::Faraday::Options
77

88
private
99

10-
def options
11-
@options ||= begin
12-
options = connection_options.dup
13-
options[:headers]['Accept'] = 'application/json; charset=utf-8'
14-
options
15-
end
16-
end
17-
1810
def connection
19-
@connection ||=
20-
::Faraday::Connection.new(endpoint, options) do |connection|
21-
connection.request :multipart
22-
connection.request :url_encoded
23-
connection.use ::Slack::Web::Faraday::Response::RaiseError
24-
connection.response :mashify, mash_class: Slack::Messages::Message
25-
connection.response :json, content_type: /\b*$/
26-
connection.use ::Slack::Web::Faraday::Response::WrapError
27-
connection.response :logger, logger if logger
28-
connection.adapter adapter
29-
end
11+
@connection ||= ::Faraday::Connection.new(endpoint, options) do |connection|
12+
connection.headers['Accept'] = 'application/json; charset=utf-8'
13+
connection.request :multipart
14+
connection.request :url_encoded
15+
connection.use ::Slack::Web::Faraday::Response::RaiseError
16+
connection.response :mashify, mash_class: Slack::Messages::Message
17+
connection.response :json, content_type: /\b*$/
18+
connection.use ::Slack::Web::Faraday::Response::WrapError
19+
connection.response :logger, logger if logger
20+
connection.adapter adapter
21+
end
3022
end
3123
end
3224
end

lib/slack/web/faraday/connection_options.rb renamed to lib/slack/web/faraday/options.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
module Slack
33
module Web
44
module Faraday
5-
module ConnectionOptions
6-
def connection_options
7-
@connection_options ||= begin
5+
module Options
6+
def options
7+
@options ||= begin
88
options = { headers: {} }
99
options[:headers]['User-Agent'] = user_agent if user_agent
1010
options[:proxy] = proxy if proxy

lib/slack/web/faraday/plain_text_connection.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

spec/fixtures/slack/web/files_upload_v2.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)