slacks 0.4.4 → 0.5.0.pre

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5dca8b35462a36816d2ac22a58904c7801e779d
4
- data.tar.gz: ea16047ea5dbafc775bbb6d79bdd30c85446aec8
3
+ metadata.gz: 3fa3ec9c05fdbe56d801c27cb5acbe5d879c07f7
4
+ data.tar.gz: 348dc602d884498580f7d72f9fba829333ab087e
5
5
  SHA512:
6
- metadata.gz: 423b632d2810deaac520ea364122ff15030876e66dacdde761d957bd7bd8cabd8caaa6d7d0a1a33293bec184f16e8fe051be35eb94291c595cc28f78857e1a54
7
- data.tar.gz: 889dc9cbc90679e968006aebeac2c798981aa0dde9873e3c14bb4e21da3ab19a3756d2da0a27ee085f298c725f5db16c5bd8f53350a987c9ddad5f58c0fa0fa1
6
+ metadata.gz: aa37ba98e00e52d176b5481cca8793d710cc2f068242e448e1b20052d3da3654503008fd6efecf2a9ec21b230f8a4d4c13fb3fd404a79cb981262e35c5b581fd
7
+ data.tar.gz: 978735f54f957ee598631502c914f544aa3666f5a87ebe5f85da1c8bdea80e00b7c1331e49e2415f1749d03afab0e023c715b7c7177db5c43a442286ce5b66d1
@@ -43,8 +43,6 @@ module Slacks
43
43
  params.merge!(attachments: MultiJson.dump(attachments)) if attachments.any?
44
44
  params.merge!(options.select { |key, _| SEND_MESSAGE_PARAMS.member?(key) })
45
45
  api("chat.postMessage", params)
46
- rescue Slacks::ResponseError
47
- $!.response
48
46
  end
49
47
  alias :say :send_message
50
48
 
@@ -53,8 +51,6 @@ module Slacks
53
51
  channel: to_channel_id(channel),
54
52
  timestamp: ts }
55
53
  api("reactions.get", params)
56
- rescue Slacks::ResponseError
57
- $!.response
58
54
  end
59
55
 
60
56
  def update_message(ts, message, options={})
@@ -70,8 +66,6 @@ module Slacks
70
66
  params.merge!(options.select { |key, _| [:username, :as_user, :parse, :link_names,
71
67
  :unfurl_links, :unfurl_media, :icon_url, :icon_emoji].member?(key) })
72
68
  api("chat.update", params)
73
- rescue Slacks::ResponseError
74
- $!.response
75
69
  end
76
70
 
77
71
  def add_reaction(emojis, message)
@@ -81,22 +75,20 @@ module Slacks
81
75
  channel: message.channel.id,
82
76
  timestamp: message.timestamp })
83
77
  end
84
- rescue Slacks::ResponseError
85
- $!.response
86
78
  end
87
79
 
88
80
  def typing_on(channel)
89
81
  websocket.write MultiJson.dump(type: "typing", channel: to_channel_id(channel))
90
82
  end
91
83
 
84
+ def ping
85
+ websocket.ping
86
+ end
87
+
92
88
 
93
89
 
94
90
  def listen!
95
91
  response = api("rtm.start")
96
- unless response["ok"]
97
- raise MigrationInProgress if response["error"] == "migration_in_progress"
98
- raise ResponseError.new(response, response["error"])
99
- end
100
92
  store_context!(response)
101
93
 
102
94
  @websocket = Slacks::Driver.new
@@ -246,8 +238,6 @@ module Slacks
246
238
 
247
239
  @groups_by_id = Hash[response.fetch("groups").map { |attrs| [attrs.fetch("id"), attrs] }]
248
240
  @group_id_by_name = Hash[response.fetch("groups").map { |attrs| [attrs.fetch("name"), attrs.fetch("id")] }]
249
- rescue KeyError
250
- raise ResponseError.new(response, $!.message)
251
241
  end
252
242
 
253
243
 
@@ -276,7 +266,6 @@ module Slacks
276
266
  def get_dm_for_user_id(user_id)
277
267
  user_ids_dm_ids[user_id] ||= begin
278
268
  response = api("im.open", user: user_id)
279
- raise UnableToDirectMessageError.new(response, user_id) unless response["ok"]
280
269
  response["channel"]["id"]
281
270
  end
282
271
  end
@@ -332,7 +321,13 @@ module Slacks
332
321
 
333
322
  def api(command, options={})
334
323
  response = http.post(command, options.merge(token: token))
335
- MultiJson.load(response.body)
324
+ response = MultiJson.load(response.body)
325
+ unless response["ok"]
326
+ response["error"].split(/,\s*/).each do |error_code|
327
+ raise ::Slacks::Response.fetch(error_code).new(response)
328
+ end
329
+ end
330
+ response
336
331
 
337
332
  rescue MultiJson::ParseError
338
333
  $!.additional_information[:response_body] = response.body
data/lib/slacks/driver.rb CHANGED
@@ -70,6 +70,10 @@ module Slacks
70
70
  @driver.text(message)
71
71
  end
72
72
 
73
+ def ping
74
+ @driver.ping
75
+ end
76
+
73
77
  def connected?
74
78
  @connected || false
75
79
  end
data/lib/slacks/errors.rb CHANGED
@@ -1,19 +1,68 @@
1
1
  require "slacks/core_ext/exception"
2
2
 
3
3
  module Slacks
4
- class MigrationInProgress < RuntimeError
5
- def initialize
6
- super "Team is being migrated between servers. Try the request again in a few seconds."
4
+ module Response
5
+ class Error < RuntimeError
6
+ attr_reader :response
7
+
8
+ def initialize(response, message)
9
+ super message
10
+ @response = response
11
+ additional_information[:response] = response
12
+ end
7
13
  end
8
- end
9
14
 
10
- class ResponseError < RuntimeError
11
- attr_reader :response
15
+ @_errors = {}
12
16
 
13
- def initialize(response, message)
14
- super message
15
- @response = response
16
- additional_information[:response] = response
17
+ def self.fetch(error_code)
18
+ @_errors.fetch(error_code, ::Slacks::Response::Error)
19
+ end
20
+
21
+ {
22
+ "account_inactive" => "Authentication token is for a deleted user or team.",
23
+ "already_reacted" => "The specified item already has the user/reaction combination.",
24
+ "bad_timestamp" => "Value passed for timestamp was invalid.",
25
+ "cant_update_message" => "Authenticated user does not have permission to update this message.",
26
+ "channel_not_found" => "Value passed for channel was invalid.",
27
+ "edit_window_closed" => "The message cannot be edited due to the team message edit settings",
28
+ "file_comment_not_found" => "File comment specified by file_comment does not exist.",
29
+ "file_not_found" => "File specified by file does not exist.",
30
+ "invalid_arg_name" => "The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
31
+ "invalid_array_arg" => "The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
32
+ "invalid_auth" => "Invalid authentication token.",
33
+ "invalid_charset" => "The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
34
+ "invalid_form_data" => "The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
35
+ "invalid_name" => "Value passed for name was invalid.",
36
+ "invalid_post_type" => "The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/json application/x-www-form-urlencoded multipart/form-data text/plain.",
37
+ "is_archived" => "Channel has been archived.",
38
+ "message_not_found" => "Message specified by channel and timestamp does not exist.",
39
+ "migration_in_progress" => "Team is being migrated between servers. See the team_migration_started event documentation for details.",
40
+ "missing_post_type" => "The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
41
+ "msg_too_long" => "Message text is too long",
42
+ "no_item_specified" => "file, file_comment, or combination of channel and timestamp was not specified.",
43
+ "no_text" => "No message text provided",
44
+ "not_authed" => "No authentication token provided.",
45
+ "not_in_channel" => "Cannot post user messages to a channel they are not in.",
46
+ "rate_limited" => "Application has posted too many messages, read the Rate Limit documentation for more information",
47
+ "request_timeout" => "The method was called via a POST request, but the POST data was either missing or truncated.",
48
+ "too_many_attachments" => "Too many attachments were provided with this message. A maximum of 100 attachments are allowed on a message.",
49
+ "too_many_emoji" => "The limit for distinct reactions (i.e emoji) on the item has been reached.",
50
+ "too_many_reactions" => "The limit for reactions a person may add to the item has been reached."
51
+ }.each do |error_code, message|
52
+ class_name = error_code.classify
53
+ class_name = {
54
+ "MsgTooLong" => "MessageTooLong"
55
+ }.fetch(class_name, class_name)
56
+
57
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
58
+ class #{class_name} < ::Slacks::Response::Error
59
+ def initialize(response)
60
+ super response, "#{message}"
61
+ end
62
+ end
63
+
64
+ @_errors["#{error_code}"] = ::Slacks::Response::#{class_name}
65
+ RUBY
17
66
  end
18
67
  end
19
68
 
@@ -24,21 +73,15 @@ module Slacks
24
73
  end
25
74
  end
26
75
 
27
- class AlreadyRespondedError < RuntimeError
28
- def initialize(message=nil)
29
- super message || "You have already replied to this Slash Command; you can only reply once"
30
- end
31
- end
32
-
33
76
  class NotInChannelError < RuntimeError
34
77
  def initialize(channel)
35
78
  super "The bot is not in the channel #{channel} and cannot reply"
36
79
  end
37
80
  end
38
81
 
39
- class UnableToDirectMessageError < ResponseError
40
- def initialize(response, user_id)
41
- super response, "Unable to direct message the user #{user_id.inspect}: #{response["error"]}"
82
+ class MissingTokenError < ArgumentError
83
+ def initialize
84
+ super "Unable to connect to Slack; a token has no"
42
85
  end
43
86
  end
44
87
  end
@@ -1,3 +1,3 @@
1
1
  module Slacks
2
- VERSION = "0.4.4"
2
+ VERSION = "0.5.0.pre"
3
3
  end
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.4.4
4
+ version: 0.5.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-19 00:00:00.000000000 Z
11
+ date: 2017-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver
@@ -207,9 +207,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
207
207
  version: '0'
208
208
  required_rubygems_version: !ruby/object:Gem::Requirement
209
209
  requirements:
210
- - - ">="
210
+ - - ">"
211
211
  - !ruby/object:Gem::Version
212
- version: '0'
212
+ version: 1.3.1
213
213
  requirements: []
214
214
  rubyforge_project:
215
215
  rubygems_version: 2.2.2