Class: Ruboty::SlackTakeTurns::SlackClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/slack_take_turns/slack_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel) ⇒ SlackClient



7
8
9
10
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 7

def initialize(channel)
  @client ||= Slack::Client.new(token: ENV['SLACK_TOKEN'])
  @channel = channel
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



5
6
7
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 5

def channel
  @channel
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 5

def client
  @client
end

Instance Method Details

#all_usersObject



12
13
14
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 12

def all_users
  @all_users ||= client.users_list['members']
end

#all_users_hashObject

ユーザを取得しやすくするためにユーザIDとユーザオブジェクトのハッシュテーブルを作る



17
18
19
20
21
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 17

def all_users_hash
  @all_users_hash ||= {}.tap do |h|
    all_users.each{|u| h[u['id']] = u}
  end
end

#channel_user_idsObject

チャンネル内の全ユーザid



24
25
26
27
28
29
30
31
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 24

def channel_user_ids
  unless @channel_user_ids&.dig(channel)
    @channel_user_ids = {} unless @channel_user_ids
    ids = private_channel? ? private_channel_user_ids : public_channel_user_ids
    @channel_user_ids[channel] = ids.sort
  end
  @channel_user_ids[channel]
end

#channels_infoObject



33
34
35
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 33

def channels_info
  @channels_info ||= client.channels_info(channel: channel)
end

#groups_listObject



47
48
49
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 47

def groups_list
  @groups_list ||= client.groups_list['groups']
end

#private_channel?Boolean



51
52
53
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 51

def private_channel?
  channels_info['error'] == "channel_not_found"
end

#private_channel_user_idsObject



41
42
43
44
45
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 41

def private_channel_user_ids
  private_channels = groups_list
  current_channel = private_channels.find{|c| c['id'] == channel}
  current_channel['members']
end

#public_channel_user_idsObject



37
38
39
# File 'lib/ruboty/slack_take_turns/slack_client.rb', line 37

def public_channel_user_ids
  channels_info['channel']['members']
end