-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON encoding to all applicable Web API arguments (#448)
* Method and template for json encoding options * regenerate files without patches * reintroduce missing patched lines and recreated patches [ci skip] * use api-ref to find JSON arguments * redo patches * update to latest slack-api-ref * use arg format to find json args * require the new file * add tests for encode_options_as_json * auto-generate tests for json params * safer strings in json tests * rubocop * add changelog entry * fix options hash in tests
- Loading branch information
Showing
29 changed files
with
290 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Slack | ||
module Web | ||
module Api | ||
module Options | ||
def encode_options_as_json(options, keys) | ||
encoded_options = options.slice(*keys).transform_values do |value| | ||
encode_json(value) | ||
end | ||
options.merge(encoded_options) | ||
end | ||
|
||
private | ||
|
||
def encode_json(value) | ||
if value.is_a?(String) | ||
value | ||
else | ||
JSON.dump(value) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,29 @@ | ||
diff --git a/lib/slack/web/api/endpoints/chat.rb b/lib/slack/web/api/endpoints/chat.rb | ||
index 701cd1e..07acc40 100644 | ||
index c3100ec..5909f4e 100644 | ||
--- a/lib/slack/web/api/endpoints/chat.rb | ||
+++ b/lib/slack/web/api/endpoints/chat.rb | ||
@@ -122,9 +122,21 @@ module Slack | ||
@@ -122,7 +122,7 @@ module Slack | ||
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postEphemeral.json | ||
def chat_postEphemeral(options = {}) | ||
raise ArgumentError, 'Required arguments :channel missing' if options[:channel].nil? | ||
- raise ArgumentError, 'Required arguments :text missing' if options[:text].nil? | ||
+ raise ArgumentError, 'Required arguments :text, :attachments or :blocks missing' if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil? | ||
raise ArgumentError, 'Required arguments :user missing' if options[:user].nil? | ||
options = options.merge(user: users_id(options)['user']['id']) if options[:user] | ||
+ # attachments must be passed as an encoded JSON string | ||
+ if options.key?(:attachments) | ||
+ attachments = options[:attachments] | ||
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String) | ||
+ options = options.merge(attachments: attachments) | ||
+ end | ||
+ # blocks must be passed as an encoded JSON string | ||
+ if options.key?(:blocks) | ||
+ blocks = options[:blocks] | ||
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String) | ||
+ options = options.merge(blocks: blocks) | ||
+ end | ||
post('chat.postEphemeral', options) | ||
end | ||
|
||
@@ -167,6 +179,19 @@ module Slack | ||
options = encode_options_as_json(options, %i[attachments blocks]) | ||
@@ -168,6 +168,7 @@ module Slack | ||
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.postMessage.json | ||
def chat_postMessage(options = {}) | ||
raise ArgumentError, 'Required arguments :channel missing' if options[:channel].nil? | ||
+ raise ArgumentError, 'Required arguments :text, :attachments or :blocks missing' if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil? | ||
+ # attachments must be passed as an encoded JSON string | ||
+ if options.key?(:attachments) | ||
+ attachments = options[:attachments] | ||
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String) | ||
+ options = options.merge(attachments: attachments) | ||
+ end | ||
+ # blocks must be passed as an encoded JSON string | ||
+ if options.key?(:blocks) | ||
+ blocks = options[:blocks] | ||
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String) | ||
+ options = options.merge(blocks: blocks) | ||
+ end | ||
options = encode_options_as_json(options, %i[attachments blocks metadata]) | ||
post('chat.postMessage', options) | ||
end | ||
|
||
@@ -268,8 +293,21 @@ module Slack | ||
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.update.json | ||
@@ -273,6 +274,7 @@ module Slack | ||
def chat_update(options = {}) | ||
raise ArgumentError, 'Required arguments :channel missing' if options[:channel].nil? | ||
+ raise ArgumentError, 'Required arguments :text, :attachments, :blocks or :reply_broadcast missing' if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil? && options[:reply_broadcast].nil? | ||
raise ArgumentError, 'Required arguments :ts missing' if options[:ts].nil? | ||
+ raise ArgumentError, 'Required arguments :text, :attachments, :blocks or :reply_broadcast missing' if options[:text].nil? && options[:attachments].nil? && options[:blocks].nil? && options[:reply_broadcast].nil? | ||
options = options.merge(channel: conversations_id(options)['channel']['id']) if options[:channel] | ||
+ # attachments must be passed as an encoded JSON string | ||
+ if options.key?(:attachments) | ||
+ attachments = options[:attachments] | ||
+ attachments = JSON.dump(attachments) unless attachments.is_a?(String) | ||
+ options = options.merge(attachments: attachments) | ||
+ end | ||
+ # blocks must be passed as an encoded JSON string | ||
+ if options.key?(:blocks) | ||
+ blocks = options[:blocks] | ||
+ blocks = JSON.dump(blocks) unless blocks.is_a?(String) | ||
+ options = options.merge(blocks: blocks) | ||
+ end | ||
options = encode_options_as_json(options, %i[attachments blocks metadata]) | ||
post('chat.update', options) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.