Skip to content

Commit

Permalink
adding Dialog support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexagranov committed Sep 29, 2017
1 parent 8417e3a commit fffbce7
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.10.1 (next)

* Your contribution here.
* [#173](https://github.com/slack-ruby/slack-ruby-client/issues/173): Updated to latest slack-api-ref = we have Dialogs!; Added dialog.1.open-json-support patch to apply the JSON fix to :dialog param - [@alexagranov](https://github.com/alexagranov).

### 0.10.0 (9/19/2017)

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Sometimes it's necessary to patch auto-generated Slack Web API methods. For exam
Make a change to a generated file, for example `lib/slack/web/api/endpoints/chat.rb` and generate a patch.

```
git diff HEAD lib/slack/web/api/endpoints/chat.rb > lib/slack/web/api/patches/chat.1.patch
git diff --no-color HEAD lib/slack/web/api/endpoints/chat.rb > lib/slack/web/api/patches/chat.1.patch
```

Run `rake slack:api:update` to ensure that the patch is cleanly applied. Implement a test for the added or modified functionality and commit the patch file.
Expand Down
1 change: 1 addition & 0 deletions bin/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'commands/channels'
require 'commands/chat'
require 'commands/conversations'
require 'commands/dialog'
require 'commands/dnd'
require 'commands/emoji'
require 'commands/files_comments'
Expand Down
14 changes: 14 additions & 0 deletions bin/commands/dialog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was auto-generated by lib/tasks/web.rake

desc 'Dialog methods.'
command 'dialog' do |g|
g.desc 'Open a dialog with a user'
g.long_desc %( Open a dialog with a user )
g.command 'open' do |c|
c.flag 'dialog', desc: 'The dialog definition. This must be a JSON-encoded string.'
c.flag 'trigger_id', desc: 'Exchange a trigger to post to the user.'
c.action do |_global_options, options, _args|
puts JSON.dump($client.dialog_open(options))
end
end
end
2 changes: 2 additions & 0 deletions lib/slack/web/api/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative 'endpoints/channels'
require_relative 'endpoints/chat'
require_relative 'endpoints/conversations'
require_relative 'endpoints/dialog'
require_relative 'endpoints/dnd'
require_relative 'endpoints/emoji'
require_relative 'endpoints/files_comments'
Expand Down Expand Up @@ -43,6 +44,7 @@ module Endpoints
include Channels
include Chat
include Conversations
include Dialog
include Dnd
include Emoji
include FilesComments
Expand Down
6 changes: 6 additions & 0 deletions lib/slack/web/api/endpoints/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ module Dialog
def dialog_open(options = {})
throw ArgumentError.new('Required arguments :dialog missing') if options[:dialog].nil?
throw ArgumentError.new('Required arguments :trigger_id missing') if options[:trigger_id].nil?
# dialog must be passed as an encoded JSON string
if options.key?(:dialog)
dialog = options[:dialog]
dialog = JSON.dump(dialog) unless dialog.is_a?(String)
options = options.merge(dialog: dialog)
end
post('dialog.open', options)
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/slack/web/api/patches/dialog.1.open-json-support.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/lib/slack/web/api/endpoints/dialog.rb b/lib/slack/web/api/endpoints/dialog.rb
index 01f9dfd..d017adf 100644
--- a/lib/slack/web/api/endpoints/dialog.rb
+++ b/lib/slack/web/api/endpoints/dialog.rb
@@ -17,6 +17,12 @@ module Slack
def dialog_open(options = {})
throw ArgumentError.new('Required arguments :dialog missing') if options[:dialog].nil?
throw ArgumentError.new('Required arguments :trigger_id missing') if options[:trigger_id].nil?
+ # dialog must be passed as an encoded JSON string
+ if options.key?(:dialog)
+ dialog = options[:dialog]
+ dialog = JSON.dump(dialog) unless dialog.is_a?(String)
+ options = options.merge(dialog: dialog)
+ end
post('dialog.open', options)
end
end
28 changes: 28 additions & 0 deletions spec/slack/web/api/endpoints/custom_specs/dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

RSpec.describe Slack::Web::Api::Endpoints::Dialog do
let(:client) { Slack::Web::Client.new }
context 'dialog_open' do
it 'automatically converts dialog into JSON' do
expect(client).to receive(:post).with(
'dialog.open',
trigger_id: '12345.98765.abcd2358fdea',
dialog: '[]'
)
client.dialog_open(trigger_id: '12345.98765.abcd2358fdea', dialog: [])
end

context 'arguments' do
it 'requires dialog' do
expect { client.dialog_open(trigger_id: '123') }.to raise_error ArgumentError, /Required arguments :dialog missing/
end
it 'requires trigger_id' do
expect { client.dialog_open(dialog: []) }.to raise_error ArgumentError, /Required arguments :trigger_id missing/
end
it 'likes both dialog and trigger_id' do
expect(client).to receive(:post).with('dialog.open', hash_including(trigger_id: '123', dialog: '[]'))
expect { client.dialog_open(dialog: [], trigger_id: '123') }.to_not raise_error
end
end
end
end
15 changes: 15 additions & 0 deletions spec/slack/web/api/endpoints/dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was auto-generated by lib/tasks/web.rake

require 'spec_helper'

RSpec.describe Slack::Web::Api::Endpoints::Dialog do
let(:client) { Slack::Web::Client.new }
context 'dialog_open' do
it 'requires dialog' do
expect { client.dialog_open(trigger_id: '12345.98765.abcd2358fdea') }.to raise_error ArgumentError, /Required arguments :dialog missing/
end
it 'requires trigger_id' do
expect { client.dialog_open(dialog: ' ') }.to raise_error ArgumentError, /Required arguments :trigger_id missing/
end
end
end

0 comments on commit fffbce7

Please sign in to comment.