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

Use conversations_list instead of deprecated channels_list #331

Merged
merged 15 commits into from
Jul 7, 2020
Merged
1 change: 1 addition & 0 deletions lib/slack/web/api/endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ module Slack
module Web
module Api
module Endpoints
include Slack::Web::Api::Mixins::Conversations
include Slack::Web::Api::Mixins::Channels
include Slack::Web::Api::Mixins::Users
include Slack::Web::Api::Mixins::Groups
Expand Down
1 change: 1 addition & 0 deletions lib/slack/web/api/mixins.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative 'mixins/channels.id'
require_relative 'mixins/conversations.id'
require_relative 'mixins/users.id'
require_relative 'mixins/users.search'
require_relative 'mixins/groups.id'
27 changes: 27 additions & 0 deletions lib/slack/web/api/mixins/conversations.id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
require_relative 'ids.id'

module Slack
module Web
module Api
module Mixins
module Conversations
include Ids
#
# This method returns a channel ID given a channel name.
#
# @option options [channel] :channel
# Channel to get ID for, prefixed with #.
def conversations_id(options = {})
name = options[:channel]
throw ArgumentError.new('Required arguments :channel missing') if name.nil?

id_for(:channel, name, '#', :channels, 'channel_not_found') do
conversations_list
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/slack/web/api/templates/method.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module Slack
<% if data['group'] == 'groups' && data['args']['channel'] && !data['args']['channel']['desc'].include?('Can be an encoded ID, or a name.') %>
options = options.merge(channel: groups_id(options)['group']['id']) if options[:channel]
<% elsif data['args']['channel'] && !data['args']['channel']['desc'].include?('Can be an encoded ID, or a name.') %>
options = options.merge(channel: channels_id(options)['channel']['id']) if options[:channel]
options = options.merge(channel: conversations_id(options)['channel']['id']) if options[:channel]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is perfect, fixes the regression in all cases.

<% end %>
<% if data['args']['user'] %>
options = options.merge(user: users_id(options)['user']['id']) if options[:user]
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/web.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# largely from https://github.com/aki017/slack-ruby-gem
require 'json-schema'
require 'erubis'
require 'active_support'
require 'active_support/core_ext'

namespace :slack do
namespace :web do
Expand Down
43 changes: 43 additions & 0 deletions spec/slack/web/api/mixins/conversations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Slack::Web::Api::Mixins::Conversations do
subject(:conversations) do
klass.new
end

let(:klass) do
Class.new do
include Slack::Web::Api::Mixins::Conversations
end
end

before do
allow(conversations).to receive(:conversations_list).and_return(
Slack::Messages::Message.new(
'channels' => [{
'id' => 'CDEADBEEF',
'name' => 'general'
}]
)
)
end

context '#conversations_id' do
it 'leaves channels specified by ID alone' do
expect(conversations.conversations_id(channel: 'C123456')).to(
eq('ok' => true, 'channel' => { 'id' => 'C123456' })
)
end
it 'translates a channel that starts with a #' do
expect(conversations.conversations_id(channel: '#general')).to(
eq('ok' => true, 'channel' => { 'id' => 'CDEADBEEF' })
)
end
it 'fails with an exception' do
expect { conversations.conversations_id(channel: '#invalid') }.to(
raise_error(Slack::Web::Api::Errors::SlackError, 'channel_not_found')
)
end
end
end