Skip to content

Commit

Permalink
Add attachments support for Slack::Web::Api::Endpoints::Chat.chat_update
Browse files Browse the repository at this point in the history
  • Loading branch information
nicka committed Mar 9, 2016
1 parent cab992d commit c0b55a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/slack/web/api/endpoints/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ def chat_postMessage(options = {})
def chat_update(options = {})
throw ArgumentError.new('Required arguments :ts missing') if options[:ts].nil?
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
throw ArgumentError.new('Required arguments :text missing') if options[:text].nil?
throw ArgumentError.new('Required arguments :text or :attachments missing') if options[:text].nil? && options[:attachments].nil?
options = options.merge(channel: channels_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
post('chat.update', options)
end
end
Expand Down
36 changes: 36 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 @@ -30,4 +30,40 @@
end
end
end

context 'chat_update' do
let(:ts) { '1405894322.002768' }
it 'automatically converts attachments into JSON' do
expect(client).to receive(:post).with(
'chat.update',
attachments: '[]',
channel: 'channel',
text: 'text',
ts: ts
)
client.chat_update(attachments: [], channel: 'channel', text: 'text', ts: ts)
end
context 'ts arguments' do
it 'requires ts' do
expect { client.chat_update(channel: 'channel') }.to raise_error ArgumentError, /Required arguments :ts missing>/
end
end
context 'text and attachment arguments' do
it 'requires text or attachments' do
expect { client.chat_update(channel: 'channel', ts: ts) }.to raise_error ArgumentError, /Required arguments :text or :attachments missing/
end
it 'only text' do
expect(client).to receive(:post).with('chat.update', hash_including(text: 'text'))
expect { client.chat_update(channel: 'channel', text: 'text', ts: ts) }.to_not raise_error
end
it 'only attachments' do
expect(client).to receive(:post).with('chat.update', hash_including(attachments: '[]'))
expect { client.chat_update(attachments: [], channel: 'channel', ts: ts) }.to_not raise_error
end
it 'both text and attachments' do
expect(client).to receive(:post).with('chat.update', hash_including(text: 'text', attachments: '[]'))
expect { client.chat_update(attachments: [], channel: 'channel', text: 'text', ts: ts) }.to_not raise_error
end
end
end
end

0 comments on commit c0b55a9

Please sign in to comment.