slacks 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0fe5955626325049c7ec170b8c67cb5b652d2d97
4
- data.tar.gz: b73672adfc7da3608a86453a6b6c7c6b5271a5c5
2
+ SHA256:
3
+ metadata.gz: 3042bf152b61c2caa8a3b4fe792b49822d46915712a8573f6bcafc86132ae2b8
4
+ data.tar.gz: 116df1499c672f6d72c23894c4b9abe0133cbe855766175488384509845c5ec0
5
5
  SHA512:
6
- metadata.gz: 44dc3585e769b303e2f82e57cc50b8d83e023c3a9e7a0cbeff276c10da8ddd67965f5f463059adc6151a35fb29f5cf3d5577a0810cfe5e2781c8090777f3315e
7
- data.tar.gz: 71a0cc131fe1fc997093f8d0da62c971bcc50241699a80ce2cabd2cb8b7c7e0b0d25eb00058b226373ab0a96307bc1abec6a30f6ec3824775adacefa1c85626c
6
+ metadata.gz: c1f5d964031547e6d9160d465a4b894f76aa7bbe3174c98dc7bb36964c52eddbb5a5d527c9bcfbd67f7aad93b5fe0869acc8ea0faa07fa97217f129bd537ad27
7
+ data.tar.gz: 03b84265b6fe59c1d2858fc12a8274fdb1301575b142f224ea6dd8d6f811c81b94dd9cab6918548e4c7249b57dd695b39bead9a5d0173af964edd334689b2a26
@@ -24,10 +24,8 @@ module Slacks
24
24
  @user_ids_dm_ids = {}
25
25
  @users_by_id = {}
26
26
  @user_id_by_name = {}
27
- @groups_by_id = {}
28
- @group_id_by_name = {}
29
- @channels_by_id = {}
30
- @channel_id_by_name = {}
27
+ @conversations_by_id = {}
28
+ @conversation_ids_by_name = {}
31
29
  end
32
30
 
33
31
 
@@ -42,7 +40,7 @@ module Slacks
42
40
  link_names: 1} # find and link channel names and user names
43
41
  params.merge!(attachments: MultiJson.dump(attachments)) if attachments.any?
44
42
  params.merge!(options.select { |key, _| SEND_MESSAGE_PARAMS.member?(key) })
45
- api("chat.postMessage", params)
43
+ api("chat.postMessage", **params)
46
44
  end
47
45
  alias :say :send_message
48
46
 
@@ -50,7 +48,7 @@ module Slacks
50
48
  params = {
51
49
  channel: to_channel_id(channel),
52
50
  timestamp: ts }
53
- api("reactions.get", params)
51
+ api("reactions.get", **params)
54
52
  end
55
53
 
56
54
  def update_message(ts, message, options={})
@@ -65,15 +63,15 @@ module Slacks
65
63
  params.merge!(attachments: MultiJson.dump(attachments)) if attachments.any?
66
64
  params.merge!(options.select { |key, _| [:username, :as_user, :parse, :link_names,
67
65
  :unfurl_links, :unfurl_media, :icon_url, :icon_emoji].member?(key) })
68
- api("chat.update", params)
66
+ api("chat.update", **params)
69
67
  end
70
68
 
71
69
  def add_reaction(emojis, message)
72
70
  Array(emojis).each do |emoji|
73
- api("reactions.add", {
71
+ api("reactions.add",
74
72
  name: emoji.gsub(/^:|:$/, ""),
75
73
  channel: message.channel.id,
76
- timestamp: message.timestamp })
74
+ timestamp: message.timestamp)
77
75
  end
78
76
  end
79
77
 
@@ -116,21 +114,16 @@ module Slacks
116
114
  # one, we'll skill it.
117
115
  next
118
116
 
119
- when EVENT_GROUP_JOINED
120
- group = data["channel"]
121
- @groups_by_id[group["id"]] = group
122
- @group_id_by_name[group["name"]] = group["id"]
117
+ when EVENT_GROUP_JOINED, EVENT_CHANNEL_CREATED
118
+ conversation = data["channel"]
119
+ @conversations_by_id[conversation["id"]] = conversation
120
+ @conversation_ids_by_name[conversation["name"]] = conversation["id"]
123
121
 
124
122
  when EVENT_USER_JOINED
125
123
  user = data["user"]
126
124
  @users_by_id[user["id"]] = user
127
125
  @user_id_by_name[user["name"]] = user["id"]
128
126
 
129
- when EVENT_CHANNEL_CREATED
130
- channel = data["channel"]
131
- @channels_by_id[channel["id"]] = channel
132
- @channel_id_by_name[channel["name"]] = channel["id"]
133
-
134
127
  when EVENT_MESSAGE
135
128
  # Don't respond to things that this bot said
136
129
  next if data["user"] == bot.id
@@ -157,10 +150,9 @@ module Slacks
157
150
 
158
151
 
159
152
  def channels
160
- channels = user_id_by_name.keys + group_id_by_name.keys + channel_id_by_name.keys
153
+ channels = user_id_by_name.keys + conversation_ids_by_name.keys
161
154
  if channels.empty?
162
- fetch_channels!
163
- fetch_groups!
155
+ fetch_conversations!
164
156
  fetch_users!
165
157
  end
166
158
  channels
@@ -183,13 +175,9 @@ module Slacks
183
175
  "id" => id,
184
176
  "is_im" => true,
185
177
  "name" => user.username }
186
- when /^G/
187
- Slacks::Channel.new(self, groups_by_id.fetch(id) do
188
- raise ArgumentError, "Unable to find a group with the ID #{id.inspect}"
189
- end)
190
178
  else
191
- Slacks::Channel.new(self, channels_by_id.fetch(id) do
192
- raise ArgumentError, "Unable to find a channel with the ID #{id.inspect}"
179
+ Slacks::Channel.new(self, conversations_by_id.fetch(id) do
180
+ raise ArgumentError, "Unable to find a conversation with the ID #{id.inspect}"
193
181
  end)
194
182
  end
195
183
  end
@@ -224,10 +212,8 @@ module Slacks
224
212
  attr_reader :user_ids_dm_ids,
225
213
  :users_by_id,
226
214
  :user_id_by_name,
227
- :groups_by_id,
228
- :group_id_by_name,
229
- :channels_by_id,
230
- :channel_id_by_name,
215
+ :conversations_by_id,
216
+ :conversation_ids_by_name,
231
217
  :websocket_url,
232
218
  :websocket
233
219
 
@@ -238,14 +224,8 @@ module Slacks
238
224
  @bot = BotUser.new(response.fetch("self"))
239
225
  @team = Team.new(response.fetch("team"))
240
226
 
241
- @channels_by_id = Hash[response.fetch("channels").map { |attrs| [attrs.fetch("id"), attrs] }]
242
- @channel_id_by_name = Hash[response.fetch("channels").map { |attrs| ["##{attrs.fetch("name")}", attrs.fetch("id")] }]
243
-
244
- @users_by_id = Hash[response.fetch("users").map { |attrs| [attrs.fetch("id"), attrs] }]
245
- @user_id_by_name = Hash[response.fetch("users").map { |attrs| ["@#{attrs.fetch("name")}", attrs.fetch("id")] }]
246
-
247
- @groups_by_id = Hash[response.fetch("groups").map { |attrs| [attrs.fetch("id"), attrs] }]
248
- @group_id_by_name = Hash[response.fetch("groups").map { |attrs| [attrs.fetch("name"), attrs.fetch("id")] }]
227
+ @conversations_by_id = Hash[response.fetch("channels").map { |attrs| [ attrs.fetch("id"), attrs ] }]
228
+ @conversation_ids_by_name = Hash[response.fetch("channels").map { |attrs| [ attrs["name"], attrs["id"] ] }]
249
229
  end
250
230
 
251
231
 
@@ -254,13 +234,9 @@ module Slacks
254
234
  return name.id if name.is_a?(Slacks::Channel)
255
235
  return name if name =~ /^[DGC]/ # this already looks like a channel id
256
236
  return get_dm_for_username(name) if name.start_with?("@")
257
- return to_group_id(name) unless name.start_with?("#")
258
237
 
259
- channel_id_by_name[name] || fetch_channels![name] || missing_channel!(name)
260
- end
261
-
262
- def to_group_id(name)
263
- group_id_by_name[name] || fetch_groups![name] || missing_group!(name)
238
+ name = name.gsub(/^#/, "") # Leading hashes are no longer a thing in the conversations API
239
+ conversation_ids_by_name[name] || fetch_conversations![name] || missing_conversation!(name)
264
240
  end
265
241
 
266
242
  def to_user_id(name)
@@ -273,39 +249,29 @@ module Slacks
273
249
 
274
250
  def get_dm_for_user_id(user_id)
275
251
  user_ids_dm_ids[user_id] ||= begin
276
- response = api("im.open", user: user_id)
252
+ response = api("conversations.open", user: user_id)
277
253
  response["channel"]["id"]
278
254
  end
279
255
  end
280
256
 
281
257
 
282
-
283
- def fetch_channels!
284
- response = api("channels.list")
285
- @channels_by_id = response["channels"].index_by { |attrs| attrs["id"] }
286
- @channel_id_by_name = Hash[response["channels"].map { |attrs| ["##{attrs["name"]}", attrs["id"]] }]
287
- end
288
-
289
- def fetch_groups!
290
- response = api("groups.list")
291
- @groups_by_id = response["groups"].index_by { |attrs| attrs["id"] }
292
- @group_id_by_name = Hash[response["groups"].map { |attrs| [attrs["name"], attrs["id"]] }]
258
+ def fetch_conversations!
259
+ conversations, ims = api("conversations.list")["channels"].partition { |attrs| attrs["is_channel"] || attrs["is_group"] }
260
+ user_ids_dm_ids.merge! Hash[ims.map { |attrs| attrs.values_at("user", "id") }]
261
+ @conversations_by_id = Hash[conversations.map { |attrs| [ attrs.fetch("id"), attrs ] }]
262
+ @conversation_ids_by_name = Hash[conversations.map { |attrs| [ attrs["name"], attrs["id"] ] }]
293
263
  end
294
264
 
295
265
  def fetch_users!
296
266
  response = api("users.list")
297
- @users_by_id = response["members"].index_by { |attrs| attrs["id"] }
267
+ @users_by_id = response["members"].each_with_object({}) { |attrs, hash| hash[attrs["id"]] = attrs }
298
268
  @user_id_by_name = Hash[response["members"].map { |attrs| ["@#{attrs["name"]}", attrs["id"]] }]
299
269
  end
300
270
 
301
271
 
302
272
 
303
- def missing_channel!(name)
304
- raise ArgumentError, "Couldn't find a channel named #{name}"
305
- end
306
-
307
- def missing_group!(name)
308
- raise ArgumentError, "Couldn't find a private group named #{name}"
273
+ def missing_conversation!(name)
274
+ raise ArgumentError, "Couldn't find a conversation named #{name}"
309
275
  end
310
276
 
311
277
  def missing_user!(name)
@@ -317,8 +283,7 @@ module Slacks
317
283
  def get_user_id_for_dm(dm)
318
284
  user_id = user_ids_dm_ids.key(dm)
319
285
  unless user_id
320
- response = api("im.list")
321
- user_ids_dm_ids.merge! Hash[response["ims"].map { |attrs| attrs.values_at("user", "id") }]
286
+ fetch_conversations!
322
287
  user_id = user_ids_dm_ids.key(dm)
323
288
  end
324
289
  raise ArgumentError, "Unable to find a user for the direct message ID #{dm.inspect}" unless user_id
@@ -327,8 +292,29 @@ module Slacks
327
292
 
328
293
 
329
294
 
330
- def api(command, params={})
331
- response = http.post(command, params.merge(token: token))
295
+ def api(command, page_limit: MAX_PAGES, **params)
296
+ params_with_token = params.merge(token: token)
297
+ response = api_post command, params_with_token
298
+ fetched_pages = 1
299
+ cursor = response.dig("response_metadata", "next_cursor")
300
+ while cursor && !cursor.empty? && fetched_pages < page_limit do
301
+ api_post(command, params_with_token.merge(cursor: cursor)).each do |key, value|
302
+ if value.is_a?(Array)
303
+ response[key].concat value
304
+ elsif value.is_a?(Hash)
305
+ response[key].merge! value
306
+ else
307
+ response[key] = value
308
+ end
309
+ end
310
+ fetched_pages += 1
311
+ cursor = response.dig("response_metadata", "next_cursor")
312
+ end
313
+ response
314
+ end
315
+
316
+ def api_post(command, params)
317
+ response = http.post(command, params)
332
318
  response = MultiJson.load(response.body)
333
319
  unless response["ok"]
334
320
  response["error"].split(/,\s*/).each do |error_code|
@@ -364,5 +350,7 @@ module Slacks
364
350
  reply_broadcast
365
351
  }.freeze
366
352
 
353
+ MAX_PAGES = 9001
354
+
367
355
  end
368
356
  end
@@ -1,3 +1,3 @@
1
1
  module Slacks
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/slacks.gemspec CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "faraday-raise-errors"
26
26
  spec.add_dependency "concurrent-ruby"
27
27
 
28
- spec.add_development_dependency "bundler", "~> 1.10"
29
- spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
30
  spec.add_development_dependency "minitest", "~> 5.0"
31
31
  spec.add_development_dependency "pry"
32
32
  spec.add_development_dependency "minitest-reporters"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-01 00:00:00.000000000 Z
11
+ date: 2021-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver
@@ -84,30 +84,30 @@ dependencies:
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '1.10'
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '1.10'
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '10.0'
103
+ version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '10.0'
110
+ version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: minitest
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -164,7 +164,7 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
- description:
167
+ description:
168
168
  email:
169
169
  - bob.lailfamily@gmail.com
170
170
  executables: []
@@ -196,7 +196,7 @@ homepage: https://github.com/houston/slacks
196
196
  licenses:
197
197
  - MIT
198
198
  metadata: {}
199
- post_install_message:
199
+ post_install_message:
200
200
  rdoc_options: []
201
201
  require_paths:
202
202
  - lib
@@ -211,9 +211,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  - !ruby/object:Gem::Version
212
212
  version: '0'
213
213
  requirements: []
214
- rubyforge_project:
215
- rubygems_version: 2.5.1
216
- signing_key:
214
+ rubygems_version: 3.1.2
215
+ signing_key:
217
216
  specification_version: 4
218
217
  summary: A library for communicating via Slack
219
218
  test_files: []