Skip to content

Commit 9b20556

Browse files
authored
Update new methods (#326)
1 parent 4a74321 commit 9b20556

11 files changed

+65
-126
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### 0.15.0 (Next)
22

3-
* [#323](https://github.com/slack-ruby/slack-ruby-client/pull/322): Require Faraday >= 1.0 - [@dblock](https://github.com/dblock).
4-
* [#323](https://github.com/slack-ruby/slack-ruby-client/pull/322): Upgrade slack-ruby-danger, rubocop, rubocop-rspec and rubocop-performance - [@dblock](https://github.com/dblock).
3+
* [#326](https://github.com/slack-ruby/slack-ruby-client/pull/326): Update new methods - [@wasabigeek](https://github.com/wasabigeek).
4+
* [#324](https://github.com/slack-ruby/slack-ruby-client/pull/324): Require Faraday >= 1.0 - [@dblock](https://github.com/dblock).
5+
* [#324](https://github.com/slack-ruby/slack-ruby-client/pull/324): Upgrade slack-ruby-danger, rubocop, rubocop-rspec and rubocop-performance - [@dblock](https://github.com/dblock).
56
* [#322](https://github.com/slack-ruby/slack-ruby-client/pull/322): Cache `Faraday::Connection` for persistent adapters - [@drbrain](https://github.com/drbrain).
67
* Your contribution here.
78

CONTRIBUTING.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,31 @@ rake slack:api:update
7575

7676
Sometimes it's necessary to patch auto-generated Slack Web API methods. For example, we want to help clients with calling `to_json` on the `attachments` parameter sent to `chat_postMessage`. See [#20](https://github.com/slack-ruby/slack-ruby-client/issues/20).
7777

78-
Make a change to a generated file, for example `lib/slack/web/api/endpoints/chat.rb` and generate a patch.
78+
The broad steps are:
79+
1. Run `rake slack:api:update` to check that existing patches are still valid.
80+
- If you run into a `failed to apply patch` error, the auto-generated methods likely drifted from the last patch. Follow the steps [below](#resolving-patch-errors).
81+
- This may add new methods if the API has updated, please split them up into multiple PRs if so.
82+
2. Make a change to a generated file, for example `lib/slack/web/api/endpoints/chat.rb`.
83+
3. Generate a patch:
84+
```
85+
git diff --no-color HEAD lib/slack/web/api/endpoints/chat.rb > lib/slack/web/api/patches/chat.1.patch
86+
```
87+
4. Run `rake slack:api:update` to ensure that the patch is cleanly applied. Implement a test for the added or modified functionality and commit the patch file.
88+
89+
##### Resolving Patch Errors
90+
91+
The auto-generated method files may drift overtime e.g. new arguments may be added or descriptions changed. Since previous patches were based on the older auto-generated files, git may be unable to apply them to the new files. Resolving them requires some good ol' splicing:
92+
1. Comment out the patching code in `lib/tasks/web.rake`:
93+
```ruby
94+
# Dir.glob("lib/slack/web/api/patches/#{group}*.patch").sort.each do |patch|
95+
# puts "- patching #{patch}"
96+
# system("git apply #{patch}") || raise('failed to apply patch')
97+
# end
98+
```
99+
2. Run `rake slack:api:update` to create the raw auto-generated files. Commit the files that you are updating, so we can run `git diff` later.
100+
3. Go through the old patches for the files (e.g. in `lib/slack/web/api/patches/chat.1.patch`), copying code into the new files.
101+
4. Continue with Step 2 [above](#patching-slack-web-api).
79102
80-
```
81-
git diff --no-color HEAD lib/slack/web/api/endpoints/chat.rb > lib/slack/web/api/patches/chat.1.patch
82-
```
83-
84-
Run `rake slack:api:update` to ensure that the patch is cleanly applied. Implement a test for the added or modified functionality and commit the patch file.
85103
86104
### Write Documentation
87105

bin/commands/chat.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@
137137
g.long_desc %( Updates a message. )
138138
g.command 'update' do |c|
139139
c.flag 'channel', desc: 'Channel containing the message to be updated.'
140-
c.flag 'text', desc: "New text for the message, using the default formatting rules. It's not required when presenting attachments."
141140
c.flag 'ts', desc: 'Timestamp of the message to be updated.'
142141
c.flag 'as_user', desc: 'Pass true to update the message as the authed user. Bot users in this context are considered authed users.'
143-
c.flag 'attachments', desc: 'A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text.'
144-
c.flag 'blocks', desc: 'A JSON-based array of structured blocks, presented as a URL-encoded string.'
145-
c.flag 'link_names', desc: 'Find and link channel names and usernames. Defaults to none. See below.'
146-
c.flag 'parse', desc: 'Change how messages are treated. Defaults to client, unlike chat.postMessage. See below.'
142+
c.flag 'attachments', desc: "A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text. If you don't include this field, the message's previous attachments will be retained. To remove previous attachments, include an empty array for this field."
143+
c.flag 'blocks', desc: "A JSON-based array of structured blocks, presented as a URL-encoded string. If you don't include this field, the message's previous blocks will be retained. To remove previous blocks, include an empty array for this field."
144+
c.flag 'link_names', desc: 'Find and link channel names and usernames. Defaults to none. If you do not specify a value for this field, the original value set for the message will be overwritten with the default, none.'
145+
c.flag 'parse', desc: 'Change how messages are treated. Defaults to client, unlike chat.postMessage. Accepts either none or full. If you do not specify a value for this field, the original value set for the message will be overwritten with the default, client.'
146+
c.flag 'text', desc: "New text for the message, using the default formatting rules. It's not required when presenting blocks or attachments."
147147
c.action do |_global_options, options, _args|
148148
puts JSON.dump($client.chat_update(options))
149149
end

lib/slack/web/api/endpoints/chat.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,20 @@ def chat_unfurl(options = {})
259259
#
260260
# @option options [channel] :channel
261261
# Channel containing the message to be updated.
262-
# @option options [Object] :text
263-
# New text for the message, using the default formatting rules. It's not required when presenting attachments.
264262
# @option options [timestamp] :ts
265263
# Timestamp of the message to be updated.
266264
# @option options [Object] :as_user
267265
# Pass true to update the message as the authed user. Bot users in this context are considered authed users.
268266
# @option options [Object] :attachments
269-
# A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text.
267+
# A JSON-based array of structured attachments, presented as a URL-encoded string. This field is required when not presenting text. If you don't include this field, the message's previous attachments will be retained. To remove previous attachments, include an empty array for this field.
270268
# @option options [Object] :blocks
271-
# A JSON-based array of structured blocks, presented as a URL-encoded string.
269+
# A JSON-based array of structured blocks, presented as a URL-encoded string. If you don't include this field, the message's previous blocks will be retained. To remove previous blocks, include an empty array for this field.
272270
# @option options [Object] :link_names
273-
# Find and link channel names and usernames. Defaults to none. See below.
271+
# Find and link channel names and usernames. Defaults to none. If you do not specify a value for this field, the original value set for the message will be overwritten with the default, none.
274272
# @option options [Object] :parse
275-
# Change how messages are treated. Defaults to client, unlike chat.postMessage. See below.
273+
# Change how messages are treated. Defaults to client, unlike chat.postMessage. Accepts either none or full. If you do not specify a value for this field, the original value set for the message will be overwritten with the default, client.
274+
# @option options [Object] :text
275+
# New text for the message, using the default formatting rules. It's not required when presenting blocks or attachments.
276276
# @see https://api.slack.com/methods/chat.update
277277
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.update.json
278278
def chat_update(options = {})

lib/slack/web/api/patches/chat.6.block-kit-support.patch renamed to lib/slack/web/api/patches/chat.1.patch

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
diff --git a/lib/slack/web/api/endpoints/chat.rb b/lib/slack/web/api/endpoints/chat.rb
2-
index 54a7db1..c535bb5 100644
2+
index dede9c8..94f7085 100644
33
--- a/lib/slack/web/api/endpoints/chat.rb
44
+++ b/lib/slack/web/api/endpoints/chat.rb
5-
@@ -97,7 +97,7 @@ module Slack
5+
@@ -121,11 +121,22 @@ module Slack
6+
# @see https://api.slack.com/methods/chat.postEphemeral
67
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postEphemeral.json
78
def chat_postEphemeral(options = {})
9+
- throw ArgumentError.new('Required arguments :attachments missing') if options[:attachments].nil?
810
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
9-
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
11+
- throw ArgumentError.new('Required arguments :text missing') if options[:text].nil?
1012
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
1113
throw ArgumentError.new('Required arguments :user missing') if options[:user].nil?
1214
options = options.merge(user: users_id(options)['user']['id']) if options[:user]
13-
# attachments must be passed as an encoded JSON string
14-
@@ -106,6 +106,12 @@ module Slack
15-
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
16-
options = options.merge(attachments: attachments)
17-
end
15+
+ # attachments must be passed as an encoded JSON string
16+
+ if options.key?(:attachments)
17+
+ attachments = options[:attachments]
18+
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String)
19+
+ options = options.merge(attachments: attachments)
20+
+ end
1821
+ # blocks must be passed as an encoded JSON string
1922
+ if options.key?(:blocks)
2023
+ blocks = options[:blocks]
@@ -24,18 +27,18 @@ index 54a7db1..c535bb5 100644
2427
post('chat.postEphemeral', options)
2528
end
2629

27-
@@ -146,13 +152,19 @@ module Slack
30+
@@ -166,7 +177,19 @@ module Slack
2831
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postMessage.json
2932
def chat_postMessage(options = {})
3033
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
31-
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
34+
- throw ArgumentError.new('Required arguments :text missing') if options[:text].nil?
3235
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
33-
# attachments must be passed as an encoded JSON string
34-
if options.key?(:attachments)
35-
attachments = options[:attachments]
36-
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
37-
options = options.merge(attachments: attachments)
38-
end
36+
+ # attachments must be passed as an encoded JSON string
37+
+ if options.key?(:attachments)
38+
+ attachments = options[:attachments]
39+
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String)
40+
+ options = options.merge(attachments: attachments)
41+
+ end
3942
+ # blocks must be passed as an encoded JSON string
4043
+ if options.key?(:blocks)
4144
+ blocks = options[:blocks]
@@ -45,19 +48,19 @@ index 54a7db1..c535bb5 100644
4548
post('chat.postMessage', options)
4649
end
4750

48-
@@ -204,7 +216,7 @@ module Slack
51+
@@ -254,8 +277,21 @@ module Slack
4952
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.update.json
5053
def chat_update(options = {})
5154
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
52-
- throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
5355
+ throw ArgumentError.new('Required arguments :text, :attachments or :blocks missing') if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil?
5456
throw ArgumentError.new('Required arguments :ts missing') if options[:ts].nil?
5557
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
56-
# attachments must be passed as an encoded JSON string
57-
@@ -213,6 +225,12 @@ module Slack
58-
attachments = JSON.dump(attachments) unless attachments.is_a?(String)
59-
options = options.merge(attachments: attachments)
60-
end
58+
+ # attachments must be passed as an encoded JSON string
59+
+ if options.key?(:attachments)
60+
+ attachments = options[:attachments]
61+
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String)
62+
+ options = options.merge(attachments: attachments)
63+
+ end
6164
+ # blocks must be passed as an encoded JSON string
6265
+ if options.key?(:blocks)
6366
+ blocks = options[:blocks]

lib/slack/web/api/patches/chat.1.text-attachments-required.patch

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

lib/slack/web/api/patches/chat.2.attachments-json.patch

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

lib/slack/web/api/patches/chat.3.update-attachments-support.patch

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

lib/slack/web/api/patches/chat.4.postEphemeral-attachments-support.patch

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

lib/slack/web/api/patches/chat.5.postEphemeral-text-or-attachments.patch

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

0 commit comments

Comments
 (0)