diff --git a/CHANGELOG.md b/CHANGELOG.md index 40d29452..327dd1c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ### 0.7.1 (Next) * Your contribution here. +* [#69](https://github.com/dblock/slack-ruby-client/issues/69): Add attachments support for `Slack::Web::Api::Endpoints::Chat.chat_update` - [@nicka](https://github.com/nicka). ### 0.7.0 (3/6/2016) @@ -95,4 +96,3 @@ ### 0.1.0 (7/25/2015) * Initial public release with Web and RealTime Messaging API support - [@dblock](https://github.com/dblock). - diff --git a/lib/slack/web/api/endpoints/chat.rb b/lib/slack/web/api/endpoints/chat.rb index 0db7e672..1c3b2eeb 100644 --- a/lib/slack/web/api/endpoints/chat.rb +++ b/lib/slack/web/api/endpoints/chat.rb @@ -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 diff --git a/lib/slack/web/api/patches/chat.3.update-attachments-support.patch b/lib/slack/web/api/patches/chat.3.update-attachments-support.patch new file mode 100644 index 00000000..9f06de1f --- /dev/null +++ b/lib/slack/web/api/patches/chat.3.update-attachments-support.patch @@ -0,0 +1,20 @@ +diff --git a/lib/slack/web/api/endpoints/chat.rb b/lib/slack/web/api/endpoints/chat.rb +index 0db7e67..1c3b2ee 100644 +--- a/lib/slack/web/api/endpoints/chat.rb ++++ b/lib/slack/web/api/endpoints/chat.rb +@@ -82,8 +82,14 @@ module Slack + 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 diff --git a/slack-ruby-client.gemspec b/slack-ruby-client.gemspec index cb6e58d7..9a4884b5 100644 --- a/slack-ruby-client.gemspec +++ b/slack-ruby-client.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |s| s.add_dependency 'hashie' s.add_development_dependency 'erubis' s.add_development_dependency 'json-schema' - s.add_development_dependency 'rake' + s.add_development_dependency 'rake', '~> 10' s.add_development_dependency 'rspec' s.add_development_dependency 'vcr' s.add_development_dependency 'webmock' diff --git a/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb b/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb index a4544fbd..f4f46385 100644 --- a/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb +++ b/spec/slack/web/api/endpoints/custom_specs/chat_spec.rb @@ -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