Skip to content

Commit abffcb6

Browse files
committed
id_for enhancements
- avoid unintentional parameter forwarding via limit -> id_limit - switch to keyword parameters - deduce error text instead of using argument - enum_method_options -> options
1 parent fa9266d commit abffcb6

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

lib/slack/web/api/mixins/conversations.id.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ module Conversations
1212
#
1313
# @option options [channel] :channel
1414
# Channel to get ID for, prefixed with #.
15-
# @option options [integer] :limit
15+
# @option options [integer] :id_limit
1616
# The page size used for conversations_list calls required to find the channel's ID
1717
def conversations_id(options = {})
1818
name = options[:channel]
19-
limit = options.fetch(:limit, conversations_id_page_size)
19+
limit = options.fetch(:id_limit, conversations_id_page_size)
2020

2121
raise ArgumentError, 'Required arguments :channel missing' if name.nil?
2222

2323
id_for(
24-
:channel,
25-
name,
26-
'#',
27-
:conversations_list,
28-
:channels,
29-
'channel_not_found',
30-
enum_method_options: { limit: limit }
24+
key: :channel,
25+
name: name,
26+
prefix: '#',
27+
enum_method: :conversations_list,
28+
list_method: :channels,
29+
options: { limit: limit }
3130
)
3231
end
3332
end

lib/slack/web/api/mixins/ids.id.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ module Mixins
66
module Ids
77
private
88

9-
def id_for(key, name, prefix, enum_method, list_method, not_found_error, enum_method_options: {}) # rubocop:disable Metrics/ParameterLists
9+
def id_for(key:, name:, prefix:, enum_method:, list_method:, options: {})
1010
return { 'ok' => true, key.to_s => { 'id' => name } } unless name[0] == prefix
1111

12-
public_send(enum_method, **enum_method_options) do |list|
12+
public_send(enum_method, **options) do |list|
1313
list.public_send(list_method).each do |li|
1414
return Slack::Messages::Message.new('ok' => true, key.to_s => { 'id' => li.id }) if li.name == name[1..-1]
1515
end
1616
end
1717

18+
not_found_error = "#{key}_not_found"
1819
raise Slack::Web::Api::Errors::SlackError, not_found_error
1920
end
2021
end

lib/slack/web/api/mixins/users.id.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ module Users
1212
#
1313
# @option options [user] :user
1414
# User to get ID for, prefixed with '@'.
15-
# @option options [integer] :limit
15+
# @option options [integer] :id_limit
1616
# The page size used for users_list calls required to find the user's ID
1717
def users_id(options = {})
1818
name = options[:user]
19-
limit = options.fetch(:limit, users_id_page_size)
19+
limit = options.fetch(:id_limit, users_id_page_size)
2020

2121
raise ArgumentError, 'Required arguments :user missing' if name.nil?
2222

2323
id_for(
24-
:user,
25-
name,
26-
'@',
27-
:users_list,
28-
:members,
29-
'user_not_found',
30-
enum_method_options: { limit: limit }
24+
key: :user,
25+
name: name,
26+
prefix: '@',
27+
enum_method: :users_list,
28+
list_method: :members,
29+
options: { limit: limit }
3130
)
3231
end
3332
end

spec/slack/web/api/mixins/conversations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
it 'forwards a provided limit to the underlying conversations_list calls' do
4242
expect(conversations).to receive(:conversations_list).with(limit: 1234)
43-
conversations.conversations_id(channel: '#general', limit: 1234)
43+
conversations.conversations_id(channel: '#general', id_limit: 1234)
4444
end
4545

4646
it 'fails with an exception' do

spec/slack/web/api/mixins/users_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
it 'forwards a provided limit to the underlying users_list calls' do
3939
expect(users).to receive(:users_list).with(limit: 1234)
40-
users.users_id(user: '@aws', limit: 1234)
40+
users.users_id(user: '@aws', id_limit: 1234)
4141
end
4242

4343
it 'fails with an exception' do

0 commit comments

Comments
 (0)