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

Add attachments support for Slack::Web::Api::Endpoints::Chat.chat_update #69

Merged
merged 1 commit into from
Mar 10, 2016
Merged
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
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.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put this back please?

* [#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
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think if we're going to do this we should do it in chat_postMessage too. Maybe simplify the code:

attachments = JSON.dump(options[:attachments]) if options.key?(:attachments) && ! options[:attachments].is_a?(String)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh we already do that in chat_postMessage, my bad. You can maybe make the above change in a future commit (but feel free to do it here too).

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