fde-slack-notification 1.1.0 → 1.3.2

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: 2784e5975e3a8efc0e22810aae9944e1d5572d09a4931fce0842843e1238c2de
4
- data.tar.gz: 0fcceb18a5fa14033d3c95be86ea9ed76f75d7d332091972cc01afa97d2fd8f4
3
+ metadata.gz: f48d43de6e5f3865f1fb98727994aa03747283ea2833b13cc3b9266e10f58070
4
+ data.tar.gz: 67ddbe72857d9586d48bad41d79ebaa19743081892cd04bb5fc354ac765aa793
5
5
  SHA512:
6
- metadata.gz: 38570cd1de29a26612660fa90e4c6031985f328e7c7f1c5baec58249b278441f595b935aed71918b5a573c0e6950952c3b22b6e0ca858547416f9bd0e98d96a8
7
- data.tar.gz: 5107caf647b0dd7feef60b498db8e61e761d32f33b9cc43a2d1a795a47f6efa5610ec5a77aa441eb6c17fee2344b005c11bc7aa3a47c0cdba414bc4b08e0a91e
6
+ metadata.gz: 289325450737e0ce1baeb9b8544189fa1867a9669483549659ab69beafc8d583af42647a9347e61adbfa7abde626f74d9e4260637165a23ed85dfc5e4e67ebe2
7
+ data.tar.gz: d0e530061d0370791619651f865ba3977dbe9466f704bdddd6627f5579cc9a607bbe30fa9e6cbfe6daca18fda1b480f791eac52bcc9f22b64c7857a2f3a8b303
data/README.md CHANGED
@@ -3,3 +3,87 @@
3
3
  [![Code Climate](https://codeclimate.com/github/fashion-data-exchange/slack-notification/badges/gpa.svg)](https://codeclimate.com/github/fashion-data-exchange/slack-notification)
4
4
 
5
5
  # Slack::Notification
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'fde-slack-notification'
12
+ ```
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ ### Authorization
20
+
21
+ The client requires the following environment variables to be set:
22
+
23
+ ```bash
24
+ SLACK_FD_WEBHOOK_URL = 'https://hooks.slack.com' //Your slack hook here
25
+ ```
26
+
27
+ ### Usage
28
+
29
+ In order to send a notification to slack, you can write a method like the following.
30
+
31
+ ```ruby
32
+ def notifier
33
+ message = FDE::Slack::Message.new(
34
+ 'your message title',
35
+ fields,
36
+ {
37
+ title_link:
38
+ {
39
+ title: 'title',
40
+ title_link: 'title',
41
+ thumb_url: 'author_icon'
42
+ },
43
+ footer: FDE::Slack::Footer.new('footer_text).to_h
44
+ }
45
+ )
46
+
47
+ message.info(channel)
48
+ end
49
+
50
+
51
+ # create a hash for your required fields
52
+ def fields
53
+ [
54
+ {
55
+ title: 'Field 1',
56
+ value: 'value 1',
57
+ short: false
58
+ },
59
+ {
60
+ title: 'Field 2',
61
+ value: 'value 2',
62
+ short: false
63
+ },
64
+ ]
65
+ end
66
+
67
+ ```
68
+
69
+ ### Configuration
70
+
71
+ To set the hook_url value, you may have an initializer under `config/initializers` like this
72
+
73
+ ```ruby
74
+ require "slack/notification"
75
+
76
+ FDE::Slack::Notification.configure do |config|
77
+ config.webhook = 'hook_url'
78
+ end
79
+ ```
80
+
81
+
82
+ ### Styling
83
+
84
+ Any of these methods can be used to get the following colored sidebars
85
+
86
+ 1. message.info(channel) # BLUE
87
+ 2. message.success(channel) # GREEN
88
+ 3. message.error(channel) # RED
89
+ 4. message.warning(channel) # YELLOW
@@ -30,17 +30,17 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
- spec.add_runtime_dependency "slack-notifier", "~> 1.5", ">= 1.5.1"
33
+ spec.add_runtime_dependency "slack-notifier", "~> 2.0"
34
34
  spec.add_runtime_dependency "dotenv", "~> 2.2", ">= 2.2.1"
35
35
 
36
- spec.add_development_dependency "bundler", "~> 1.15"
37
- spec.add_development_dependency "rake", "~> 10.0"
38
- spec.add_development_dependency "rspec", "~> 3.0"
39
- spec.add_development_dependency "rdoc", "~> 5.1", ">= 5.1"
36
+ spec.add_development_dependency "bundler", "~> 2"
37
+ spec.add_development_dependency "rake", "~> 13.0"
38
+ spec.add_development_dependency "rspec", "~> 3"
39
+ spec.add_development_dependency "rdoc", "~> 5"
40
40
  spec.add_development_dependency "vcr", "~> 3.0.3", ">= 3.0.3"
41
41
  spec.add_development_dependency 'webmock', '~> 3.0', '>= 3.0.1'
42
42
 
43
- spec.add_development_dependency "pry", "~> 0.10"
44
- spec.add_development_dependency "pry-remote", "~> 0.1"
45
- spec.add_development_dependency "pry-nav", "~> 0.2"
43
+ spec.add_development_dependency "pry", "~> 0"
44
+ spec.add_development_dependency "pry-remote", "~> 0"
45
+ spec.add_development_dependency "pry-nav", "~> 0"
46
46
  end
@@ -9,6 +9,9 @@ module FDE
9
9
  YELLOW = '#FEEFB3'.freeze
10
10
  RED = '#FFBABA'.freeze
11
11
 
12
+ RETRY_LIMIT = 3
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,11 +76,24 @@ 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
76
- raise FDE::Slack::Message::Error
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
95
+ raise api_error, message_hash: message_hash
96
+
77
97
  end
78
98
  end
79
99
 
@@ -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.1.0"
4
+ VERSION = "1.3.2"
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.1.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Langenegger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2020-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-notifier
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.5.1
19
+ version: '2.0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '1.5'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 1.5.1
26
+ version: '2.0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: dotenv
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -56,62 +50,56 @@ dependencies:
56
50
  requirements:
57
51
  - - "~>"
58
52
  - !ruby/object:Gem::Version
59
- version: '1.15'
53
+ version: '2'
60
54
  type: :development
61
55
  prerelease: false
62
56
  version_requirements: !ruby/object:Gem::Requirement
63
57
  requirements:
64
58
  - - "~>"
65
59
  - !ruby/object:Gem::Version
66
- version: '1.15'
60
+ version: '2'
67
61
  - !ruby/object:Gem::Dependency
68
62
  name: rake
69
63
  requirement: !ruby/object:Gem::Requirement
70
64
  requirements:
71
65
  - - "~>"
72
66
  - !ruby/object:Gem::Version
73
- version: '10.0'
67
+ version: '13.0'
74
68
  type: :development
75
69
  prerelease: false
76
70
  version_requirements: !ruby/object:Gem::Requirement
77
71
  requirements:
78
72
  - - "~>"
79
73
  - !ruby/object:Gem::Version
80
- version: '10.0'
74
+ version: '13.0'
81
75
  - !ruby/object:Gem::Dependency
82
76
  name: rspec
83
77
  requirement: !ruby/object:Gem::Requirement
84
78
  requirements:
85
79
  - - "~>"
86
80
  - !ruby/object:Gem::Version
87
- version: '3.0'
81
+ version: '3'
88
82
  type: :development
89
83
  prerelease: false
90
84
  version_requirements: !ruby/object:Gem::Requirement
91
85
  requirements:
92
86
  - - "~>"
93
87
  - !ruby/object:Gem::Version
94
- version: '3.0'
88
+ version: '3'
95
89
  - !ruby/object:Gem::Dependency
96
90
  name: rdoc
97
91
  requirement: !ruby/object:Gem::Requirement
98
92
  requirements:
99
93
  - - "~>"
100
94
  - !ruby/object:Gem::Version
101
- version: '5.1'
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '5.1'
95
+ version: '5'
105
96
  type: :development
106
97
  prerelease: false
107
98
  version_requirements: !ruby/object:Gem::Requirement
108
99
  requirements:
109
100
  - - "~>"
110
101
  - !ruby/object:Gem::Version
111
- version: '5.1'
112
- - - ">="
113
- - !ruby/object:Gem::Version
114
- version: '5.1'
102
+ version: '5'
115
103
  - !ruby/object:Gem::Dependency
116
104
  name: vcr
117
105
  requirement: !ruby/object:Gem::Requirement
@@ -158,42 +146,42 @@ dependencies:
158
146
  requirements:
159
147
  - - "~>"
160
148
  - !ruby/object:Gem::Version
161
- version: '0.10'
149
+ version: '0'
162
150
  type: :development
163
151
  prerelease: false
164
152
  version_requirements: !ruby/object:Gem::Requirement
165
153
  requirements:
166
154
  - - "~>"
167
155
  - !ruby/object:Gem::Version
168
- version: '0.10'
156
+ version: '0'
169
157
  - !ruby/object:Gem::Dependency
170
158
  name: pry-remote
171
159
  requirement: !ruby/object:Gem::Requirement
172
160
  requirements:
173
161
  - - "~>"
174
162
  - !ruby/object:Gem::Version
175
- version: '0.1'
163
+ version: '0'
176
164
  type: :development
177
165
  prerelease: false
178
166
  version_requirements: !ruby/object:Gem::Requirement
179
167
  requirements:
180
168
  - - "~>"
181
169
  - !ruby/object:Gem::Version
182
- version: '0.1'
170
+ version: '0'
183
171
  - !ruby/object:Gem::Dependency
184
172
  name: pry-nav
185
173
  requirement: !ruby/object:Gem::Requirement
186
174
  requirements:
187
175
  - - "~>"
188
176
  - !ruby/object:Gem::Version
189
- version: '0.2'
177
+ version: '0'
190
178
  type: :development
191
179
  prerelease: false
192
180
  version_requirements: !ruby/object:Gem::Requirement
193
181
  requirements:
194
182
  - - "~>"
195
183
  - !ruby/object:Gem::Version
196
- version: '0.2'
184
+ version: '0'
197
185
  description: A simple slack client to send messages into a slack channel
198
186
  email:
199
187
  - f.langenegger@fadendaten.ch
@@ -218,6 +206,7 @@ files:
218
206
  - lib/slack/message.rb
219
207
  - lib/slack/notification.rb
220
208
  - lib/slack/notification/version.rb
209
+ - lib/slack/util/http_client.rb
221
210
  homepage: ''
222
211
  licenses:
223
212
  - MIT
@@ -238,8 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
227
  - !ruby/object:Gem::Version
239
228
  version: '0'
240
229
  requirements: []
241
- rubyforge_project:
242
- rubygems_version: 2.7.6
230
+ rubygems_version: 3.1.4
243
231
  signing_key:
244
232
  specification_version: 4
245
233
  summary: A simple slack client