class Repp::HeartfulSlack::SlackService

Attributes

web_client[R]

Public Class Methods

new(web_client) click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 8
def initialize(web_client)
  @web_client = web_client
  refresh
end

Public Instance Methods

find_channel(uid) click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 47
def find_channel(uid)
  return @channel_caches[uid] if @channel_caches.key?(uid)

  resp = web_client.conversations_info(channel: uid)
  return @channel_caches[uid] = resp.channel if resp.ok

  resp = web_client.groups_info(channel: uid)
  return @channel_caches[uid] = resp.group if resp.ok

  @channel_caches[uid] = nil
end
find_user(uid) click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 29
def find_user(uid)
  return @user_caches[uid] if @user_caches.key?(uid)

  resp = web_client.users_info(user: uid)
  @user_caches[uid] = resp.ok ? resp.user : nil
end
post(text:, channel:, attachments: [], as_user: true, name: nil, emoji: nil) click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 59
def post(text:, channel:, attachments: [], as_user: true, name: nil, emoji: nil)
  web_client.chat_postMessage({
    text: text,
    channel: channel,
    as_user: as_user,
    username: name,
    icon_emoji: ":#{emoji}:",
    attachments: attachments
  }.delete_if { |_, v| v.nil? })
end
refresh() click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 13
def refresh
  refresh_users_cache
  refresh_channel_caches
end
refresh_channel_caches() click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 36
def refresh_channel_caches
  @channel_caches = {}

  resp = web_client.channels_list
  return unless resp.ok

  resp.channels.each do |channel|
    @channel_caches[channel.id] = channel
  end
end
refresh_users_cache() click to toggle source
# File lib/repp/heartful_slack/slack_service.rb, line 18
def refresh_users_cache
  @user_caches = {}

  resp = web_client.users_list
  return unless resp.ok

  resp.members.each do |member|
    @user_caches[member.id] = member
  end
end