slack-notifier 2.2.2 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slack-notifier.rb +4 -2
- data/lib/slack-notifier/config.rb +6 -1
- data/lib/slack-notifier/payload_middleware.rb +1 -0
- data/lib/slack-notifier/payload_middleware/channels.rb +21 -0
- data/lib/slack-notifier/payload_middleware/stack.rb +16 -2
- data/lib/slack-notifier/version.rb +1 -1
- data/spec/integration/ping_integration_test.rb +1 -0
- data/spec/lib/slack-notifier/config_spec.rb +1 -1
- data/spec/lib/slack-notifier/payload_middleware/channels_spec.rb +20 -0
- data/spec/lib/slack-notifier/payload_middleware/stack_spec.rb +27 -1
- data/spec/lib/slack-notifier_spec.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68824ffcac979f733a61c7b5a77d0d21e8ed1dfe
|
4
|
+
data.tar.gz: '08d9cee6b3a56238304f54756640794f443c78ec'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65efd0c9618f26051b0f562f3ee46b5e6c697c479a6fe50d2014f77da3fdfb13c1ad6009f255cf6481cb4922d7bcc33b49ce8d4a65fb3372734f422f87189e7d
|
7
|
+
data.tar.gz: 9b636e5fcd4d0bc0c815fb6d3c0625a9f1fd62a366bd24e4f0cd3fbe368eadc0405410ce0786a87b12ccd5fbd4f9968b1d89239cd955e9ed62f6407982a76276
|
data/lib/slack-notifier.rb
CHANGED
@@ -42,9 +42,11 @@ module Slack
|
|
42
42
|
payload = config.defaults.merge(payload)
|
43
43
|
|
44
44
|
params[:http_options] = payload.delete(:http_options) if payload.key?(:http_options)
|
45
|
-
params[:payload] = middleware.call(payload).to_json
|
46
45
|
|
47
|
-
|
46
|
+
middleware.call(payload).each do |pld|
|
47
|
+
params[:payload] = pld.to_json
|
48
|
+
client.post endpoint, params
|
49
|
+
end
|
48
50
|
end
|
49
51
|
|
50
52
|
private
|
@@ -5,7 +5,12 @@ module Slack
|
|
5
5
|
def initialize
|
6
6
|
@http_client = Util::HTTPClient
|
7
7
|
@defaults = {}
|
8
|
-
@middleware = [
|
8
|
+
@middleware = [
|
9
|
+
:format_message,
|
10
|
+
:format_attachments,
|
11
|
+
:at,
|
12
|
+
:channels,
|
13
|
+
]
|
9
14
|
end
|
10
15
|
|
11
16
|
def http_client client=nil
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Slack
|
4
|
+
class Notifier
|
5
|
+
class PayloadMiddleware
|
6
|
+
class Channels < Base
|
7
|
+
middleware_name :channels
|
8
|
+
|
9
|
+
def call payload={}
|
10
|
+
return payload unless payload[:channel].respond_to?(:to_ary)
|
11
|
+
|
12
|
+
payload[:channel].to_ary.map do |channel|
|
13
|
+
pld = payload.dup
|
14
|
+
pld[:channel] = channel
|
15
|
+
pld
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -25,10 +25,24 @@ module Slack
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def call payload={}
|
28
|
-
stack.inject payload do |pld, middleware|
|
29
|
-
|
28
|
+
result = stack.inject payload do |pld, middleware|
|
29
|
+
as_array(pld).flat_map do |p|
|
30
|
+
middleware.call(p)
|
31
|
+
end
|
30
32
|
end
|
33
|
+
|
34
|
+
as_array(result)
|
31
35
|
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def as_array args
|
40
|
+
if args.respond_to?(:to_ary)
|
41
|
+
args.to_ary
|
42
|
+
else
|
43
|
+
[args]
|
44
|
+
end
|
45
|
+
end
|
32
46
|
end
|
33
47
|
end
|
34
48
|
end
|
@@ -10,5 +10,6 @@ end
|
|
10
10
|
puts "testing with #{ruby}"
|
11
11
|
|
12
12
|
notifier = Slack::Notifier.new ENV["SLACK_WEBHOOK_URL"], username: "notifier"
|
13
|
+
notifier.ping "hello", channel: ["#general", "#random"]
|
13
14
|
notifier.ping "hello/こんにちは from notifier test script on #{ruby}\225"
|
14
15
|
notifier.ping attachments: [{ color: "#1BF5AF", fallback: "fallback", text: "attachment" }]
|
@@ -46,7 +46,7 @@ RSpec.describe Slack::Notifier::Config do
|
|
46
46
|
it "is [:format_message, :format_attachments, :at] if not set" do
|
47
47
|
subject = described_class.new
|
48
48
|
|
49
|
-
expect(subject.middleware).to eq [:format_message, :format_attachments, :at]
|
49
|
+
expect(subject.middleware).to eq [:format_message, :format_attachments, :at, :channels]
|
50
50
|
end
|
51
51
|
|
52
52
|
it "takes an array or a splat of args" do
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Slack::Notifier::PayloadMiddleware::Channels do
|
4
|
+
it "leaves string channels alone" do
|
5
|
+
subject = described_class.new(:notifier)
|
6
|
+
payload = { text: "hello", channel: "hodor" }
|
7
|
+
|
8
|
+
expect(subject.call(payload)).to eq text: "hello", channel: "hodor"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "splits payload into multiple if given an array of channels" do
|
12
|
+
subject = described_class.new(:notifier)
|
13
|
+
payload = { text: "hello", channel: %w[foo hodor] }
|
14
|
+
|
15
|
+
expect(subject.call(payload)).to eq [
|
16
|
+
{ text: "hello", channel: "foo" },
|
17
|
+
{ text: "hello", channel: "hodor" }
|
18
|
+
]
|
19
|
+
end
|
20
|
+
end
|
@@ -4,6 +4,10 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
|
4
4
|
double(call: 1)
|
5
5
|
end
|
6
6
|
|
7
|
+
let(:return_one_twice) do
|
8
|
+
double(call: [1, 1])
|
9
|
+
end
|
10
|
+
|
7
11
|
let(:return_two) do
|
8
12
|
double(call: 2)
|
9
13
|
end
|
@@ -18,6 +22,7 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
|
18
22
|
Slack::Notifier::PayloadMiddleware.send(:remove_instance_variable, :@registry)
|
19
23
|
|
20
24
|
Slack::Notifier::PayloadMiddleware.register return_one, :return_one
|
25
|
+
Slack::Notifier::PayloadMiddleware.register return_one_twice, :return_one_twice
|
21
26
|
Slack::Notifier::PayloadMiddleware.register return_two, :return_two
|
22
27
|
Slack::Notifier::PayloadMiddleware.register return_three, :return_three
|
23
28
|
end
|
@@ -87,7 +92,28 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
|
87
92
|
expect(return_three).to receive(:call).with(1)
|
88
93
|
expect(return_two).to receive(:call).with(3)
|
89
94
|
|
90
|
-
expect(subject.call(5)).to eq 2
|
95
|
+
expect(subject.call(5)).to eq [2]
|
96
|
+
end
|
97
|
+
|
98
|
+
it "allows any middleware to return an array but other's don't need special behavior" do
|
99
|
+
allow(return_one_twice).to receive(:new).and_return(return_one_twice)
|
100
|
+
allow(return_two).to receive(:new).and_return(return_two)
|
101
|
+
|
102
|
+
subject = described_class.new(:notifier)
|
103
|
+
subject.set(:return_one_twice, :return_two)
|
104
|
+
|
105
|
+
expect(subject.call(5)).to eq [2, 2]
|
106
|
+
end
|
107
|
+
|
108
|
+
it "handles multiple middleware splitting payload" do
|
109
|
+
allow(return_one_twice).to receive(:new).and_return(return_one_twice)
|
110
|
+
allow(return_two).to receive(:new).and_return(return_two)
|
111
|
+
|
112
|
+
subject = described_class.new(:notifier)
|
113
|
+
subject.set(:return_one_twice, :return_one_twice, :return_two)
|
114
|
+
|
115
|
+
expect(subject.call(5)).to eq [2, 2, 2, 2]
|
116
|
+
|
91
117
|
end
|
92
118
|
end
|
93
119
|
end
|
@@ -84,7 +84,7 @@ RSpec.describe Slack::Notifier do
|
|
84
84
|
|
85
85
|
expect(stack).to receive(:call)
|
86
86
|
.with(channel: "default", user: "rocket")
|
87
|
-
.and_return(test: "stack")
|
87
|
+
.and_return([test: "stack"])
|
88
88
|
|
89
89
|
expect(mock_http).to receive(:post).with(
|
90
90
|
URI.parse("http://example.com"),
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-notifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Sloan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: " A slim ruby wrapper for posting to slack webhooks "
|
14
14
|
email:
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- lib/slack-notifier/payload_middleware.rb
|
23
23
|
- lib/slack-notifier/payload_middleware/at.rb
|
24
24
|
- lib/slack-notifier/payload_middleware/base.rb
|
25
|
+
- lib/slack-notifier/payload_middleware/channels.rb
|
25
26
|
- lib/slack-notifier/payload_middleware/format_attachments.rb
|
26
27
|
- lib/slack-notifier/payload_middleware/format_message.rb
|
27
28
|
- lib/slack-notifier/payload_middleware/stack.rb
|
@@ -34,6 +35,7 @@ files:
|
|
34
35
|
- spec/lib/slack-notifier/config_spec.rb
|
35
36
|
- spec/lib/slack-notifier/payload_middleware/at_spec.rb
|
36
37
|
- spec/lib/slack-notifier/payload_middleware/base_spec.rb
|
38
|
+
- spec/lib/slack-notifier/payload_middleware/channels_spec.rb
|
37
39
|
- spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
|
38
40
|
- spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
|
39
41
|
- spec/lib/slack-notifier/payload_middleware/stack_spec.rb
|
@@ -72,6 +74,7 @@ test_files:
|
|
72
74
|
- spec/lib/slack-notifier/config_spec.rb
|
73
75
|
- spec/lib/slack-notifier/payload_middleware/at_spec.rb
|
74
76
|
- spec/lib/slack-notifier/payload_middleware/base_spec.rb
|
77
|
+
- spec/lib/slack-notifier/payload_middleware/channels_spec.rb
|
75
78
|
- spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
|
76
79
|
- spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
|
77
80
|
- spec/lib/slack-notifier/payload_middleware/stack_spec.rb
|