Class: Slack::Client
- Inherits:
-
Object
- Object
- Slack::Client
- Defined in:
- lib/slack/client.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #channels ⇒ Object
- #filter_message(text) ⇒ Object
- #get(api, options = {}) ⇒ Object
- #groups ⇒ Object
- #ims ⇒ Object
-
#initialize(token) ⇒ Client
constructor
A new instance of Client.
- #messages(name, type, options) ⇒ Object
- #mpim ⇒ Object
- #user_by_id(id) ⇒ Object
- #user_by_name(name) ⇒ Object
- #users ⇒ Object
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
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/slack/client.rb', line 3 def end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
3 4 5 |
# File 'lib/slack/client.rb', line 3 def token @token end |
Instance Method Details
#channels ⇒ Object
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 (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, = {}) RestClient.get("https://slack.com/api/#{api}", { params: .merge(token: token) }) end |
#groups ⇒ Object
57 58 59 |
# File 'lib/slack/client.rb', line 57 def groups @groups ||= JSON.parse(get('groups.list', { exclude_archived: 1 }))['groups'] end |
#ims ⇒ Object
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 (name, type, ) 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 = JSON.parse(get('channels.history', .merge(channel: channel['id'])))['messages'] .select { || ['type'] == 'message' && ['subtype'].nil? }.map do || [ Time.at(['ts'].to_i), channel['id'], channel['name'], ['user'], user_by_id(['user'])['name'], (['text']) ] end.reverse end |
#mpim ⇒ Object
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 |
#users ⇒ Object
49 50 51 |
# File 'lib/slack/client.rb', line 49 def users @users ||= JSON.parse(get('users.list', { presence: 0 }))['members'] end |