fde-slack-notification 1.2.1 → 1.3.1

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
  SHA256:
3
- metadata.gz: aa529431a2cfe13fa1deedecf26504d6ab4c4034fed23024da208eed611c884e
4
- data.tar.gz: 89917fc9336855d30bb4bf9dd90ca36708f4ed87f6543dcca0b14aca08a6b6ff
3
+ metadata.gz: a27394e5562d79819b179f8e001754ed59c49143450ac72f0dbc7751a3f90961
4
+ data.tar.gz: c1e22be84903957d0d8ab6582bd031a9e7d5b75a11041a14f6767971ed61db5f
5
5
  SHA512:
6
- metadata.gz: 8fc84421fef8a53233937d406c0053138376398b6a659c7d12a7724edc7547e091de3de514706a9aa0735bec1886bcf1a5bed9e796ec04b84ccd0a2283982465
7
- data.tar.gz: c54e6f661699731af42d7a0f4206433fd62d61fca85153a094f4e3df9477ee82b7a35d241258fa4fb0242d4f6ac695db3dc37fcc261aeba9219108636486aed1
6
+ metadata.gz: e5aca1edeaf43bfbb723c16c42bb9786945e6b5f6bbe216d0fdff8251842453aa8813453cd101f5f11bb266f1b3fcb5dc8c21bd5b6df34cd6a5a36aec586fb16
7
+ data.tar.gz: 9d78a00e0c8faec3653c1dc19ae5b5aedd835f46dee1cbbdc15e5e61df3bbe217c6d1d17b1a576b361a003a48ad6ae82d4746d74961abfc4c5d3bba34e151dbf
@@ -9,6 +9,9 @@ module FDE
9
9
  YELLOW = '#FEEFB3'.freeze
10
10
  RED = '#FFBABA'.freeze
11
11
 
12
+ RETRY_LIMIT = 1
13
+ TOO_MANY_REQUESTS_STATUS_CODE = "429"
14
+
12
15
 
13
16
  attr_accessor :username,
14
17
  :title,
@@ -17,6 +20,8 @@ module FDE
17
20
  :footer,
18
21
  :color
19
22
 
23
+ attr_reader :retries
24
+
20
25
  def initialize(title, fields, options = {})
21
26
  @title = title
22
27
  @fields = fields
@@ -32,6 +37,8 @@ module FDE
32
37
  if options[:footer]
33
38
  @footer = options[:footer]
34
39
  end
40
+
41
+ @retries = 0
35
42
  end
36
43
 
37
44
  def deliver(channel, level: :info)
@@ -69,10 +76,22 @@ module FDE
69
76
  FDE::Slack::Notification.config.webhook,
70
77
  channel: channel,
71
78
  username: @username
72
- )
79
+ ) do
80
+ # configure Slack Notifier gem to use our custom HTTPClient
81
+ # see https://github.com/stevenosloan/slack-notifier#custom-http-client
82
+ http_client FDE::Slack::Util::HTTPClient
83
+ end
84
+
73
85
  begin
74
86
  notifier.ping message_hash
75
- rescue ::Slack::Notifier::APIError
87
+ rescue FDE::Slack::APIError => api_error
88
+ # TooManyRequests, Slack Rate Limit
89
+ if api_error.response.code == TOO_MANY_REQUESTS_STATUS_CODE && @retries < RETRY_LIMIT
90
+ timeout = api_error.response.header['Retry-After'].to_i
91
+ sleep(timeout) if timeout
92
+ @retries += 1
93
+ retry
94
+ end
76
95
  raise FDE::Slack::Message::Error
77
96
  end
78
97
  end
@@ -5,6 +5,7 @@ require "slack/message"
5
5
  require "slack/field"
6
6
  require "slack/author"
7
7
  require "slack/footer"
8
+ require "slack/util/http_client"
8
9
 
9
10
  module FDE
10
11
  module Slack
@@ -1,7 +1,7 @@
1
1
  module FDE
2
2
  module Slack
3
3
  module Notification
4
- VERSION = "1.2.1"
4
+ VERSION = "1.3.1"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+
5
+ # Custom HTTP Client to auto-retry once when Slack Rate Limit error is encountered.
6
+ # The basis of this code is from https://github.com/stevenosloan/slack-notifier/blob/master/lib/slack-notifier/util/http_client.rb
7
+ # The improved APIError code from https://github.com/stevenosloan/slack-notifier/pull/111
8
+ # And the rate limit retry is custom
9
+ module FDE
10
+ module Slack
11
+ class APIError < StandardError
12
+ attr_reader :response
13
+
14
+ def initialize(response)
15
+ @response = response
16
+ end
17
+
18
+ def message
19
+ <<-MSG
20
+ The slack API returned an error with HTTP Code #{@response.code}
21
+ Check the "Handling Errors" section on https://api.slack.com/incoming-webhooks for more information
22
+ MSG
23
+ end
24
+ end
25
+
26
+ module Util
27
+ class HTTPClient
28
+ class << self
29
+ def post uri, params
30
+ HTTPClient.new(uri, params).call
31
+ end
32
+ end
33
+
34
+ attr_reader :uri, :params, :http_options
35
+
36
+ def initialize uri, params
37
+ @uri = uri
38
+ @http_options = params.delete(:http_options) || {}
39
+ @params = params
40
+ end
41
+
42
+ def call
43
+ http_obj.request(request_obj).tap do |response|
44
+ unless response.is_a?(Net::HTTPSuccess)
45
+ raise FDE::Slack::APIError.new(response)
46
+ end
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def request_obj
53
+ req = Net::HTTP::Post.new uri.request_uri
54
+ req.set_form_data params
55
+
56
+ req
57
+ end
58
+
59
+ def http_obj
60
+ http = Net::HTTP.new uri.host, uri.port
61
+ http.use_ssl = (uri.scheme == "https")
62
+
63
+ http_options.each do |opt, val|
64
+ if http.respond_to? "#{opt}="
65
+ http.send "#{opt}=", val
66
+ else
67
+ warn "Net::HTTP doesn't respond to `#{opt}=`, ignoring that option"
68
+ end
69
+ end
70
+
71
+ http
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fde-slack-notification
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Langenegger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-04 00:00:00.000000000 Z
11
+ date: 2020-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-notifier
@@ -104,20 +104,20 @@ dependencies:
104
104
  name: vcr
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - ">="
107
+ - - "~>"
108
108
  - !ruby/object:Gem::Version
109
109
  version: 3.0.3
110
- - - "~>"
110
+ - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: 3.0.3
113
113
  type: :development
114
114
  prerelease: false
115
115
  version_requirements: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - ">="
117
+ - - "~>"
118
118
  - !ruby/object:Gem::Version
119
119
  version: 3.0.3
120
- - - "~>"
120
+ - - ">="
121
121
  - !ruby/object:Gem::Version
122
122
  version: 3.0.3
123
123
  - !ruby/object:Gem::Dependency
@@ -206,6 +206,7 @@ files:
206
206
  - lib/slack/message.rb
207
207
  - lib/slack/notification.rb
208
208
  - lib/slack/notification/version.rb
209
+ - lib/slack/util/http_client.rb
209
210
  homepage: ''
210
211
  licenses:
211
212
  - MIT
@@ -226,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
227
  - !ruby/object:Gem::Version
227
228
  version: '0'
228
229
  requirements: []
229
- rubygems_version: 3.0.3
230
+ rubygems_version: 3.1.4
230
231
  signing_key:
231
232
  specification_version: 4
232
233
  summary: A simple slack client