Class: Slk::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/api/client.rb

Overview

Wrapper for Slack client.counts and auth.test API endpoints

Instance Method Summary collapse

Constructor Details

#initialize(api_client, workspace) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/slk/api/client.rb', line 7

def initialize(api_client, workspace)
  @api = api_client
  @workspace = workspace
end

Instance Method Details

#auth_testObject



16
17
18
# File 'lib/slk/api/client.rb', line 16

def auth_test
  @api.post(@workspace, 'auth.test')
end

#countsObject



12
13
14
# File 'lib/slk/api/client.rb', line 12

def counts
  @api.post(@workspace, 'client.counts')
end

#team_idObject



20
21
22
# File 'lib/slk/api/client.rb', line 20

def team_id
  @team_id ||= auth_test['team_id']
end

#total_unread_countObject



39
40
41
42
# File 'lib/slk/api/client.rb', line 39

def total_unread_count
  response = counts
  sum_mentions(response, 'channels') + sum_mentions(response, 'ims') + sum_mentions(response, 'mpims')
end

#unread_channelsObject



24
25
26
27
28
29
# File 'lib/slk/api/client.rb', line 24

def unread_channels
  response = counts
  channels = response['channels'] || []

  channels.select { |ch| (ch['mention_count'] || 0).positive? || ch['has_unreads'] }
end

#unread_dmsObject



31
32
33
34
35
36
37
# File 'lib/slk/api/client.rb', line 31

def unread_dms
  response = counts
  dms = response['ims'] || []
  mpims = response['mpims'] || []

  (dms + mpims).select { |dm| (dm['mention_count'] || 0).positive? || dm['has_unreads'] }
end