class Ruboty::SlackTakeTurns::SlackClient

Attributes

channel[R]
client[R]

Public Class Methods

new(channel) click to toggle source
# File lib/ruboty/slack_take_turns/slack_client.rb, line 7
def initialize(channel)
  @client ||= Slack::Client.new(token: ENV['SLACK_TOKEN'])
  @channel = channel
  @conversations_members = {}
end

Public Instance Methods

all_users() click to toggle source
# File lib/ruboty/slack_take_turns/slack_client.rb, line 13
def all_users
  @all_users ||= client.users_list['members']
end
all_users_hash() click to toggle source

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

# File lib/ruboty/slack_take_turns/slack_client.rb, line 18
def all_users_hash
  @all_users_hash ||= {}.tap do |h|
    all_users.each{|u| h[u['id']] = u}
  end
end
channel_user_ids() click to toggle source

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

# File lib/ruboty/slack_take_turns/slack_client.rb, line 25
def channel_user_ids
  unless @channel_user_ids&.dig(channel)
    @channel_user_ids = {} unless @channel_user_ids
    ids = conversations_members(channel)
    # 退職してアカウント停止した人とボットは除く
    ids = ids.select{|id| user = find_user_by_user_id(id); !user['deleted'] && !user['is_bot']}
    @channel_user_ids[channel] = ids.sort
  end
  @channel_user_ids[channel]
end
conversations_members(channel) click to toggle source
# File lib/ruboty/slack_take_turns/slack_client.rb, line 36
def conversations_members(channel)
  members = @conversations_members[channel]
  @conversations_members[channel] = client.conversations_members(channel: channel)['members'] unless members
  @conversations_members[channel]
end
find_user_by_user_id(user_id) click to toggle source
# File lib/ruboty/slack_take_turns/slack_client.rb, line 42
def find_user_by_user_id(user_id)
  all_users_hash[user_id] || raise("user not found user_id: #{user_id}#")
end