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 10, 2016
1 parent cab992d commit 8bd165f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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).

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
20 changes: 20 additions & 0 deletions lib/slack/web/api/patches/chat.3.update-attachments-support.patch
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion slack-ruby-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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 8bd165f

Please sign in to comment.