slack-ruby-client 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +19 -17
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +8 -0
  5. data/README.md +24 -4
  6. data/bin/commands/chat.rb +10 -9
  7. data/bin/commands/files.rb +19 -1
  8. data/examples/hi_real_time/hi.rb +2 -0
  9. data/lib/slack-ruby-client.rb +4 -2
  10. data/lib/slack/config.rb +4 -0
  11. data/lib/slack/logger.rb +13 -0
  12. data/lib/slack/messages/message.rb +6 -0
  13. data/lib/slack/real_time/api/schema/event.json +1 -0
  14. data/lib/slack/real_time/api/templates/event_handler.erb +4 -18
  15. data/lib/slack/real_time/client.rb +29 -5
  16. data/lib/slack/real_time/concurrency/celluloid.rb +6 -0
  17. data/lib/slack/real_time/concurrency/eventmachine.rb +22 -1
  18. data/lib/slack/real_time/config.rb +5 -1
  19. data/lib/slack/real_time/socket.rb +5 -0
  20. data/lib/slack/real_time/stores.rb +10 -0
  21. data/lib/slack/real_time/stores/base.rb +34 -0
  22. data/lib/slack/real_time/stores/starter.rb +361 -0
  23. data/lib/slack/real_time/stores/store.rb +479 -0
  24. data/lib/slack/version.rb +1 -1
  25. data/lib/slack/web/api/endpoints/api.rb +1 -1
  26. data/lib/slack/web/api/endpoints/auth.rb +1 -1
  27. data/lib/slack/web/api/endpoints/channels.rb +14 -14
  28. data/lib/slack/web/api/endpoints/chat.rb +15 -13
  29. data/lib/slack/web/api/endpoints/dnd.rb +5 -5
  30. data/lib/slack/web/api/endpoints/emoji.rb +1 -1
  31. data/lib/slack/web/api/endpoints/files.rb +30 -6
  32. data/lib/slack/web/api/endpoints/groups.rb +16 -16
  33. data/lib/slack/web/api/endpoints/im.rb +5 -5
  34. data/lib/slack/web/api/endpoints/mpim.rb +5 -5
  35. data/lib/slack/web/api/endpoints/oauth.rb +1 -1
  36. data/lib/slack/web/api/endpoints/pins.rb +3 -3
  37. data/lib/slack/web/api/endpoints/reactions.rb +4 -4
  38. data/lib/slack/web/api/endpoints/rtm.rb +1 -1
  39. data/lib/slack/web/api/endpoints/search.rb +3 -3
  40. data/lib/slack/web/api/endpoints/stars.rb +3 -3
  41. data/lib/slack/web/api/endpoints/team.rb +3 -3
  42. data/lib/slack/web/api/endpoints/usergroups.rb +6 -6
  43. data/lib/slack/web/api/endpoints/users.rb +5 -5
  44. data/lib/slack/web/api/patches/chat.1.text-attachments-required.patch +1 -1
  45. data/lib/slack/web/api/templates/method.erb +1 -1
  46. data/lib/slack/web/client.rb +1 -0
  47. data/lib/tasks/real_time.rake +27 -21
  48. data/slack-ruby-client.gemspec +1 -0
  49. data/spec/integration/integration_spec.rb +13 -1
  50. data/spec/slack/real_time/client_spec.rb +40 -5
  51. data/spec/slack/real_time/concurrency/celluloid_spec.rb +2 -1
  52. data/spec/slack/real_time/concurrency/eventmachine_spec.rb +3 -2
  53. data/spec/slack/real_time/event_handlers/event_handlers_spec.rb +15 -0
  54. data/spec/slack/real_time/event_handlers/team_spec.rb +54 -52
  55. data/spec/slack/web/api/endpoints/files_spec.rb +10 -0
  56. data/spec/slack/web/client_spec.rb +1 -1
  57. data/spec/support/real_time/connected_client.rb +3 -3
  58. metadata +23 -71
  59. data/lib/slack/real_time/api/templates/event_handlers.erb +0 -5
  60. data/lib/slack/real_time/event_handlers.rb +0 -66
  61. data/lib/slack/real_time/event_handlers/accounts_changed.rb +0 -18
  62. data/lib/slack/real_time/event_handlers/bot_added.rb +0 -16
  63. data/lib/slack/real_time/event_handlers/bot_changed.rb +0 -17
  64. data/lib/slack/real_time/event_handlers/channel_archive.rb +0 -17
  65. data/lib/slack/real_time/event_handlers/channel_created.rb +0 -17
  66. data/lib/slack/real_time/event_handlers/channel_deleted.rb +0 -16
  67. data/lib/slack/real_time/event_handlers/channel_history_changed.rb +0 -18
  68. data/lib/slack/real_time/event_handlers/channel_joined.rb +0 -22
  69. data/lib/slack/real_time/event_handlers/channel_left.rb +0 -17
  70. data/lib/slack/real_time/event_handlers/channel_marked.rb +0 -18
  71. data/lib/slack/real_time/event_handlers/channel_rename.rb +0 -17
  72. data/lib/slack/real_time/event_handlers/channel_unarchive.rb +0 -17
  73. data/lib/slack/real_time/event_handlers/commands_changed.rb +0 -18
  74. data/lib/slack/real_time/event_handlers/dnd_updated.rb +0 -18
  75. data/lib/slack/real_time/event_handlers/dnd_updated_user.rb +0 -18
  76. data/lib/slack/real_time/event_handlers/email_domain_changed.rb +0 -16
  77. data/lib/slack/real_time/event_handlers/emoji_changed.rb +0 -18
  78. data/lib/slack/real_time/event_handlers/file_change.rb +0 -18
  79. data/lib/slack/real_time/event_handlers/file_comment_added.rb +0 -18
  80. data/lib/slack/real_time/event_handlers/file_comment_deleted.rb +0 -18
  81. data/lib/slack/real_time/event_handlers/file_comment_edited.rb +0 -18
  82. data/lib/slack/real_time/event_handlers/file_created.rb +0 -18
  83. data/lib/slack/real_time/event_handlers/file_deleted.rb +0 -18
  84. data/lib/slack/real_time/event_handlers/file_private.rb +0 -18
  85. data/lib/slack/real_time/event_handlers/file_public.rb +0 -18
  86. data/lib/slack/real_time/event_handlers/file_shared.rb +0 -18
  87. data/lib/slack/real_time/event_handlers/file_unshared.rb +0 -18
  88. data/lib/slack/real_time/event_handlers/group_archive.rb +0 -17
  89. data/lib/slack/real_time/event_handlers/group_close.rb +0 -16
  90. data/lib/slack/real_time/event_handlers/group_history_changed.rb +0 -18
  91. data/lib/slack/real_time/event_handlers/group_joined.rb +0 -16
  92. data/lib/slack/real_time/event_handlers/group_left.rb +0 -17
  93. data/lib/slack/real_time/event_handlers/group_marked.rb +0 -18
  94. data/lib/slack/real_time/event_handlers/group_open.rb +0 -16
  95. data/lib/slack/real_time/event_handlers/group_rename.rb +0 -17
  96. data/lib/slack/real_time/event_handlers/group_unarchive.rb +0 -17
  97. data/lib/slack/real_time/event_handlers/im_close.rb +0 -16
  98. data/lib/slack/real_time/event_handlers/im_created.rb +0 -16
  99. data/lib/slack/real_time/event_handlers/im_history_changed.rb +0 -18
  100. data/lib/slack/real_time/event_handlers/im_marked.rb +0 -18
  101. data/lib/slack/real_time/event_handlers/im_open.rb +0 -16
  102. data/lib/slack/real_time/event_handlers/manual_presence_change.rb +0 -16
  103. data/lib/slack/real_time/event_handlers/pin_added.rb +0 -18
  104. data/lib/slack/real_time/event_handlers/pin_removed.rb +0 -18
  105. data/lib/slack/real_time/event_handlers/pref_change.rb +0 -16
  106. data/lib/slack/real_time/event_handlers/presence_change.rb +0 -17
  107. data/lib/slack/real_time/event_handlers/reaction_added.rb +0 -18
  108. data/lib/slack/real_time/event_handlers/reaction_removed.rb +0 -18
  109. data/lib/slack/real_time/event_handlers/reconnect_url.rb +0 -18
  110. data/lib/slack/real_time/event_handlers/star_added.rb +0 -18
  111. data/lib/slack/real_time/event_handlers/star_removed.rb +0 -18
  112. data/lib/slack/real_time/event_handlers/subteam_created.rb +0 -18
  113. data/lib/slack/real_time/event_handlers/subteam_self_added.rb +0 -18
  114. data/lib/slack/real_time/event_handlers/subteam_self_removed.rb +0 -18
  115. data/lib/slack/real_time/event_handlers/subteam_updated.rb +0 -18
  116. data/lib/slack/real_time/event_handlers/team_domain_change.rb +0 -17
  117. data/lib/slack/real_time/event_handlers/team_join.rb +0 -16
  118. data/lib/slack/real_time/event_handlers/team_migration_started.rb +0 -18
  119. data/lib/slack/real_time/event_handlers/team_plan_change.rb +0 -16
  120. data/lib/slack/real_time/event_handlers/team_pref_change.rb +0 -16
  121. data/lib/slack/real_time/event_handlers/team_profile_change.rb +0 -18
  122. data/lib/slack/real_time/event_handlers/team_profile_delete.rb +0 -18
  123. data/lib/slack/real_time/event_handlers/team_profile_reorder.rb +0 -18
  124. data/lib/slack/real_time/event_handlers/team_rename.rb +0 -16
  125. data/lib/slack/real_time/event_handlers/user_change.rb +0 -16
  126. data/lib/slack/real_time/event_handlers/user_typing.rb +0 -18
  127. data/lib/slack/real_time/store.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b713ee1b5bbc32bbb54b434199c8245d1edcc528
4
- data.tar.gz: 7122943dc5dabf15ccf656be06b3df08c24e39ba
3
+ metadata.gz: f0aaadcdcd3f329bb20548b8041a631bb8a28519
4
+ data.tar.gz: 27a9e84d26d1934ee603972453f370102554cdce
5
5
  SHA512:
6
- metadata.gz: 8e9b7148626db6c19db75a8688f6f4233158ce7dc7162d084525516ceb8d458e14a8a2dca875d64ac88cad5913e60f3923b28038b54201de7bfa28bb625cb99d
7
- data.tar.gz: 464204c4a4285827791b0c166d958f2fafe7cdadc38af68e40f3db8798e746fd78f70eed0dc5c649f93c8523a2fe8bdda6328ee75456d872cffa7201f1b7fc48
6
+ metadata.gz: a26e4b6f75a481faa9ff2c2b1d45a0f6922aca6784fa88f0f5772a8ad24df56676cc694e595a7fbdeb6a21fdfcb7a74ed0d9c5b5a54cb76b6b5a8747fcf8490f
7
+ data.tar.gz: 70c7e4981a819f91e425a4ba19a1769aad0f5260b0b15a8a2b55085eea824bee75c50e18845e89e91265cbdc4b72e91aa34bb2592629a2b7047639ba988d6dbc
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-02-02 17:03:58 -0500 using RuboCop version 0.35.0.
3
+ # on 2016-02-29 14:50:42 -0500 using RuboCop version 0.35.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -21,26 +21,34 @@ Lint/UnusedBlockArgument:
21
21
  Exclude:
22
22
  - 'lib/slack/messages/formatting.rb'
23
23
 
24
- # Offense count: 12
24
+ # Offense count: 14
25
25
  Metrics/AbcSize:
26
- Max: 31
26
+ Max: 44
27
27
 
28
- # Offense count: 1
28
+ # Offense count: 2
29
29
  # Configuration parameters: CountComments.
30
30
  Metrics/ClassLength:
31
- Max: 131
31
+ Max: 159
32
32
 
33
- # Offense count: 586
33
+ # Offense count: 1
34
+ Metrics/CyclomaticComplexity:
35
+ Max: 9
36
+
37
+ # Offense count: 542
34
38
  # Configuration parameters: AllowURI, URISchemes.
35
39
  Metrics/LineLength:
36
40
  Max: 288
37
41
 
38
- # Offense count: 6
42
+ # Offense count: 8
39
43
  # Configuration parameters: CountComments.
40
44
  Metrics/MethodLength:
41
- Max: 24
45
+ Max: 32
42
46
 
43
- # Offense count: 80
47
+ # Offense count: 1
48
+ Metrics/PerceivedComplexity:
49
+ Max: 11
50
+
51
+ # Offense count: 52
44
52
  # Configuration parameters: Exclude.
45
53
  Style/Documentation:
46
54
  Enabled: false
@@ -51,18 +59,12 @@ Style/FileName:
51
59
  Exclude:
52
60
  - 'lib/slack-ruby-client.rb'
53
61
 
54
- # Offense count: 90
62
+ # Offense count: 92
55
63
  # Configuration parameters: AllowedVariables.
56
64
  Style/GlobalVars:
57
65
  Enabled: false
58
66
 
59
- # Offense count: 1
60
- # Configuration parameters: MinBodyLength.
61
- Style/GuardClause:
62
- Exclude:
63
- - 'lib/slack/real_time/store.rb'
64
-
65
- # Offense count: 15
67
+ # Offense count: 17
66
68
  # Configuration parameters: EnforcedStyle, SupportedStyles.
67
69
  Style/MethodName:
68
70
  Enabled: false
@@ -3,6 +3,7 @@ language: ruby
3
3
  cache: bundler
4
4
 
5
5
  rvm:
6
+ - 2.3.0
6
7
  - 2.2
7
8
  - 2.1
8
9
  - 2.0
@@ -1,3 +1,11 @@
1
+ ### 0.7.0 (3/6/2016)
2
+
3
+ * [#68](https://github.com/dblock/slack-ruby-client/issues/68): The `Slack::RealTime::Config#store_class` is now globally configurable - [@dblock](https://github.com/dblock).
4
+ * [#67](https://github.com/dblock/slack-ruby-client/pull/67): Make `logger` configurable and log HTTP requests and responses as well as RealTime events and socket data - [@mikz](https://github.com/mikz), [@dblock](https://github.com/dblock).
5
+ * Added `Slack::RealTime::Stores::Store` and `Slack::RealTime::Stores::Starter` - [@dblock](https://github.com/dblock).
6
+ * Added `files_revokePublicURL` and `files_sharedPublicURL` to Web API - [@dblock](https://github.com/dblock).
7
+ * [#60](https://github.com/dblock/slack-ruby-client/issues/60): Exceptions in event handlers and commands are no longer fatal - [@dblock](https://github.com/dblock).
8
+
1
9
  ### 0.6.0 (2/4/2016)
2
10
 
3
11
  * [#54](https://github.com/dblock/slack-ruby-client/issues/54): RealTime client maintains a local store of team data for `client.self`, `team`, `users`, `channels`, `groups`, `ims` and `bots` data - [@dblock](https://github.com/dblock).
data/README.md CHANGED
@@ -18,7 +18,7 @@ A Ruby client for the Slack [Web](https://api.slack.com/web) and [RealTime Messa
18
18
 
19
19
  ## Stable Release
20
20
 
21
- You're reading the documentation for the **stable** release of slack-ruby-client, 0.6.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
21
+ You're reading the documentation for the **stable** release of slack-ruby-client, 0.7.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
22
22
 
23
23
  ## Installation
24
24
 
@@ -55,6 +55,15 @@ end
55
55
 
56
56
  This sets a global default token. You can also pass a token into the initializer of both `Slack::Web::Client` and `Slack::RealTime::Client` or configure those separately via `Slack::Web::Config.configure` and `Slack::RealTime::Config.configure`. The instance token will be used over the client type token over the global default.
57
57
 
58
+ ### Global Settings
59
+
60
+ The following global settings are supported via `Slack.configure`.
61
+
62
+ setting | description
63
+ -------------|-------------------------------------------------------------------------------------------------
64
+ token | Slack API token.
65
+ logger | An optional logger, defaults to `::Logger.new(STDOUT)` at `Logger::WARN` level.
66
+
58
67
  ### Web Client
59
68
 
60
69
  The Slack Web API allows you to build applications that interact with Slack.
@@ -215,7 +224,17 @@ bots | Details of the integrations set up on this team.
215
224
 
216
225
  It also tracks changes, such as users being renamed, added or deleted, therefore `client.users` is always up-to-date.
217
226
 
218
- Tracking with a local store can be disabled with `Slack::RealTime::Client.new(store_class: nil)`
227
+ Tracking with a local store can be disabled with `Slack::RealTime::Client.new(store_class: nil)`. Other stores are also available.
228
+
229
+ #### Slack::RealTime::Stores::Store
230
+
231
+ The default store that tracks all changes.
232
+
233
+ #### Slack::RealTime::Stores::Starter
234
+
235
+ A smaller store that only stores and tracks information about the bot user, but not channels, users, groups, ims or bots.
236
+
237
+ ### Configuring Slack::RealTime::Client
219
238
 
220
239
  You can configure the RealTime client either globally or via the initializer.
221
240
 
@@ -236,10 +255,11 @@ setting | description
236
255
  token | Slack API token.
237
256
  websocket_ping | The number of seconds that indicates how often the WebSocket should send ping frames, default is 30.
238
257
  websocket_proxy | Connect via proxy, include `:origin` and `:headers`.
239
- store_class | Local store class name, default is an in-memory `Slack::RealTime::Store`.
258
+ store_class | Local store class name, default is an in-memory `Slack::RealTime::Stores::Store`.
240
259
  start_options | Options to pass into `rtm.start`, default is `{}`.
260
+ logger | Optional `Logger` instance that logs RealTime requests and socket data.
241
261
 
242
- Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.start](https://api.slack.com/methods/rtm.start), configure Web client options via `Slack::Web::Client.configure` as described above.
262
+ Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.start](https://api.slack.com/methods/rtm.start). While `token` and `logger` options are passed down from the RealTime client, you may also configure Web client options via `Slack::Web::Client.configure` as described above.
243
263
 
244
264
  See a fully working example in [examples/hi_real_time](examples/hi_real_time/hi.rb).
245
265
 
@@ -17,29 +17,30 @@ command 'chat' do |g|
17
17
  g.command 'postMessage' do |c|
18
18
  c.flag 'channel', desc: 'Channel, private group, or IM channel to send message to. Can be an encoded ID, or a name. See below for more details.'
19
19
  c.flag 'text', desc: 'Text of the message to send. See below for an explanation of formatting.'
20
- c.flag 'username', desc: 'Name of bot.'
21
- c.flag 'as_user', desc: 'Pass true to post the message as the authed user, instead of as a bot.'
22
- c.flag 'parse', desc: 'Change how messages are treated. See below.'
20
+ c.flag 'parse', desc: 'Change how messages are treated. Defaults to none. See below.'
23
21
  c.flag 'link_names', desc: 'Find and link channel names and usernames.'
24
22
  c.flag 'attachments', desc: 'Structured message attachments.'
25
23
  c.flag 'unfurl_links', desc: 'Pass true to enable unfurling of primarily text-based content.'
26
24
  c.flag 'unfurl_media', desc: 'Pass false to disable unfurling of media content.'
27
- c.flag 'icon_url', desc: 'URL to an image to use as the icon for this message.'
28
- c.flag 'icon_emoji', desc: 'emoji to use as the icon for this message. Overrides icon_url.'
25
+ c.flag 'username', desc: "Set your bot's user name. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below."
26
+ c.flag 'as_user', desc: 'Pass true to post the message as the authed user, instead of as a bot. Defaults to false. See authorship below.'
27
+ c.flag 'icon_url', desc: 'URL to an image to use as the icon for this message. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below.'
28
+ c.flag 'icon_emoji', desc: 'emoji to use as the icon for this message. Overrides icon_url. Must be used in conjunction with as_user set to false, otherwise ignored. See authorship below.'
29
29
  c.action do |_global_options, options, _args|
30
30
  puts JSON.dump($client.chat_postMessage(options))
31
31
  end
32
32
  end
33
33
 
34
- g.desc 'This method updates a message in a channel.'
35
- g.long_desc %( This method updates a message in a channel. )
34
+ g.desc 'This method updates a message in a channel. Though related to chat.postMessage, some parameters of chat.update are handled differently.'
35
+ g.long_desc %( This method updates a message in a channel. Though related to chat.postMessage, some parameters of chat.update are handled differently. )
36
36
  g.command 'update' do |c|
37
37
  c.flag 'ts', desc: 'Timestamp of the message to be updated.'
38
38
  c.flag 'channel', desc: 'Channel containing the message to be updated.'
39
39
  c.flag 'text', desc: 'New text for the message, using the default formatting rules.'
40
40
  c.flag 'attachments', desc: 'Structured message attachments.'
41
- c.flag 'parse', desc: 'Change how messages are treated. See below.'
42
- c.flag 'link_names', desc: 'Find and link channel names and usernames.'
41
+ c.flag 'parse', desc: 'Change how messages are treated. Defaults to client, unlike chat.postMessage. See below.'
42
+ c.flag 'link_names', desc: 'Find and link channel names and usernames. Defaults to none. This parameter should be used in conjunction with parse. To set link_names to 1, specify a parse mode of full.'
43
+ c.flag 'as_user', desc: 'Pass true to update the message as the authed user. Bot users in this context are considered authed users.'
43
44
  c.action do |_global_options, options, _args|
44
45
  puts JSON.dump($client.chat_update(options))
45
46
  end
@@ -25,7 +25,7 @@ command 'files' do |g|
25
25
  g.desc 'This method returns information about a file in your team.'
26
26
  g.long_desc %( This method returns information about a file in your team. )
27
27
  g.command 'info' do |c|
28
- c.flag 'file', desc: 'File to fetch info for.'
28
+ c.flag 'file', desc: 'Specify a file by providing its ID.'
29
29
  c.action do |_global_options, options, _args|
30
30
  puts JSON.dump($client.files_info(options))
31
31
  end
@@ -56,6 +56,24 @@ You can pass multiple values in the types argument, like types=posts,snippets.Th
56
56
  end
57
57
  end
58
58
 
59
+ g.desc 'This method disables public/external sharing for a file.'
60
+ g.long_desc %( This method disables public/external sharing for a file. )
61
+ g.command 'revokePublicURL' do |c|
62
+ c.flag 'file', desc: 'File to revoke.'
63
+ c.action do |_global_options, options, _args|
64
+ puts JSON.dump($client.files_revokePublicURL(options))
65
+ end
66
+ end
67
+
68
+ g.desc 'This method enables public/external sharing for a file.'
69
+ g.long_desc %( This method enables public/external sharing for a file. )
70
+ g.command 'sharedPublicURL' do |c|
71
+ c.flag 'file', desc: 'File to share.'
72
+ c.action do |_global_options, options, _args|
73
+ puts JSON.dump($client.files_sharedPublicURL(options))
74
+ end
75
+ end
76
+
59
77
  g.desc 'This method allows you to create or upload an existing file.'
60
78
  g.long_desc %( This method allows you to create or upload an existing file. )
61
79
  g.command 'upload' do |c|
@@ -2,6 +2,8 @@ require 'slack-ruby-client'
2
2
 
3
3
  Slack.configure do |config|
4
4
  config.token = ENV['SLACK_API_TOKEN']
5
+ config.logger = Logger.new(STDOUT)
6
+ config.logger.level = Logger::WARN
5
7
  fail 'Missing ENV[SLACK_API_TOKEN]!' unless config.token
6
8
  end
7
9
 
@@ -1,4 +1,5 @@
1
1
  require 'slack/version'
2
+ require 'slack/logger'
2
3
  require 'slack/config'
3
4
 
4
5
  # Messages
@@ -26,6 +27,8 @@ require 'slack/web/api/endpoints'
26
27
  require 'slack/web/client'
27
28
 
28
29
  # RealTime API
30
+ require 'active_support'
31
+ require 'active_support/core_ext'
29
32
  require 'slack/real_time/concurrency'
30
33
  require 'slack/real_time/socket'
31
34
  require 'slack/real_time/api/message_id'
@@ -33,7 +36,6 @@ require 'slack/real_time/api/ping'
33
36
  require 'slack/real_time/api/message'
34
37
  require 'slack/real_time/api/typing'
35
38
  require 'slack/real_time/models'
39
+ require 'slack/real_time/stores'
36
40
  require 'slack/real_time/config'
37
41
  require 'slack/real_time/client'
38
- require 'slack/real_time/store'
39
- require 'slack/real_time/event_handlers'
@@ -3,10 +3,14 @@ module Slack
3
3
  extend self
4
4
 
5
5
  attr_accessor :token
6
+ attr_accessor :logger
6
7
 
7
8
  def reset
8
9
  self.token = nil
10
+ self.logger = nil
9
11
  end
12
+
13
+ reset
10
14
  end
11
15
 
12
16
  class << self
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ module Slack
4
+ class Logger < ::Logger
5
+ def self.default
6
+ @logger ||= begin
7
+ logger = new STDOUT
8
+ logger.level = Logger::WARN
9
+ logger
10
+ end
11
+ end
12
+ end
13
+ end
@@ -4,6 +4,12 @@ module Slack
4
4
  def presence
5
5
  super['presence']
6
6
  end
7
+
8
+ def to_s
9
+ keys.sort_by(&:to_s).map do |key|
10
+ "#{key}=#{self[key]}"
11
+ end.join(', ')
12
+ end
7
13
  end
8
14
  end
9
15
  end
@@ -1,18 +1,4 @@
1
- # This file was auto-generated by lib/tasks/real_time.rake
2
-
3
- # module Slack
4
- # module RealTime
5
- # module EventHandlers
6
- # module <%= class_name %>
7
- # # <%= desc %>
8
- # # @see https://api.slack.com/events/<%= name %>
9
- # # @see https://github.com/dblock/slack-api-ref/blob/master/events/<%= name %>.json
10
- # def self.call(_client, _data)
11
- # # TODO
12
- # end
13
- # end
14
- # end
15
- # end
16
- # end
17
-
18
- # Slack::RealTime::Client.events['<%= name %>'] = Slack::RealTime::EventHandlers::<%= class_name %>
1
+ # <%= desc %>
2
+ # @see https://api.slack.com/events/<%= name %>
3
+ # @see https://github.com/dblock/slack-api-ref/blob/master/events/<%= name %>.json
4
+ # on :<%= name %> do |data|
@@ -20,14 +20,17 @@ module Slack
20
20
  attr_accessor :url
21
21
  attr_accessor(*Config::ATTRIBUTES)
22
22
 
23
+ protected :logger, :logger=
24
+ protected :store_class, :store_class=
25
+
23
26
  def initialize(options = {})
24
27
  @callbacks = Hash.new { |h, k| h[k] = [] }
25
28
  Slack::RealTime::Config::ATTRIBUTES.each do |key|
26
- send("#{key}=", options[key] || Slack::RealTime.config.send(key))
29
+ send("#{key}=", options.key?(key) ? options[key] : Slack::RealTime.config.send(key))
27
30
  end
28
- @store_class = options.key?(:store_class) ? options[:store_class] : Slack::RealTime::Store
29
31
  @token ||= Slack.config.token
30
- @web_client = Slack::Web::Client.new(token: token)
32
+ @logger ||= (Slack::Config.logger || Slack::Logger.default)
33
+ @web_client = Slack::Web::Client.new(token: token, logger: logger)
31
34
  end
32
35
 
33
36
  [:users, :self, :channels, :team, :teams, :groups, :ims, :bots].each do |store_method|
@@ -91,6 +94,7 @@ module Slack
91
94
  socket_options = {}
92
95
  socket_options[:ping] = websocket_ping if websocket_ping
93
96
  socket_options[:proxy] = websocket_proxy if websocket_proxy
97
+ socket_options[:logger] = logger
94
98
  socket_options
95
99
  end
96
100
 
@@ -101,15 +105,18 @@ module Slack
101
105
  yield driver if block_given?
102
106
 
103
107
  driver.on :open do |event|
108
+ logger.debug("#{self.class}##{__method__}") { event.class.name }
104
109
  open(event)
105
110
  callback(event, :open)
106
111
  end
107
112
 
108
113
  driver.on :message do |event|
114
+ logger.debug("#{self.class}##{__method__}") { "#{event.class}, #{event.data}" }
109
115
  dispatch(event)
110
116
  end
111
117
 
112
118
  driver.on :close do |event|
119
+ logger.debug("#{self.class}##{__method__}") { event.class.name }
113
120
  callback(event, :close)
114
121
  close(event)
115
122
  end
@@ -123,6 +130,7 @@ module Slack
123
130
 
124
131
  def send_json(data)
125
132
  fail ClientNotStartedError unless started?
133
+ logger.debug("#{self.class}##{__method__}") { data }
126
134
  @socket.send_data(data.to_json)
127
135
  end
128
136
 
@@ -144,6 +152,10 @@ module Slack
144
152
  callbacks.each do |c|
145
153
  c.call(event)
146
154
  end
155
+ true
156
+ rescue StandardError => e
157
+ logger.error e
158
+ false
147
159
  end
148
160
 
149
161
  def dispatch(event)
@@ -152,13 +164,22 @@ module Slack
152
164
  type = data.type
153
165
  return false unless type
154
166
  type = type.to_s
167
+ logger.debug("#{self.class}##{__method__}") { data.to_s }
155
168
  run_handlers(type, data) if @store
156
169
  run_callbacks(type, data)
170
+ rescue StandardError => e
171
+ logger.error e
172
+ false
157
173
  end
158
174
 
159
175
  def run_handlers(type, data)
160
- handler = Slack::RealTime::Client.events[type]
161
- handler.call(self, data) if handler
176
+ handlers = store.class.events[type.to_s]
177
+ handlers.each do |handler|
178
+ store.instance_exec(data, &handler)
179
+ end if handlers
180
+ rescue StandardError => e
181
+ logger.error e
182
+ false
162
183
  end
163
184
 
164
185
  def run_callbacks(type, data)
@@ -168,6 +189,9 @@ module Slack
168
189
  c.call(data)
169
190
  end
170
191
  true
192
+ rescue StandardError => e
193
+ logger.error e
194
+ false
171
195
  end
172
196
  end
173
197
  end
@@ -41,9 +41,15 @@ module Slack
41
41
 
42
42
  def read
43
43
  buffer = socket.readpartial(BLOCK_SIZE)
44
+ logger.debug("#{self.class}##{__method__}") { buffer }
44
45
  driver.parse buffer
45
46
  end
46
47
 
48
+ def write(data)
49
+ logger.debug("#{self.class}##{__method__}") { data }
50
+ socket.write(data)
51
+ end
52
+
47
53
  def start_async
48
54
  future = yield self if block_given?
49
55