slack-ruby-client 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -1
  3. data/.rubocop_todo.yml +23 -6
  4. data/.travis.yml +3 -0
  5. data/CHANGELOG.md +11 -0
  6. data/Gemfile +2 -0
  7. data/README.md +89 -1
  8. data/RELEASING.md +3 -1
  9. data/UPGRADING.md +26 -0
  10. data/bin/commands.rb +20 -0
  11. data/bin/commands/api.rb +14 -0
  12. data/bin/commands/auth.rb +12 -0
  13. data/bin/commands/channels.rb +140 -0
  14. data/bin/commands/chat.rb +47 -0
  15. data/bin/commands/emoji.rb +12 -0
  16. data/bin/commands/files.rb +61 -0
  17. data/bin/commands/groups.rb +158 -0
  18. data/bin/commands/im.rb +53 -0
  19. data/bin/commands/mpim.rb +53 -0
  20. data/bin/commands/oauth.rb +16 -0
  21. data/bin/commands/pins.rb +37 -0
  22. data/bin/commands/reactions.rb +53 -0
  23. data/bin/commands/rtm.rb +15 -0
  24. data/bin/commands/search.rb +40 -0
  25. data/bin/commands/stars.rb +37 -0
  26. data/bin/commands/team.rb +32 -0
  27. data/bin/commands/usergroups.rb +73 -0
  28. data/bin/commands/users.rb +48 -0
  29. data/bin/slack +50 -0
  30. data/examples/hi_real_time/Gemfile +2 -0
  31. data/examples/hi_real_time_and_web/Gemfile +2 -0
  32. data/examples/hi_real_time_async/Gemfile +5 -0
  33. data/examples/hi_real_time_async/hi.rb +29 -0
  34. data/lib/slack-ruby-client.rb +2 -2
  35. data/lib/slack/real_time/client.rb +72 -30
  36. data/lib/slack/real_time/concurrency.rb +8 -0
  37. data/lib/slack/real_time/concurrency/celluloid.rb +92 -0
  38. data/lib/slack/real_time/concurrency/eventmachine.rb +39 -0
  39. data/lib/slack/real_time/config.rb +23 -1
  40. data/lib/slack/real_time/socket.rb +50 -12
  41. data/lib/slack/version.rb +1 -1
  42. data/lib/slack/web/api/endpoints.rb +2 -0
  43. data/lib/slack/web/api/endpoints/groups.rb +1 -1
  44. data/lib/slack/web/api/endpoints/team.rb +1 -1
  45. data/lib/slack/web/api/endpoints/usergroups.rb +113 -0
  46. data/lib/slack/web/api/error.rb +6 -0
  47. data/lib/slack/web/api/schema/group.json +14 -0
  48. data/lib/slack/web/api/tasks/generate.rake +19 -3
  49. data/lib/slack/web/api/templates/command.erb +34 -0
  50. data/lib/slack/web/api/templates/commands.erb +5 -0
  51. data/lib/slack/web/config.rb +2 -0
  52. data/lib/slack/web/faraday/connection.rb +3 -2
  53. data/lib/slack/web/faraday/response/raise_error.rb +2 -1
  54. data/slack-ruby-client.gemspec +4 -2
  55. data/spec/fixtures/slack/web/429_error.yml +83 -0
  56. data/spec/fixtures/slack/web/rtm_start.yml +1 -1
  57. data/spec/fixtures/slack/web/users_list.yml +72 -0
  58. data/spec/integration/integration_spec.rb +88 -0
  59. data/spec/slack/real_time/client_spec.rb +8 -5
  60. data/spec/slack/real_time/concurrency/celluloid_spec.rb +58 -0
  61. data/spec/slack/real_time/concurrency/eventmachine_spec.rb +49 -0
  62. data/spec/slack/slack_spec.rb +52 -0
  63. data/spec/slack/web/api/endpoints/auth_spec.rb +6 -1
  64. data/spec/slack/web/api/endpoints/users_spec.rb +13 -0
  65. data/spec/slack/web/api/error_spec.rb +14 -0
  66. data/spec/slack/web/client_spec.rb +16 -0
  67. data/spec/support/real_time/concurrency/mock.rb +31 -0
  68. data/spec/support/real_time/connected_client.rb +5 -2
  69. metadata +55 -8
  70. data/spec/slack/real_time/socket_spec.rb +0 -46
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ RSpec.describe Slack::RealTime::Concurrency::Celluloid::Socket do
5
+ context 'with url' do
6
+ let(:url) { 'wss://echo.websocket.org/websocket/xyz' }
7
+ subject(:socket) { described_class.new(url, ping: 42) }
8
+ let(:driver) { WebSocket::Driver::Client }
9
+ let(:ws) { double(driver) }
10
+
11
+ describe '#initialize' do
12
+ it 'sets url' do
13
+ expect(socket.url).to eq url
14
+ end
15
+ end
16
+
17
+ describe '#connect!' do
18
+ before do
19
+ allow(ws).to receive(:on).with(:close)
20
+ end
21
+
22
+ xit 'connects' do
23
+ expect(socket.driver).to receive(:start)
24
+
25
+ socket.connect!
26
+ end
27
+
28
+ xit 'pings every 30s' do
29
+ expect(driver).to receive(:client).with(socket).and_return(ws)
30
+ socket.connect!
31
+ end
32
+ end
33
+
34
+ describe '#disconnect!' do
35
+ it 'closes and nils the websocket' do
36
+ socket.instance_variable_set('@driver', ws)
37
+ expect(ws).to receive(:close)
38
+ socket.disconnect!
39
+ end
40
+ end
41
+
42
+ describe 'send_data' do
43
+ let(:driver) { socket.driver }
44
+ before do
45
+ allow(driver).to receive(:start)
46
+ allow(subject).to receive(:run_loop)
47
+ socket.connect!
48
+ end
49
+
50
+ it 'sends data' do
51
+ expect(driver).to receive(:text).with('data')
52
+ subject.send_data('data')
53
+ end
54
+ end
55
+ end
56
+ end
57
+ rescue LoadError
58
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ RSpec.describe Slack::RealTime::Concurrency::Eventmachine::Socket do
5
+ context 'with url' do
6
+ let(:url) { 'wss://ms174.slack-msgs.com/websocket/xyz' }
7
+ let(:socket) { described_class.new(url, ping: 42) }
8
+ let(:ws) { double(Faye::WebSocket::Client) }
9
+ describe '#initialize' do
10
+ it 'sets url' do
11
+ expect(socket.url).to eq url
12
+ end
13
+ end
14
+ describe '#connect!' do
15
+ before do
16
+ allow(ws).to receive(:on).with(:close)
17
+ end
18
+ it 'connects' do
19
+ allow(Faye::WebSocket::Client).to receive(:new).and_return(ws)
20
+ socket.connect!
21
+ expect(socket.instance_variable_get('@driver')).to eq ws
22
+ end
23
+ it 'pings every 30s' do
24
+ expect(Faye::WebSocket::Client).to receive(:new).with(url, nil, ping: 42).and_return(ws)
25
+ socket.connect!
26
+ end
27
+ end
28
+ describe '#disconnect!' do
29
+ it 'closes and nils the websocket' do
30
+ socket.instance_variable_set('@driver', ws)
31
+ expect(ws).to receive(:close)
32
+ socket.disconnect!
33
+ end
34
+ end
35
+ describe 'send_data' do
36
+ before do
37
+ allow(Faye::WebSocket::Client).to receive(:new).and_return(ws)
38
+ allow(ws).to receive(:on)
39
+ socket.connect!
40
+ end
41
+ it 'sends data' do
42
+ expect(ws).to receive(:send).with('data')
43
+ socket.send_data('data')
44
+ end
45
+ end
46
+ end
47
+ end
48
+ rescue LoadError
49
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slack do
4
+ let(:slack) { File.expand_path(File.join(__FILE__, '../../../bin/slack')) }
5
+ describe '#help' do
6
+ it 'displays help' do
7
+ help = `"#{slack}" help`
8
+ expect(help).to include 'slack - Slack client.'
9
+ end
10
+ end
11
+ context 'globals' do
12
+ it 'enables request and response logging with -d' do
13
+ output = `"#{slack}" --vcr-cassette-name=web/auth_test_success --slack-api-token=token -d auth test 2>&1`
14
+ expect(output).to include 'post https://slack.com/api/auth.test'
15
+ expect(output).to include 'Status: 200'
16
+ end
17
+ it 'requires --slack-api-token' do
18
+ err = `"#{slack}" auth test 2>&1`
19
+ expect(err).to start_with 'error: parse error: Set Slack API token via --slack-api-token or SLACK_API_TOKEN.'
20
+ end
21
+ end
22
+ describe '#auth' do
23
+ context 'bad auth' do
24
+ it 'fails with an exception' do
25
+ err = `"#{slack}" --vcr-cassette-name=web/auth_test_error --slack-api-token=token auth test 2>&1`
26
+ expect(err).to eq "error: not_authed\n"
27
+ end
28
+ end
29
+ context 'good auth' do
30
+ it 'succeeds' do
31
+ json = JSON.parse `"#{slack}" --vcr-cassette-name=web/auth_test_success --slack-api-token=token auth test 2>&1`
32
+ expect(json).to eq(
33
+ 'ok' => true,
34
+ 'url' => 'https://rubybot.slack.com/',
35
+ 'team' => 'team_name',
36
+ 'user' => 'user_name',
37
+ 'team_id' => 'TDEADBEEF',
38
+ 'user_id' => 'UBAADFOOD'
39
+ )
40
+ expect(json['ok']).to be true
41
+ end
42
+ end
43
+ end
44
+ describe '#users' do
45
+ it 'list' do
46
+ json = JSON.parse `"#{slack}" --vcr-cassette-name=web/users_list --slack-api-token=token users list --presence=true 2>&1`
47
+ expect(json['ok']).to be true
48
+ expect(json['members'].size).to eq 9
49
+ expect(json['members'].first['presence']).to eq 'away'
50
+ end
51
+ end
52
+ end
@@ -4,7 +4,7 @@ RSpec.describe Slack::Web::Api::Endpoints::Auth do
4
4
  let(:client) { Slack::Web::Client.new }
5
5
  context 'without auth', vcr: { cassette_name: 'web/auth_test_error' } do
6
6
  it 'fails with an exception' do
7
- expect { client.auth_test }.to raise_error Slack::Web::Api::Error
7
+ expect { client.auth_test }.to raise_error Slack::Web::Api::Error, 'not_authed'
8
8
  end
9
9
  end
10
10
  context 'with auth', vcr: { cassette_name: 'web/auth_test_success' } do
@@ -12,4 +12,9 @@ RSpec.describe Slack::Web::Api::Endpoints::Auth do
12
12
  expect { client.auth_test }.to_not raise_error
13
13
  end
14
14
  end
15
+ context '429 error', vcr: { cassette_name: 'web/429_error' } do
16
+ it 'fails with an exception' do
17
+ expect { client.auth_test }.to raise_error Faraday::ClientError
18
+ end
19
+ end
15
20
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Slack::Web::Api::Endpoints::Users do
4
+ let(:client) { Slack::Web::Client.new }
5
+ context 'users' do
6
+ it 'list', vcr: { cassette_name: 'web/users_list' } do
7
+ json = client.users_list(presence: true)
8
+ expect(json['ok']).to be true
9
+ expect(json['members'].size).to eq 9
10
+ expect(json['members'].first['presence']).to eq 'away'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Slack::Web::Api::Error do
4
+ let(:client) { Slack::Web::Client.new }
5
+ it 'provides access to the response object', vcr: { cassette_name: 'web/auth_test_error' } do
6
+ begin
7
+ client.auth_test
8
+ fail 'Expected to receive Slack::Web::Api::Error.'
9
+ rescue Slack::Web::Api::Error => e
10
+ expect(e.response).to_not be_nil
11
+ expect(e.response.status).to eq 200
12
+ end
13
+ end
14
+ end
@@ -111,5 +111,21 @@ RSpec.describe Slack::Web::Client do
111
111
  end
112
112
  end
113
113
  end
114
+ context 'logger option' do
115
+ let(:logger) { Logger.new(STDOUT) }
116
+ before do
117
+ Slack::Web::Client.configure do |config|
118
+ config.logger = logger
119
+ end
120
+ end
121
+ describe '#initialize' do
122
+ it 'sets logger' do
123
+ expect(client.logger).to eq logger
124
+ end
125
+ it 'creates a connection with a logger' do
126
+ expect(client.send(:connection).builder.handlers).to include ::Faraday::Response::Logger
127
+ end
128
+ end
129
+ end
114
130
  end
115
131
  end
@@ -0,0 +1,31 @@
1
+ module Slack
2
+ module RealTime
3
+ module Concurrency
4
+ module Mock
5
+ class WebSocket
6
+ end
7
+
8
+ class Socket < ::Slack::RealTime::Socket
9
+ def self.close
10
+ end
11
+
12
+ def start_async
13
+ reactor = Thread.new {}
14
+ yield self if block_given?
15
+ reactor
16
+ end
17
+
18
+ def send_data(message)
19
+ driver.send(message)
20
+ end
21
+
22
+ protected
23
+
24
+ def connect
25
+ @driver = WebSocket.new(url, nil, options)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,11 +1,14 @@
1
1
  RSpec.shared_context 'connected client' do
2
2
  let(:client) { Slack::RealTime::Client.new }
3
- let(:ws) { double(Faye::WebSocket::Client) }
3
+ let(:ws) { double(Slack::RealTime::Concurrency::Mock::WebSocket) }
4
4
  let(:url) { 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' }
5
5
  let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
6
6
  before do
7
- allow(EM).to receive(:run).and_yield
7
+ Slack::RealTime.configure do |config|
8
+ config.concurrency = Slack::RealTime::Concurrency::Mock
9
+ end
8
10
  allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30).and_return(socket)
11
+ allow(socket).to receive(:start_sync).and_yield
9
12
  allow(socket).to receive(:connect!).and_yield(ws)
10
13
  allow(ws).to receive(:on)
11
14
  client.start!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-08 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: faye-websocket
42
+ name: json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: eventmachine
56
+ name: websocket-driver
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: json
70
+ name: gli
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -180,7 +180,8 @@ dependencies:
180
180
  version: 0.35.0
181
181
  description:
182
182
  email: dblock@dblock.org
183
- executables: []
183
+ executables:
184
+ - slack
184
185
  extensions: []
185
186
  extra_rdoc_files: []
186
187
  files:
@@ -197,12 +198,35 @@ files:
197
198
  - README.md
198
199
  - RELEASING.md
199
200
  - Rakefile
201
+ - UPGRADING.md
202
+ - bin/commands.rb
203
+ - bin/commands/api.rb
204
+ - bin/commands/auth.rb
205
+ - bin/commands/channels.rb
206
+ - bin/commands/chat.rb
207
+ - bin/commands/emoji.rb
208
+ - bin/commands/files.rb
209
+ - bin/commands/groups.rb
210
+ - bin/commands/im.rb
211
+ - bin/commands/mpim.rb
212
+ - bin/commands/oauth.rb
213
+ - bin/commands/pins.rb
214
+ - bin/commands/reactions.rb
215
+ - bin/commands/rtm.rb
216
+ - bin/commands/search.rb
217
+ - bin/commands/stars.rb
218
+ - bin/commands/team.rb
219
+ - bin/commands/usergroups.rb
220
+ - bin/commands/users.rb
221
+ - bin/slack
200
222
  - examples/hi_real_time/Gemfile
201
223
  - examples/hi_real_time/hi.gif
202
224
  - examples/hi_real_time/hi.rb
203
225
  - examples/hi_real_time_and_web/Gemfile
204
226
  - examples/hi_real_time_and_web/hi.gif
205
227
  - examples/hi_real_time_and_web/hi.rb
228
+ - examples/hi_real_time_async/Gemfile
229
+ - examples/hi_real_time_async/hi.rb
206
230
  - examples/hi_web/Gemfile
207
231
  - examples/hi_web/hi.gif
208
232
  - examples/hi_web/hi.rb
@@ -216,6 +240,9 @@ files:
216
240
  - lib/slack/real_time/api/ping.rb
217
241
  - lib/slack/real_time/api/typing.rb
218
242
  - lib/slack/real_time/client.rb
243
+ - lib/slack/real_time/concurrency.rb
244
+ - lib/slack/real_time/concurrency/celluloid.rb
245
+ - lib/slack/real_time/concurrency/eventmachine.rb
219
246
  - lib/slack/real_time/config.rb
220
247
  - lib/slack/real_time/socket.rb
221
248
  - lib/slack/version.rb
@@ -237,12 +264,16 @@ files:
237
264
  - lib/slack/web/api/endpoints/search.rb
238
265
  - lib/slack/web/api/endpoints/stars.rb
239
266
  - lib/slack/web/api/endpoints/team.rb
267
+ - lib/slack/web/api/endpoints/usergroups.rb
240
268
  - lib/slack/web/api/endpoints/users.rb
241
269
  - lib/slack/web/api/error.rb
242
270
  - lib/slack/web/api/patches/chat.1.text-attachments-required.patch
243
271
  - lib/slack/web/api/patches/chat.2.attachments-json.patch
272
+ - lib/slack/web/api/schema/group.json
244
273
  - lib/slack/web/api/schema/method.json
245
274
  - lib/slack/web/api/tasks/generate.rake
275
+ - lib/slack/web/api/templates/command.erb
276
+ - lib/slack/web/api/templates/commands.erb
246
277
  - lib/slack/web/api/templates/endpoints.erb
247
278
  - lib/slack/web/api/templates/method.erb
248
279
  - lib/slack/web/client.rb
@@ -254,20 +285,28 @@ files:
254
285
  - screenshots/register-bot.png
255
286
  - slack-ruby-client.gemspec
256
287
  - slack.png
288
+ - spec/fixtures/slack/web/429_error.yml
257
289
  - spec/fixtures/slack/web/auth_test_error.yml
258
290
  - spec/fixtures/slack/web/auth_test_success.yml
259
291
  - spec/fixtures/slack/web/rtm_start.yml
292
+ - spec/fixtures/slack/web/users_list.yml
293
+ - spec/integration/integration_spec.rb
260
294
  - spec/slack/config_spec.rb
261
295
  - spec/slack/real_time/api/message_spec.rb
262
296
  - spec/slack/real_time/api/ping_spec.rb
263
297
  - spec/slack/real_time/api/typing_spec.rb
264
298
  - spec/slack/real_time/client_spec.rb
265
- - spec/slack/real_time/socket_spec.rb
299
+ - spec/slack/real_time/concurrency/celluloid_spec.rb
300
+ - spec/slack/real_time/concurrency/eventmachine_spec.rb
301
+ - spec/slack/slack_spec.rb
266
302
  - spec/slack/version_spec.rb
267
303
  - spec/slack/web/api/endpoints/auth_spec.rb
268
304
  - spec/slack/web/api/endpoints/chat_spec.rb
305
+ - spec/slack/web/api/endpoints/users_spec.rb
306
+ - spec/slack/web/api/error_spec.rb
269
307
  - spec/slack/web/client_spec.rb
270
308
  - spec/spec_helper.rb
309
+ - spec/support/real_time/concurrency/mock.rb
271
310
  - spec/support/real_time/connected_client.rb
272
311
  - spec/support/token.rb
273
312
  - spec/support/vcr.rb
@@ -296,20 +335,28 @@ signing_key:
296
335
  specification_version: 4
297
336
  summary: Slack Web and RealTime API client.
298
337
  test_files:
338
+ - spec/fixtures/slack/web/429_error.yml
299
339
  - spec/fixtures/slack/web/auth_test_error.yml
300
340
  - spec/fixtures/slack/web/auth_test_success.yml
301
341
  - spec/fixtures/slack/web/rtm_start.yml
342
+ - spec/fixtures/slack/web/users_list.yml
343
+ - spec/integration/integration_spec.rb
302
344
  - spec/slack/config_spec.rb
303
345
  - spec/slack/real_time/api/message_spec.rb
304
346
  - spec/slack/real_time/api/ping_spec.rb
305
347
  - spec/slack/real_time/api/typing_spec.rb
306
348
  - spec/slack/real_time/client_spec.rb
307
- - spec/slack/real_time/socket_spec.rb
349
+ - spec/slack/real_time/concurrency/celluloid_spec.rb
350
+ - spec/slack/real_time/concurrency/eventmachine_spec.rb
351
+ - spec/slack/slack_spec.rb
308
352
  - spec/slack/version_spec.rb
309
353
  - spec/slack/web/api/endpoints/auth_spec.rb
310
354
  - spec/slack/web/api/endpoints/chat_spec.rb
355
+ - spec/slack/web/api/endpoints/users_spec.rb
356
+ - spec/slack/web/api/error_spec.rb
311
357
  - spec/slack/web/client_spec.rb
312
358
  - spec/spec_helper.rb
359
+ - spec/support/real_time/concurrency/mock.rb
313
360
  - spec/support/real_time/connected_client.rb
314
361
  - spec/support/token.rb
315
362
  - spec/support/vcr.rb