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

feat: Allows either unfurl_id & source or channel & ts in chat.unfurl method #502

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions lib/slack/web/api/endpoints/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ def chat_scheduleMessage(options = {})
# @see https://api.slack.com/methods/chat.unfurl
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/chat/chat.unfurl.json
def chat_unfurl(options = {})
raise ArgumentError, 'Required arguments :channel missing' if options[:channel].nil?
raise ArgumentError, 'Required arguments :ts missing' if options[:ts].nil?
if options[:channel].nil? || options[:ts].nil?
if options[:unfurl_id].nil? || options[:source].nil?
raise ArgumentError, 'Either a combination of :channel and :ts or :unfurl_id and :source is required'
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code is generated, so you'll have to either make a patch in https://github.com/slack-ruby/slack-ruby-client/tree/master/lib/slack/web/api/patches, or figure out a way to specify mutually exclusive options.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note the code as is would allow channel and source, so you will want to do something else.

raise ArgumentError, 'Required arguments :unfurls missing' if options[:unfurls].nil?
options = options.merge(channel: conversations_id(options)['channel']['id']) if options[:channel]
options = encode_options_as_json(options, %i[unfurls user_auth_blocks])
Expand Down
89 changes: 89 additions & 0 deletions spec/slack/web/api/endpoints/custom_specs/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,95 @@
end
end

context '#chat_unfurl' do
it 'calls the post method with the correct parameters channel and ts' do
# Mock the conversations_id method
expect(client).to receive(:conversations_id).with(hash_including(channel: 'channel')).and_return({'channel' => {'id' => 'channel_id'}})

# Expect the post method to be called with the updated parameters
expect(client).to receive(:post).with('chat.unfurl', hash_including(
channel: 'channel_id',
ts: 'timestamp',
unfurls: {
'http://www.foo.com' => {
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "This is a plain text section block.",
"emoji": true
}
}
]
}
}
))

# Call the chat_unfurl method with valid arguments
client.chat_unfurl(channel: 'channel', ts: 'timestamp', unfurls: {
'http://www.foo.com' => {
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "This is a plain text section block.",
"emoji": true
}
}
]
}
})
end

it 'calls the post method with the correct parameters unfurl_id and source' do
# Expect the post method to be called with the updated parameters
expect(client).to receive(:post).with('chat.unfurl', hash_including(
unfurl_id: 'unfurl_id',
source: 'source',
unfurls: {
'http://www.foo.com' => {
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "This is a plain text section block.",
"emoji": true
}
}
]
}
}
))

# Call the chat_unfurl method with valid arguments
client.chat_unfurl(unfurl_id: 'unfurl_id', source: 'source', unfurls: {
'http://www.foo.com' => {
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "This is a plain text section block.",
"emoji": true
}
}
]
}
})
end

it 'raises an error if :channel and :ts are missing' do
expect { client.chat_unfurl(unfurls: {}) }.to raise_error(ArgumentError, /Either a combination of :channel and :ts or :unfurl_id and :source is required/)
end

it 'raises an error if :unfurls is missing' do
expect { client.chat_unfurl(channel: 'channel', ts: 'timestamp') }.to raise_error(ArgumentError, /Required arguments :unfurls missing/)
end
end

context 'chat_update' do
let(:ts) { '1405894322.002768' }

Expand Down
Loading