Skip to content

Commit

Permalink
Adds views.publish method (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwright authored and dblock committed Oct 25, 2019
1 parent 2175a02 commit 2692773
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [#293](https://github.com/slack-ruby/slack-ruby-client/pull/293): Rubocop auto-correct and comprehensive todo - [@jcraigk](https://github.com/jcraigk).
* [#297](https://github.com/slack-ruby/slack-ruby-client/pull/297): Various Rubocop fixes - [@jcraigk](https://github.com/jcraigk).
* [#298](https://github.com/slack-ruby/slack-ruby-client/pull/298): Add `admin.apps`, `admin.app.requests` and `views` endpoints - [@jmanian](https://github.com/jmanian).
* [#302](https://github.com/slack-ruby/slack-ruby-client/pull/302): Add `oauth.v2.access` and `views.published` endpoints - [@jwright](https://github.com/jwright).
* Your contribution here.

### 0.14.4 (2019/7/28)
Expand Down
1 change: 1 addition & 0 deletions bin/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require 'commands/migration'
require 'commands/mpim'
require 'commands/oauth'
require 'commands/oauth_v2'
require 'commands/pins'
require 'commands/reactions'
require 'commands/reminders'
Expand Down
17 changes: 17 additions & 0 deletions bin/commands/oauth_v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
# This file was auto-generated by lib/tasks/web.rake

desc 'OauthV2 methods.'
command 'oauth_v2' do |g|
g.desc 'Exchanges a temporary OAuth verifier code for an access token.'
g.long_desc %( Exchanges a temporary OAuth verifier code for an access token. )
g.command 'access' do |c|
c.flag 'code', desc: 'The code param returned via the OAuth callback.'
c.flag 'client_id', desc: 'Issued when you created your application.'
c.flag 'client_secret', desc: 'Issued when you created your application.'
c.flag 'redirect_uri', desc: 'This must match the originally submitted URI (if one was sent).'
c.action do |_global_options, options, _args|
puts JSON.dump($client.oauth_v2_access(options))
end
end
end
4 changes: 1 addition & 3 deletions bin/commands/reactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
g.desc 'Adds a reaction to an item.'
g.long_desc %( Adds a reaction to an item. )
g.command 'add' do |c|
c.flag 'name', desc: 'Reaction (emoji) name.'
c.flag 'channel', desc: 'Channel where the message to add reaction to was posted.'
c.flag 'file', desc: "File to add reaction to. Now that file threads work the way you'd expect, this argument is deprecated. Specify the timestamp and channel of the message associated with a file instead."
c.flag 'file_comment', desc: "File comment to add reaction to. Now that file threads work the way you'd expect, this argument is deprecated. Specify the timestamp and channel of the message associated with a file instead."
c.flag 'name', desc: 'Reaction (emoji) name.'
c.flag 'timestamp', desc: 'Timestamp of the message to add reaction to.'
c.action do |_global_options, options, _args|
puts JSON.dump($client.reactions_add(options))
Expand Down
17 changes: 14 additions & 3 deletions bin/commands/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
g.long_desc %( Open a view for a user. )
g.command 'open' do |c|
c.flag 'trigger_id', desc: 'Exchange a trigger to post to the user.'
c.flag 'view', desc: 'The view payload. This must be a JSON-encoded string.'
c.flag 'view', desc: 'A view payload. This must be a JSON-encoded string.'
c.action do |_global_options, options, _args|
puts JSON.dump($client.views_open(options))
end
end

g.desc 'Publish a static view for a User.'
g.long_desc %( Publish a static view for a User. )
g.command 'publish' do |c|
c.flag 'user_id', desc: 'id of the user you want publish a view to.'
c.flag 'view', desc: 'A view payload. This must be a JSON-encoded string.'
c.flag 'hash', desc: 'A string that represents view state to protect against possible race conditions.'
c.action do |_global_options, options, _args|
puts JSON.dump($client.views_publish(options))
end
end

g.desc 'Push a view onto the stack of a root view.'
g.long_desc %( Push a view onto the stack of a root view. )
g.command 'push' do |c|
c.flag 'trigger_id', desc: 'Exchange a trigger to post to the user.'
c.flag 'view', desc: 'The view payload. This must be a JSON-encoded string.'
c.flag 'view', desc: 'A view payload. This must be a JSON-encoded string.'
c.action do |_global_options, options, _args|
puts JSON.dump($client.views_push(options))
end
Expand All @@ -26,7 +37,7 @@
g.desc 'Update an existing view.'
g.long_desc %( Update an existing view. )
g.command 'update' do |c|
c.flag 'view', desc: 'The view payload. This must be a JSON-encoded string.'
c.flag 'view', desc: 'A view payload This must be a JSON-encoded string.'
c.flag 'external_id', desc: 'A unique identifier of the view set by the developer. Must be unique for all views on a team. Max length of 255 characters. Either view_id or external_id is required.'
c.flag 'hash', desc: 'A string that represents view state to protect against possible race conditions.'
c.flag 'view_id', desc: 'A unique identifier of the view to be updated. Either view_id or external_id is required.'
Expand Down
2 changes: 2 additions & 0 deletions lib/slack/web/api/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_relative 'endpoints/migration'
require_relative 'endpoints/mpim'
require_relative 'endpoints/oauth'
require_relative 'endpoints/oauth_v2'
require_relative 'endpoints/pins'
require_relative 'endpoints/reactions'
require_relative 'endpoints/reminders'
Expand Down Expand Up @@ -77,6 +78,7 @@ module Endpoints
include Migration
include Mpim
include Oauth
include OauthV2
include Pins
include Reactions
include Reminders
Expand Down
30 changes: 30 additions & 0 deletions lib/slack/web/api/endpoints/oauth_v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true
# This file was auto-generated by lib/tasks/web.rake

module Slack
module Web
module Api
module Endpoints
module OauthV2
#
# Exchanges a temporary OAuth verifier code for an access token.
#
# @option options [Object] :code
# The code param returned via the OAuth callback.
# @option options [Object] :client_id
# Issued when you created your application.
# @option options [Object] :client_secret
# Issued when you created your application.
# @option options [Object] :redirect_uri
# This must match the originally submitted URI (if one was sent).
# @see https://api.slack.com/methods/oauth.v2.access
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/oauth.v2/oauth.v2.access.json
def oauth_v2_access(options = {})
throw ArgumentError.new('Required arguments :code missing') if options[:code].nil?
post('oauth.v2.access', options)
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/slack/web/api/endpoints/pins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Pins
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/pins/pins.add.json
def pins_add(options = {})
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
throw ArgumentError.new('Required arguments :timestamp missing') if options[:timestamp].nil?
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
post('pins.add', options)
end
Expand Down
10 changes: 4 additions & 6 deletions lib/slack/web/api/endpoints/reactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ module Reactions
#
# Adds a reaction to an item.
#
# @option options [Object] :name
# Reaction (emoji) name.
# @option options [channel] :channel
# Channel where the message to add reaction to was posted.
# @option options [file] :file
# File to add reaction to. Now that file threads work the way you'd expect, this argument is deprecated. Specify the timestamp and channel of the message associated with a file instead.
# @option options [Object] :file_comment
# File comment to add reaction to. Now that file threads work the way you'd expect, this argument is deprecated. Specify the timestamp and channel of the message associated with a file instead.
# @option options [Object] :name
# Reaction (emoji) name.
# @option options [Object] :timestamp
# Timestamp of the message to add reaction to.
# @see https://api.slack.com/methods/reactions.add
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/reactions/reactions.add.json
def reactions_add(options = {})
throw ArgumentError.new('Required arguments :channel missing') if options[:channel].nil?
throw ArgumentError.new('Required arguments :name missing') if options[:name].nil?
throw ArgumentError.new('Required arguments :timestamp missing') if options[:timestamp].nil?
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
post('reactions.add', options)
end
Expand Down
28 changes: 25 additions & 3 deletions lib/slack/web/api/endpoints/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Views
# @option options [Object] :trigger_id
# Exchange a trigger to post to the user.
# @option options [Object] :view
# The view payload. This must be a JSON-encoded string.
# A view payload. This must be a JSON-encoded string.
# @see https://api.slack.com/methods/views.open
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/views/views.open.json
def views_open(options = {})
Expand All @@ -26,13 +26,35 @@ def views_open(options = {})
post('views.open', options)
end

#
# Publish a static view for a User.
#
# @option options [Object] :user_id
# id of the user you want publish a view to.
# @option options [Object] :view
# A view payload. This must be a JSON-encoded string.
# @option options [Object] :hash
# A string that represents view state to protect against possible race conditions.
# @see https://api.slack.com/methods/views.publish
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/views/views.publish.json
def views_publish(options = {})
throw ArgumentError.new('Required arguments :user_id missing') if options[:user_id].nil?
throw ArgumentError.new('Required arguments :view missing') if options[:view].nil?
if options.key?(:view)
view = options[:view]
view = JSON.dump(view) unless view.is_a?(String)
options = options.merge(view: view)
end
post('views.publish', options)
end

#
# Push a view onto the stack of a root view.
#
# @option options [Object] :trigger_id
# Exchange a trigger to post to the user.
# @option options [Object] :view
# The view payload. This must be a JSON-encoded string.
# A view payload. This must be a JSON-encoded string.
# @see https://api.slack.com/methods/views.push
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/views/views.push.json
def views_push(options = {})
Expand All @@ -50,7 +72,7 @@ def views_push(options = {})
# Update an existing view.
#
# @option options [Object] :view
# The view payload. This must be a JSON-encoded string.
# A view payload This must be a JSON-encoded string.
# @option options [Object] :external_id
# A unique identifier of the view set by the developer. Must be unique for all views on a team. Max length of 255 characters. Either view_id or external_id is required.
# @option options [Object] :hash
Expand Down
16 changes: 16 additions & 0 deletions lib/slack/web/api/patches/views.1.views-published.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/lib/slack/web/api/endpoints/views.rb b/lib/slack/web/api/endpoints/views.rb
index 31626b9..8182a9c 100644
--- a/lib/slack/web/api/endpoints/views.rb
+++ b/lib/slack/web/api/endpoints/views.rb
@@ -40,6 +40,11 @@ module Slack
def views_publish(options = {})
throw ArgumentError.new('Required arguments :user_id missing') if options[:user_id].nil?
throw ArgumentError.new('Required arguments :view missing') if options[:view].nil?
+ if options.key?(:view)
+ view = options[:view]
+ view = JSON.dump(view) unless view.is_a?(String)
+ options = options.merge(view: view)
+ end
post('views.publish', options)
end

28 changes: 28 additions & 0 deletions spec/slack/web/api/endpoints/custom_specs/views_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@
end
end

describe 'views_publish' do
let(:user_id) { 'U1234' }

context 'with a hash for view' do
let(:view_str) { '{"celery":"man"}' }

it 'automatically converts view into JSON' do
expect(client).to receive(:post).with('views.publish',
user_id: user_id,
trigger_id: trigger_id,
view: view_str)
client.views_publish(user_id: user_id, trigger_id: trigger_id, view: { celery: 'man' })
end
end

context 'with a string for view' do
let(:view_str) { 'celery man' }

it 'leaves view as is' do
expect(client).to receive(:post).with('views.publish',
user_id: user_id,
trigger_id: trigger_id,
view: view_str)
client.views_publish(user_id: user_id, trigger_id: trigger_id, view: 'celery man')
end
end
end

describe 'views_push' do
context 'with a hash for view' do
let(:view_str) { '{"celery":"man"}' }
Expand Down
13 changes: 13 additions & 0 deletions spec/slack/web/api/endpoints/oauth_v2_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true
# This file was auto-generated by lib/tasks/web.rake

require 'spec_helper'

RSpec.describe Slack::Web::Api::Endpoints::OauthV2 do
let(:client) { Slack::Web::Client.new }
context 'oauth.v2_access' do
it 'requires code' do
expect { client.oauth_v2_access }.to raise_error ArgumentError, /Required arguments :code missing/
end
end
end
5 changes: 4 additions & 1 deletion spec/slack/web/api/endpoints/pins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
let(:client) { Slack::Web::Client.new }
context 'pins_add' do
it 'requires channel' do
expect { client.pins_add }.to raise_error ArgumentError, /Required arguments :channel missing/
expect { client.pins_add(timestamp: '1234567890.123456') }.to raise_error ArgumentError, /Required arguments :channel missing/
end
it 'requires timestamp' do
expect { client.pins_add(channel: 'C1234567890') }.to raise_error ArgumentError, /Required arguments :timestamp missing/
end
end
context 'pins_list' do
Expand Down
8 changes: 7 additions & 1 deletion spec/slack/web/api/endpoints/reactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
RSpec.describe Slack::Web::Api::Endpoints::Reactions do
let(:client) { Slack::Web::Client.new }
context 'reactions_add' do
it 'requires channel' do
expect { client.reactions_add(name: 'thumbsup', timestamp: '1234567890.123456') }.to raise_error ArgumentError, /Required arguments :channel missing/
end
it 'requires name' do
expect { client.reactions_add }.to raise_error ArgumentError, /Required arguments :name missing/
expect { client.reactions_add(channel: 'C1234567890', timestamp: '1234567890.123456') }.to raise_error ArgumentError, /Required arguments :name missing/
end
it 'requires timestamp' do
expect { client.reactions_add(channel: 'C1234567890', name: 'thumbsup') }.to raise_error ArgumentError, /Required arguments :timestamp missing/
end
end
context 'reactions_remove' do
Expand Down

0 comments on commit 2692773

Please sign in to comment.