slacks 0.5.0.pre → 0.5.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
2
  SHA1:
3
- metadata.gz: 3fa3ec9c05fdbe56d801c27cb5acbe5d879c07f7
4
- data.tar.gz: 348dc602d884498580f7d72f9fba829333ab087e
3
+ metadata.gz: 0fe5955626325049c7ec170b8c67cb5b652d2d97
4
+ data.tar.gz: b73672adfc7da3608a86453a6b6c7c6b5271a5c5
5
5
  SHA512:
6
- metadata.gz: aa37ba98e00e52d176b5481cca8793d710cc2f068242e448e1b20052d3da3654503008fd6efecf2a9ec21b230f8a4d4c13fb3fd404a79cb981262e35c5b581fd
7
- data.tar.gz: 978735f54f957ee598631502c914f544aa3666f5a87ebe5f85da1c8bdea80e00b7c1331e49e2415f1749d03afab0e023c715b7c7177db5c43a442286ce5b66d1
6
+ metadata.gz: 44dc3585e769b303e2f82e57cc50b8d83e023c3a9e7a0cbeff276c10da8ddd67965f5f463059adc6151a35fb29f5cf3d5577a0810cfe5e2781c8090777f3315e
7
+ data.tar.gz: 71a0cc131fe1fc997093f8d0da62c971bcc50241699a80ce2cabd2cb8b7c7e0b0d25eb00058b226373ab0a96307bc1abec6a30f6ec3824775adacefa1c85626c
@@ -12,17 +12,12 @@ module Slacks
12
12
  end
13
13
 
14
14
  def reply(*messages)
15
- messages.flatten!
16
15
  return unless messages.any?
17
16
 
18
- first_message = messages.shift
19
- message_options = {}
20
- message_options = messages.shift if messages.length == 1 && messages[0].is_a?(Hash)
21
- slack.send_message(first_message, message_options.merge(channel: id))
22
-
23
- messages.each do |message|
24
- sleep message.length / slack.typing_speed
25
- slack.send_message(message, channel: id)
17
+ if messages.first.is_a?(Array)
18
+ reply_many(messages[0])
19
+ else
20
+ reply_one(*messages)
26
21
  end
27
22
  end
28
23
  alias :say :reply
@@ -61,6 +56,19 @@ module Slacks
61
56
  "##{name}"
62
57
  end
63
58
 
59
+ protected
60
+
61
+ def reply_one(message, options={})
62
+ slack.send_message(message, options.merge(channel: id))
63
+ end
64
+
65
+ def reply_many(messages)
66
+ messages.each_with_index.map do |message, i|
67
+ sleep message.length / slack.typing_speed if i > 0
68
+ slack.send_message(message, channel: id)
69
+ end
70
+ end
71
+
64
72
  private
65
73
  attr_reader :slack
66
74
  end
@@ -77,14 +77,22 @@ module Slacks
77
77
  end
78
78
  end
79
79
 
80
+
81
+
80
82
  def typing_on(channel)
83
+ raise NotListeningError unless listening?
81
84
  websocket.write MultiJson.dump(type: "typing", channel: to_channel_id(channel))
82
85
  end
83
86
 
84
87
  def ping
88
+ raise NotListeningError unless listening?
85
89
  websocket.ping
86
90
  end
87
91
 
92
+ def listening?
93
+ !websocket.nil?
94
+ end
95
+
88
96
 
89
97
 
90
98
  def listen!
@@ -319,12 +327,12 @@ module Slacks
319
327
 
320
328
 
321
329
 
322
- def api(command, options={})
323
- response = http.post(command, options.merge(token: token))
330
+ def api(command, params={})
331
+ response = http.post(command, params.merge(token: token))
324
332
  response = MultiJson.load(response.body)
325
333
  unless response["ok"]
326
334
  response["error"].split(/,\s*/).each do |error_code|
327
- raise ::Slacks::Response.fetch(error_code).new(response)
335
+ raise ::Slacks::Response.fetch(error_code).new(command, params, response)
328
336
  end
329
337
  end
330
338
  response
@@ -3,19 +3,29 @@ require "slacks/core_ext/exception"
3
3
  module Slacks
4
4
  module Response
5
5
  class Error < RuntimeError
6
- attr_reader :response
6
+ attr_reader :command, :params, :response
7
7
 
8
- def initialize(response, message)
8
+ def initialize(command, params, response, message)
9
9
  super message
10
+ @command = command
11
+ @params = params
10
12
  @response = response
13
+ additional_information[:command] = command
14
+ additional_information[:params] = params
11
15
  additional_information[:response] = response
12
16
  end
13
17
  end
14
18
 
19
+ class UnspecifiedError < ::Slacks::Response::Error
20
+ def initialize(command, params, response)
21
+ super command, params, response, "Request failed with #{response["error"].inspect}"
22
+ end
23
+ end
24
+
15
25
  @_errors = {}
16
26
 
17
27
  def self.fetch(error_code)
18
- @_errors.fetch(error_code, ::Slacks::Response::Error)
28
+ @_errors.fetch(error_code, ::Slacks::Response::UnspecifiedError)
19
29
  end
20
30
 
21
31
  {
@@ -25,6 +35,7 @@ module Slacks
25
35
  "cant_update_message" => "Authenticated user does not have permission to update this message.",
26
36
  "channel_not_found" => "Value passed for channel was invalid.",
27
37
  "edit_window_closed" => "The message cannot be edited due to the team message edit settings",
38
+ "fatal_error" => "",
28
39
  "file_comment_not_found" => "File comment specified by file_comment does not exist.",
29
40
  "file_not_found" => "File specified by file does not exist.",
30
41
  "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.",
@@ -49,15 +60,15 @@ module Slacks
49
60
  "too_many_emoji" => "The limit for distinct reactions (i.e emoji) on the item has been reached.",
50
61
  "too_many_reactions" => "The limit for reactions a person may add to the item has been reached."
51
62
  }.each do |error_code, message|
52
- class_name = error_code.classify
63
+ class_name = error_code.gsub(/(?:^|_)([a-z]+)/) { $1.capitalize }
53
64
  class_name = {
54
65
  "MsgTooLong" => "MessageTooLong"
55
66
  }.fetch(class_name, class_name)
56
67
 
57
68
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
58
69
  class #{class_name} < ::Slacks::Response::Error
59
- def initialize(response)
60
- super response, "#{message}"
70
+ def initialize(command, params, response)
71
+ super command, params, response, "#{message}"
61
72
  end
62
73
  end
63
74
 
@@ -84,4 +95,10 @@ module Slacks
84
95
  super "Unable to connect to Slack; a token has no"
85
96
  end
86
97
  end
98
+
99
+ class NotListeningError < ArgumentError
100
+ def initialize
101
+ super "Not connected to the RTM API; call `listen!` first"
102
+ end
103
+ end
87
104
  end
@@ -1,3 +1,3 @@
1
1
  module Slacks
2
- VERSION = "0.5.0.pre"
2
+ VERSION = "0.5.0"
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.5.0.pre
4
+ version: 0.5.0
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-28 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver
@@ -207,12 +207,12 @@ 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: 1.3.1
212
+ version: '0'
213
213
  requirements: []
214
214
  rubyforge_project:
215
- rubygems_version: 2.2.2
215
+ rubygems_version: 2.5.1
216
216
  signing_key:
217
217
  specification_version: 4
218
218
  summary: A library for communicating via Slack