Class: Slack::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/slack/client.rb', line 5

def initialize(token)
  @token = token
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/slack/client.rb', line 3

def options
  @options
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/slack/client.rb', line 3

def token
  @token
end

Instance Method Details

#channelsObject



53
54
55
# File 'lib/slack/client.rb', line 53

def channels
  @channels ||= JSON.parse(get('channels.list', { exclude_archived: 1 }))['channels']
end

#filter_message(text) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slack/client.rb', line 29

def filter_message(text)
  text = text.dup
  scan = text.scan(/\<@[0-9A-Z]+\>/)
  return text unless scan

  scan.each do |pattern|
    user_id = pattern.match(/<@(?<user_id>.*)>/)[:user_id]
    text.gsub!(/#{pattern}/, "@#{user_by_id(user_id)['name']}")
  end
  text
end

#get(api, options = {}) ⇒ Object



69
70
71
# File 'lib/slack/client.rb', line 69

def get(api, options = {})
  RestClient.get("https://slack.com/api/#{api}", { params: options.merge(token: token) })
end

#groupsObject



57
58
59
# File 'lib/slack/client.rb', line 57

def groups
  @groups ||= JSON.parse(get('groups.list', { exclude_archived: 1 }))['groups']
end

#imsObject



61
62
63
# File 'lib/slack/client.rb', line 61

def ims
  @ims ||= JSON.parse(get('im.list'))['ims']
end

#messages(name, type, options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/slack/client.rb', line 9

def messages(name, type, options)
  channel = case type
  when 'channel' then channels.find { |item| item['name'] == name }
  when 'group' then groups.find { |item| item['name'] == name }
  when 'direct' then im.find { |item| item['user'] == name }
  when 'multi_direct' then mpim.find { |item| item['name'] == name }
  end
  messages = JSON.parse(get('channels.history', options.merge(channel: channel['id'])))['messages']
  messages.select { |message| message['type'] == 'message' && message['subtype'].nil? }.map do |message|
    [
      Time.at(message['ts'].to_i),
      channel['id'],
      channel['name'],
      message['user'],
      user_by_id(message['user'])['name'],
      filter_message(message['text'])
    ]
  end.reverse
end

#mpimObject



65
66
67
# File 'lib/slack/client.rb', line 65

def mpim
  @mpims ||= JSON.parse(get('mpim.list'))['groups']
end

#user_by_id(id) ⇒ Object



41
42
43
# File 'lib/slack/client.rb', line 41

def user_by_id(id)
  users.find { |user| user['id'] == id }
end

#user_by_name(name) ⇒ Object



45
46
47
# File 'lib/slack/client.rb', line 45

def user_by_name(name)
  users.find { |user| user['name'] == name }
end

#usersObject



49
50
51
# File 'lib/slack/client.rb', line 49

def users
  @users ||= JSON.parse(get('users.list', { presence: 0 }))['members']
end