slack_log_device 2.0.1 → 2.1.0
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 +4 -4
- data/README.mdown +15 -1
- data/VERSION +1 -1
- data/lib/slack_log_device.rb +10 -2
- data/spec/slack_log_device_spec.rb +67 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7c2fbccccb4df5730e6f9ea792d23df250aeaae
|
4
|
+
data.tar.gz: 03700bccd9ef1366dfe3035d8ba31db4aa87002b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 951dc372a3de3848fa203ad903e4bb5c2efde689a28f52249730cadd8555b0137365b845f1fd089f6860857e9faeb957702f110d8e7ca6bdcc105e73423b9993
|
7
|
+
data.tar.gz: 9735f2db5fd0ea7332023c9c8211d7974d07fc6365fc15e01bf87d66782dfc232c041792850ceb991b093ff9d08ae223c1c68ad444e3687fbc63307be777a99a
|
data/README.mdown
CHANGED
@@ -31,12 +31,26 @@ request.
|
|
31
31
|
|
32
32
|
- `auto_flush`: To flush messages directly when a message is written (disabled
|
33
33
|
by default).
|
34
|
-
- `
|
34
|
+
- `channel`: The channel to post message on (webhook configured channel by
|
35
|
+
default). It can be a channel (if starting with `#`) or a specific user (if
|
36
|
+
starting with a `@`).
|
37
|
+
- `flush_delay`: The delay in seconds to send buffered messages (1 by default).
|
35
38
|
- `max_buffer_size`: The max buffer size to flush messages (8192 by default).
|
36
39
|
- `timeout`: The timeout in seconds to send message to slack (5 by default).
|
37
40
|
- `username`: The username to post message as (nil by default).
|
38
41
|
- `webhook_url`: The URL of the webhook (mandatory).
|
39
42
|
|
43
|
+
## Rails configuration
|
44
|
+
|
45
|
+
For a rails application, it is recommanded to use following configuration into
|
46
|
+
`config/environments/production.rb` file:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
config.logger = ActiveSupport::Logger.new(SlackLogDevice.new(webhook_url: 'https://hooks.slack.com/services/...', username: 'MyRailsApp'))
|
50
|
+
config.logger.formatter = ::Logger::Formatter.new
|
51
|
+
config.log_level = :warn
|
52
|
+
```
|
53
|
+
|
40
54
|
## Executing test suite
|
41
55
|
|
42
56
|
This project is fully tested with [Rspec 3](http://github.com/rspec/rspec).
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.0
|
1
|
+
2.1.0
|
data/lib/slack_log_device.rb
CHANGED
@@ -5,14 +5,15 @@ require 'logger'
|
|
5
5
|
|
6
6
|
class SlackLogDevice
|
7
7
|
|
8
|
-
attr_reader :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url
|
8
|
+
attr_reader :channel, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url
|
9
9
|
|
10
10
|
def initialize(options = {})
|
11
|
-
options.assert_valid_keys(:auto_flush, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url)
|
11
|
+
options.assert_valid_keys(:auto_flush, :channel, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url)
|
12
12
|
@buffer = []
|
13
13
|
@mutex = Mutex.new
|
14
14
|
@flush_thread
|
15
15
|
self.auto_flush = options[:auto_flush]
|
16
|
+
self.channel = options[:channel]
|
16
17
|
self.flush_delay = options.key?(:flush_delay) ? options[:flush_delay] : 1
|
17
18
|
self.max_buffer_size = options.key?(:max_buffer_size) ? options[:max_buffer_size] : 8192
|
18
19
|
self.timeout = options.key?(:timeout) ? options[:timeout] : 5
|
@@ -28,6 +29,12 @@ class SlackLogDevice
|
|
28
29
|
@auto_flush = value.present?
|
29
30
|
end
|
30
31
|
|
32
|
+
def channel=(value)
|
33
|
+
channel = value.to_s.presence
|
34
|
+
raise ArgumentError.new("Invalid channel specified: #{value.inspect}, it must start with # or @ and be in lower case with no spaces or special chars and its length must not exceed 22 chars") if channel && channel !~ /^[@#][a-z0-9_-]{1,21}$/
|
35
|
+
@channel = channel
|
36
|
+
end
|
37
|
+
|
31
38
|
def close
|
32
39
|
# Does nothing, this method must exist to consider the LogDevice as an IO.
|
33
40
|
end
|
@@ -40,6 +47,7 @@ class SlackLogDevice
|
|
40
47
|
@buffer.clear
|
41
48
|
end
|
42
49
|
data = { 'text' => message.to_s }
|
50
|
+
data['channel'] = channel if channel.present?
|
43
51
|
data['username'] = username if username.present?
|
44
52
|
begin
|
45
53
|
HTTParty.post(webhook_url, body: data.to_json, headers: { 'Content-Type': 'application/json' }, timeout: timeout)
|
@@ -34,6 +34,65 @@ describe SlackLogDevice do
|
|
34
34
|
|
35
35
|
end
|
36
36
|
|
37
|
+
describe '#channel' do
|
38
|
+
|
39
|
+
it 'is null by default' do
|
40
|
+
options.delete(:channel)
|
41
|
+
expect(device.channel).to be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'is nil if blank' do
|
45
|
+
options[:channel] = " \n"
|
46
|
+
expect(device.channel).to be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'can be set' do
|
50
|
+
expect {
|
51
|
+
device.channel = '#foo-bar_42abc'
|
52
|
+
}.to change { device.channel }.from(nil).to('#foo-bar_42abc')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'raise an error if it contains spaces' do
|
56
|
+
expect {
|
57
|
+
device.channel = '#foo bar'
|
58
|
+
}.to raise_error(ArgumentError, 'Invalid channel specified: "#foo bar", it must start with # or @ and be in lower case with no spaces or special chars and its length must not exceed 22 chars')
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'raise an error if it contains more than 22 chars (including #)' do
|
62
|
+
expect {
|
63
|
+
device.channel = "##{'a' * 22}"
|
64
|
+
}.to raise_error(ArgumentError)
|
65
|
+
device.channel = "##{'a' * 21}" # ok
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'raise an error if it contains upper case letters' do
|
69
|
+
expect {
|
70
|
+
device.channel = '#Foo'
|
71
|
+
}.to raise_error(ArgumentError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'raise an error if it contains special chars' do
|
75
|
+
expect {
|
76
|
+
device.channel = '#f{oo'
|
77
|
+
}.to raise_error(ArgumentError)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'raise an error if it does not start with a # or @' do
|
81
|
+
expect {
|
82
|
+
device.channel = 'foo'
|
83
|
+
}.to raise_error(ArgumentError)
|
84
|
+
device.channel = '#foo' # ok
|
85
|
+
device.channel = '@foo' # ok
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'raise an error if it only contains prefix' do
|
89
|
+
expect {
|
90
|
+
device.channel = '#'
|
91
|
+
}.to raise_error(ArgumentError)
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
37
96
|
describe '#flush' do
|
38
97
|
|
39
98
|
it 'sends a post to webhook URL with given given message and specified username' do
|
@@ -49,6 +108,13 @@ describe SlackLogDevice do
|
|
49
108
|
device.flush
|
50
109
|
end
|
51
110
|
|
111
|
+
it 'use specified channel' do
|
112
|
+
options[:channel] = '#foo'
|
113
|
+
device.write('BAM!')
|
114
|
+
expect(HTTParty).to receive(:post).with(options[:webhook_url], body: { 'text' => 'BAM!', 'channel': '#foo', 'username' => options[:username] }.to_json, headers: { 'Content-Type': 'application/json' }, timeout: 5)
|
115
|
+
device.flush
|
116
|
+
end
|
117
|
+
|
52
118
|
it 'use specified timeout' do
|
53
119
|
options[:timeout] = 12
|
54
120
|
device.write('BAM!')
|
@@ -155,7 +221,7 @@ describe SlackLogDevice do
|
|
155
221
|
it 'raise an error if an invalid option is given' do
|
156
222
|
expect {
|
157
223
|
SlackLogDevice.new(foo: 'bar')
|
158
|
-
}.to raise_error(ArgumentError, "Unknown key: :foo. Valid keys are: :auto_flush, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url")
|
224
|
+
}.to raise_error(ArgumentError, "Unknown key: :foo. Valid keys are: :auto_flush, :channel, :flush_delay, :max_buffer_size, :timeout, :username, :webhook_url")
|
159
225
|
end
|
160
226
|
|
161
227
|
it 'raise an error if webhook option is not given' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack_log_device
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexis Toulotte
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|