slack-notifier 2.2.1 → 2.4.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 +5 -5
- data/lib/slack-notifier.rb +5 -2
- data/lib/slack-notifier/config.rb +7 -1
- data/lib/slack-notifier/payload_middleware.rb +2 -0
- data/lib/slack-notifier/payload_middleware/base.rb +1 -0
- data/lib/slack-notifier/payload_middleware/channels.rb +21 -0
- data/lib/slack-notifier/payload_middleware/format_attachments.rb +10 -3
- data/lib/slack-notifier/payload_middleware/format_message.rb +2 -1
- data/lib/slack-notifier/payload_middleware/stack.rb +17 -2
- data/lib/slack-notifier/util/escape.rb +1 -0
- data/lib/slack-notifier/util/http_client.rb +2 -1
- data/lib/slack-notifier/util/link_formatter.rb +29 -12
- data/lib/slack-notifier/version.rb +2 -1
- data/spec/end_to_end_spec.rb +14 -3
- data/spec/integration/ping_integration_test.rb +2 -0
- data/spec/lib/slack-notifier/config_spec.rb +4 -4
- data/spec/lib/slack-notifier/payload_middleware/at_spec.rb +1 -1
- data/spec/lib/slack-notifier/payload_middleware/base_spec.rb +1 -0
- data/spec/lib/slack-notifier/payload_middleware/channels_spec.rb +20 -0
- data/spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb +16 -3
- data/spec/lib/slack-notifier/payload_middleware/format_message_spec.rb +2 -1
- data/spec/lib/slack-notifier/payload_middleware/stack_spec.rb +27 -1
- data/spec/lib/slack-notifier/payload_middleware_spec.rb +1 -0
- data/spec/lib/slack-notifier/util/http_client_spec.rb +2 -1
- data/spec/lib/slack-notifier/util/link_formatter_spec.rb +75 -1
- data/spec/lib/slack-notifier_spec.rb +3 -2
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b0deceb91f70d0812f01b82db70eacb8dbf01d4d1985ca02375ac13b36d829e
|
4
|
+
data.tar.gz: 5ec7d0eb36e91bbee6f510411646b4c57935aa661706b791e1dc0941fa66dbd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d42ebede5966444cf3101f9410f442e3fad0cdd3a9a7a25f67ff8c068c474bbd49f08d120070cca8652ffd351d68e99f90f648090b97469e3519761b167f513
|
7
|
+
data.tar.gz: 950d6e0ebc557de14b56b8ea5982e6057eb54530ec7b5b406f3c28ca22c3aaf26744c2ce6d69654b39bc75c8525d43204687b8adb97c331cf21c91f9629bd97f
|
data/lib/slack-notifier.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require "uri"
|
3
4
|
require "json"
|
4
5
|
|
@@ -42,9 +43,11 @@ module Slack
|
|
42
43
|
payload = config.defaults.merge(payload)
|
43
44
|
|
44
45
|
params[:http_options] = payload.delete(:http_options) if payload.key?(:http_options)
|
45
|
-
params[:payload] = middleware.call(payload).to_json
|
46
46
|
|
47
|
-
|
47
|
+
middleware.call(payload).map do |pld|
|
48
|
+
params[:payload] = pld.to_json
|
49
|
+
client.post endpoint, params
|
50
|
+
end
|
48
51
|
end
|
49
52
|
|
50
53
|
private
|
@@ -1,11 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
class Config
|
5
6
|
def initialize
|
6
7
|
@http_client = Util::HTTPClient
|
7
8
|
@defaults = {}
|
8
|
-
@middleware = [
|
9
|
+
@middleware = %i[
|
10
|
+
format_message
|
11
|
+
format_attachments
|
12
|
+
at
|
13
|
+
channels
|
14
|
+
]
|
9
15
|
end
|
10
16
|
|
11
17
|
def http_client client=nil
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
class PayloadMiddleware
|
@@ -20,3 +21,4 @@ require_relative "payload_middleware/base"
|
|
20
21
|
require_relative "payload_middleware/format_message"
|
21
22
|
require_relative "payload_middleware/format_attachments"
|
22
23
|
require_relative "payload_middleware/at"
|
24
|
+
require_relative "payload_middleware/channels"
|
@@ -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
|
@@ -1,22 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
class PayloadMiddleware
|
5
6
|
class FormatAttachments < Base
|
6
7
|
middleware_name :format_attachments
|
7
8
|
|
8
|
-
options formats: [
|
9
|
+
options formats: %i[html markdown]
|
9
10
|
|
10
11
|
def call payload={}
|
11
|
-
|
12
|
-
|
12
|
+
payload = payload.dup
|
13
|
+
attachments = payload.delete(:attachments)
|
14
|
+
attachments ||= payload.delete("attachments")
|
15
|
+
|
16
|
+
attachments = wrap_array(attachments).map do |attachment|
|
13
17
|
["text", :text].each do |key|
|
14
18
|
if attachment.key?(key)
|
15
19
|
attachment[key] = Util::LinkFormatter.format(attachment[key], options)
|
16
20
|
end
|
17
21
|
end
|
22
|
+
|
23
|
+
attachment
|
18
24
|
end
|
19
25
|
|
26
|
+
payload[:attachments] = attachments if attachments && !attachments.empty?
|
20
27
|
payload
|
21
28
|
end
|
22
29
|
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
class PayloadMiddleware
|
5
6
|
class FormatMessage < Base
|
6
7
|
middleware_name :format_message
|
7
8
|
|
8
|
-
options formats: [
|
9
|
+
options formats: %i[html markdown]
|
9
10
|
|
10
11
|
def call payload={}
|
11
12
|
return payload unless payload[:text]
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
class PayloadMiddleware
|
@@ -25,10 +26,24 @@ module Slack
|
|
25
26
|
end
|
26
27
|
|
27
28
|
def call payload={}
|
28
|
-
stack.inject payload do |pld, middleware|
|
29
|
-
|
29
|
+
result = stack.inject payload do |pld, middleware|
|
30
|
+
as_array(pld).flat_map do |p|
|
31
|
+
middleware.call(p)
|
32
|
+
end
|
30
33
|
end
|
34
|
+
|
35
|
+
as_array(result)
|
31
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def as_array args
|
41
|
+
if args.respond_to?(:to_ary)
|
42
|
+
args.to_ary
|
43
|
+
else
|
44
|
+
[args]
|
45
|
+
end
|
46
|
+
end
|
32
47
|
end
|
33
48
|
end
|
34
49
|
end
|
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
require "net/http"
|
4
4
|
|
5
|
-
|
6
5
|
module Slack
|
7
6
|
class Notifier
|
8
7
|
class APIError < StandardError; end
|
@@ -23,6 +22,7 @@ module Slack
|
|
23
22
|
@params = params
|
24
23
|
end
|
25
24
|
|
25
|
+
# rubocop:disable Layout/IndentHeredoc
|
26
26
|
def call
|
27
27
|
http_obj.request(request_obj).tap do |response|
|
28
28
|
unless response.is_a?(Net::HTTPSuccess)
|
@@ -33,6 +33,7 @@ MSG
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
36
|
+
# rubocop:enable Layout/IndentHeredoc
|
36
37
|
|
37
38
|
private
|
38
39
|
|
@@ -1,38 +1,55 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Slack
|
3
4
|
class Notifier
|
4
5
|
module Util
|
5
6
|
class LinkFormatter
|
6
7
|
# http://rubular.com/r/19cNXW5qbH
|
7
|
-
HTML_PATTERN =
|
8
|
+
HTML_PATTERN = %r{
|
9
|
+
<a
|
10
|
+
(?:.*?)
|
11
|
+
href=['"](.+?)['"]
|
12
|
+
(?:.*?)>
|
13
|
+
(.+?)
|
14
|
+
</a>
|
15
|
+
}x
|
16
|
+
|
17
|
+
# the path portion of a url can contain these characters
|
18
|
+
VALID_PATH_CHARS = '\w\-\.\~\/\?\#\='
|
8
19
|
|
9
|
-
#
|
10
|
-
|
20
|
+
# Attempt at only matching pairs of parens per
|
21
|
+
# the markdown spec http://spec.commonmark.org/0.27/#links
|
22
|
+
#
|
23
|
+
# http://rubular.com/r/y107aevxqT
|
24
|
+
MARKDOWN_PATTERN = %r{
|
25
|
+
\[ ([^\[\]]*?) \]
|
26
|
+
\( ((https?://.*?) | (mailto:.*?)) \)
|
27
|
+
(?! [#{VALID_PATH_CHARS}]* \) )
|
28
|
+
}x
|
11
29
|
|
12
30
|
class << self
|
13
31
|
def format string, opts={}
|
14
|
-
LinkFormatter.new(string, opts).formatted
|
32
|
+
LinkFormatter.new(string, **opts).formatted
|
15
33
|
end
|
16
34
|
end
|
17
35
|
|
18
36
|
attr_reader :formats
|
19
37
|
|
20
|
-
def initialize string, formats: [
|
38
|
+
def initialize string, formats: %i[html markdown]
|
21
39
|
@formats = formats
|
22
40
|
@orig = string.respond_to?(:scrub) ? string.scrub : string
|
23
41
|
end
|
24
42
|
|
25
|
-
# rubocop:disable
|
43
|
+
# rubocop:disable Lint/RescueWithoutErrorClass
|
26
44
|
def formatted
|
45
|
+
return @orig unless @orig.respond_to?(:gsub)
|
46
|
+
|
27
47
|
sub_markdown_links(sub_html_links(@orig))
|
28
48
|
rescue => e
|
29
|
-
|
30
|
-
|
31
|
-
else
|
32
|
-
raise e
|
33
|
-
end
|
49
|
+
raise e unless RUBY_VERSION < "2.1" && e.message.include?("invalid byte sequence")
|
50
|
+
raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
|
34
51
|
end
|
35
|
-
# rubocop:enable
|
52
|
+
# rubocop:enable Lint/RescueWithoutErrorClass
|
36
53
|
|
37
54
|
private
|
38
55
|
|
data/spec/end_to_end_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# encoding: utf-8
|
3
|
+
|
3
4
|
require "spec_helper"
|
4
5
|
|
5
6
|
RSpec.describe Slack::Notifier do
|
@@ -31,6 +32,9 @@ RSpec.describe Slack::Notifier do
|
|
31
32
|
{ text: "hello", channel: "hodor" } =>
|
32
33
|
{ payload: { text: "hello", channel: "hodor" } },
|
33
34
|
|
35
|
+
{ text: nil, attachments: [{ text: "attachment message" }] } =>
|
36
|
+
{ payload: { text: nil, attachments: [{ text: "attachment message" }] } },
|
37
|
+
|
34
38
|
{ text: "the message", channel: "foo", attachments: [{ color: "#000",
|
35
39
|
text: "attachment message",
|
36
40
|
fallback: "fallback message" }] } =>
|
@@ -50,9 +54,16 @@ RSpec.describe Slack::Notifier do
|
|
50
54
|
{ attachments: { color: "#000",
|
51
55
|
text: "attachment message [hodor](http://winterfell.com)",
|
52
56
|
fallback: "fallback message" } } =>
|
53
|
-
{ payload: { attachments: { color: "#000",
|
54
|
-
|
55
|
-
|
57
|
+
{ payload: { attachments: [{ color: "#000",
|
58
|
+
text: "attachment message <http://winterfell.com|hodor>",
|
59
|
+
fallback: "fallback message" }] } },
|
60
|
+
|
61
|
+
{ attachments: { color: "#000",
|
62
|
+
text: nil,
|
63
|
+
fallback: "fallback message" } } =>
|
64
|
+
{ payload: { attachments: [{ color: "#000",
|
65
|
+
text: nil,
|
66
|
+
fallback: "fallback message" }] } },
|
56
67
|
|
57
68
|
{ text: "hello", http_options: { timeout: 5 } } =>
|
58
69
|
{ http_options: { timeout: 5 }, payload: { text: "hello" } }
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# encoding: utf-8
|
3
|
+
|
3
4
|
require_relative "../../lib/slack-notifier"
|
4
5
|
|
5
6
|
ruby = if defined?(JRUBY_VERSION)
|
@@ -10,5 +11,6 @@ end
|
|
10
11
|
puts "testing with #{ruby}"
|
11
12
|
|
12
13
|
notifier = Slack::Notifier.new ENV["SLACK_WEBHOOK_URL"], username: "notifier"
|
14
|
+
notifier.ping "hello", channel: ["#general", "#random"]
|
13
15
|
notifier.ping "hello/こんにちは from notifier test script on #{ruby}\225"
|
14
16
|
notifier.ping attachments: [{ color: "#1BF5AF", fallback: "fallback", text: "attachment" }]
|
@@ -46,17 +46,17 @@ 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 [
|
49
|
+
expect(subject.middleware).to eq %i[format_message format_attachments at channels]
|
50
50
|
end
|
51
51
|
|
52
52
|
it "takes an array or a splat of args" do
|
53
53
|
subject = described_class.new
|
54
54
|
|
55
55
|
subject.middleware :layer, :two
|
56
|
-
expect(subject.middleware).to eq [
|
56
|
+
expect(subject.middleware).to eq %i[layer two]
|
57
57
|
|
58
|
-
subject.middleware [
|
59
|
-
expect(subject.middleware).to eq [
|
58
|
+
subject.middleware %i[one layer]
|
59
|
+
expect(subject.middleware).to eq %i[one layer]
|
60
60
|
end
|
61
61
|
|
62
62
|
it "allows passing options to middleware stack" do
|
@@ -3,7 +3,7 @@
|
|
3
3
|
RSpec.describe Slack::Notifier::PayloadMiddleware::At do
|
4
4
|
it "can handle array at" do
|
5
5
|
subject = described_class.new(:notifier)
|
6
|
-
payload = { text: "hello", at: [
|
6
|
+
payload = { text: "hello", at: %i[john ken here] }
|
7
7
|
|
8
8
|
expect(subject.call(payload)).to eq text: "<@john> <@ken> <!here> hello"
|
9
9
|
end
|
@@ -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
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
RSpec.describe Slack::Notifier::PayloadMiddleware::FormatAttachments do
|
3
4
|
it "passes the text of attachments through linkformatter with options[:formats]" do
|
4
5
|
subject = described_class.new(:notifier, formats: [:html])
|
@@ -10,22 +11,34 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::FormatAttachments do
|
|
10
11
|
it "searches through string or symbol keys" do
|
11
12
|
subject = described_class.new(:notifier)
|
12
13
|
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
|
13
|
-
.with("hello", formats: [
|
14
|
+
.with("hello", formats: %i[html markdown])
|
14
15
|
subject.call("attachments" => [{ "text" => "hello" }])
|
15
16
|
|
16
17
|
subject = described_class.new(:notifier)
|
17
18
|
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
|
18
|
-
.with("hello", formats: [
|
19
|
+
.with("hello", formats: %i[html markdown])
|
19
20
|
subject.call(attachments: [{ text: "hello" }])
|
20
21
|
end
|
21
22
|
|
22
23
|
it "can handle a single attachment" do
|
23
24
|
subject = described_class.new(:notifier)
|
24
25
|
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
|
25
|
-
.with("hello", formats: [
|
26
|
+
.with("hello", formats: %i[html markdown])
|
26
27
|
subject.call(attachments: { text: "hello" })
|
27
28
|
end
|
28
29
|
|
30
|
+
it "wraps attachment into array if given as a single hash" do
|
31
|
+
params = {
|
32
|
+
attachments: { text: "hello" }
|
33
|
+
}
|
34
|
+
payload = {
|
35
|
+
attachments: [{ text: "hello" }]
|
36
|
+
}
|
37
|
+
subject = described_class.new(:notifier)
|
38
|
+
|
39
|
+
expect(subject.call(params)).to eq payload
|
40
|
+
end
|
41
|
+
|
29
42
|
it "returns the payload unmodified if not :attachments key" do
|
30
43
|
payload = { foo: :bar }
|
31
44
|
subject = described_class.new(:notifier)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
RSpec.describe Slack::Notifier::PayloadMiddleware::FormatMessage do
|
3
4
|
it "passes the text through linkformatter with options[:formats]" do
|
4
5
|
subject = described_class.new(:notifier, formats: [:html])
|
@@ -8,7 +9,7 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::FormatMessage do
|
|
8
9
|
|
9
10
|
subject = described_class.new(:notifier)
|
10
11
|
expect(Slack::Notifier::Util::LinkFormatter).to receive(:format)
|
11
|
-
.with("hello", formats: [
|
12
|
+
.with("hello", formats: %i[html markdown])
|
12
13
|
subject.call(text: "hello")
|
13
14
|
|
14
15
|
subject = described_class.new(:notifier, formats: [:markdown])
|
@@ -1,9 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
3
4
|
let(:return_one) do
|
4
5
|
double(call: 1)
|
5
6
|
end
|
6
7
|
|
8
|
+
let(:return_one_twice) do
|
9
|
+
double(call: [1, 1])
|
10
|
+
end
|
11
|
+
|
7
12
|
let(:return_two) do
|
8
13
|
double(call: 2)
|
9
14
|
end
|
@@ -18,6 +23,7 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
|
18
23
|
Slack::Notifier::PayloadMiddleware.send(:remove_instance_variable, :@registry)
|
19
24
|
|
20
25
|
Slack::Notifier::PayloadMiddleware.register return_one, :return_one
|
26
|
+
Slack::Notifier::PayloadMiddleware.register return_one_twice, :return_one_twice
|
21
27
|
Slack::Notifier::PayloadMiddleware.register return_two, :return_two
|
22
28
|
Slack::Notifier::PayloadMiddleware.register return_three, :return_three
|
23
29
|
end
|
@@ -87,7 +93,27 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
|
|
87
93
|
expect(return_three).to receive(:call).with(1)
|
88
94
|
expect(return_two).to receive(:call).with(3)
|
89
95
|
|
90
|
-
expect(subject.call(5)).to eq 2
|
96
|
+
expect(subject.call(5)).to eq [2]
|
97
|
+
end
|
98
|
+
|
99
|
+
it "allows any middleware to return an array but other's don't need special behavior" do
|
100
|
+
allow(return_one_twice).to receive(:new).and_return(return_one_twice)
|
101
|
+
allow(return_two).to receive(:new).and_return(return_two)
|
102
|
+
|
103
|
+
subject = described_class.new(:notifier)
|
104
|
+
subject.set(:return_one_twice, :return_two)
|
105
|
+
|
106
|
+
expect(subject.call(5)).to eq [2, 2]
|
107
|
+
end
|
108
|
+
|
109
|
+
it "handles multiple middleware splitting payload" do
|
110
|
+
allow(return_one_twice).to receive(:new).and_return(return_one_twice)
|
111
|
+
allow(return_two).to receive(:new).and_return(return_two)
|
112
|
+
|
113
|
+
subject = described_class.new(:notifier)
|
114
|
+
subject.set(:return_one_twice, :return_one_twice, :return_two)
|
115
|
+
|
116
|
+
expect(subject.call(5)).to eq [2, 2, 2, 2]
|
91
117
|
end
|
92
118
|
end
|
93
119
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
RSpec.describe Slack::Notifier::Util::HTTPClient do
|
3
4
|
describe "::post" do
|
4
5
|
it "initializes Util::HTTPClient with the given uri and params then calls" do
|
@@ -6,7 +7,7 @@ RSpec.describe Slack::Notifier::Util::HTTPClient do
|
|
6
7
|
|
7
8
|
expect(described_class)
|
8
9
|
.to receive(:new).with("uri", "params")
|
9
|
-
|
10
|
+
.and_return(http_post_double)
|
10
11
|
expect(http_post_double).to receive(:call)
|
11
12
|
|
12
13
|
described_class.post "uri", "params"
|
@@ -1,7 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
# encoding: utf-8
|
3
|
+
|
3
4
|
# rubocop:disable Metrics/LineLength
|
4
5
|
RSpec.describe Slack::Notifier::Util::LinkFormatter do
|
6
|
+
describe "initialize & formatted" do
|
7
|
+
it "can be initialized without format args" do
|
8
|
+
subject = described_class.new("Hello World")
|
9
|
+
expect(subject.formatted()).to eq("Hello World")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can be initialized with format args" do
|
13
|
+
subject = described_class.new("Hello World", formats: [:html])
|
14
|
+
expect(subject.formatted()).to eq("Hello World")
|
15
|
+
end
|
16
|
+
end
|
5
17
|
describe "::format" do
|
6
18
|
it "formats html links" do
|
7
19
|
formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a>.")
|
@@ -56,7 +68,7 @@ RSpec.describe Slack::Notifier::Util::LinkFormatter do
|
|
56
68
|
end
|
57
69
|
end
|
58
70
|
|
59
|
-
it
|
71
|
+
it "doesn't replace valid Japanese" do
|
60
72
|
formatted = described_class.format("こんにちは")
|
61
73
|
expect(formatted).to eq "こんにちは"
|
62
74
|
end
|
@@ -71,6 +83,68 @@ RSpec.describe Slack::Notifier::Util::LinkFormatter do
|
|
71
83
|
expect(formatted).to eq "<mailto:john@example.com|John>"
|
72
84
|
end
|
73
85
|
|
86
|
+
it "handles links with trailing parentheses" do
|
87
|
+
formatted = described_class.format("Hello World, enjoy [foo(bar)](http://example.com/foo(bar))<a href='http://example.com/bar(foo)'>bar(foo)</a>")
|
88
|
+
expect(formatted).to include("http://example.com/foo(bar)|foo(bar)")
|
89
|
+
expect(formatted).to include("http://example.com/bar(foo)|bar(foo)")
|
90
|
+
end
|
91
|
+
|
92
|
+
it "formats a number of differently formatted links" do
|
93
|
+
input_output = {
|
94
|
+
"Hello World, enjoy [this](http://example.com)." =>
|
95
|
+
"Hello World, enjoy <http://example.com|this>.",
|
96
|
+
|
97
|
+
"Hello World, enjoy [[this](http://example.com) in brackets]." =>
|
98
|
+
"Hello World, enjoy [<http://example.com|this> in brackets].",
|
99
|
+
|
100
|
+
"Hello World, enjoy ([this](http://example.com) in parens)." =>
|
101
|
+
"Hello World, enjoy (<http://example.com|this> in parens).",
|
102
|
+
|
103
|
+
"Hello World, enjoy [](http://example.com)." =>
|
104
|
+
"Hello World, enjoy <http://example.com>.",
|
105
|
+
|
106
|
+
"Hello World, enjoy [link with query](http://example.com?foo=bar)." =>
|
107
|
+
"Hello World, enjoy <http://example.com?foo=bar|link with query>.",
|
108
|
+
|
109
|
+
"Hello World, enjoy [link with fragment](http://example.com/#foo-bar)." =>
|
110
|
+
"Hello World, enjoy <http://example.com/#foo-bar|link with fragment>.",
|
111
|
+
|
112
|
+
"Hello World, enjoy [link with parens](http://example.com/foo(bar)/baz)." =>
|
113
|
+
"Hello World, enjoy <http://example.com/foo(bar)/baz|link with parens>.",
|
114
|
+
|
115
|
+
"Hello World, enjoy [link with query](http://example.com/(parens)?foo=bar)." =>
|
116
|
+
"Hello World, enjoy <http://example.com/(parens)?foo=bar|link with query>.",
|
117
|
+
|
118
|
+
"Hello World, enjoy [link with parens](http://example.com/baz?bang=foo(bar))." =>
|
119
|
+
"Hello World, enjoy <http://example.com/baz?bang=foo(bar)|link with parens>.",
|
120
|
+
|
121
|
+
"Hello World, enjoy [link with fragment](http://example.com/(parens)#foo-bar)." =>
|
122
|
+
"Hello World, enjoy <http://example.com/(parens)#foo-bar|link with fragment>.",
|
123
|
+
|
124
|
+
"Hello World, enjoy [link with fragment](http://example.com/#foo-bar=(baz))." =>
|
125
|
+
"Hello World, enjoy <http://example.com/#foo-bar=(baz)|link with fragment>.",
|
126
|
+
|
127
|
+
"Hello World, enjoy [this](http://example.com?foo=bar)[this2](http://example2.com)." =>
|
128
|
+
"Hello World, enjoy <http://example.com?foo=bar|this><http://example2.com|this2>.",
|
129
|
+
|
130
|
+
"Hello World, enjoy [this](http://example.com?foo=bar) [this2](http://example2.com/#fragment)." =>
|
131
|
+
"Hello World, enjoy <http://example.com?foo=bar|this> <http://example2.com/#fragment|this2>.",
|
132
|
+
|
133
|
+
"Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>." =>
|
134
|
+
"Hello World, enjoy <http://example.com|this><http://example2.com|this2>.",
|
135
|
+
|
136
|
+
"Hello world, [John](mailto:john@example.com)." =>
|
137
|
+
"Hello world, <mailto:john@example.com|John>.",
|
138
|
+
|
139
|
+
"Hello World, enjoy [foo(bar)](http://example.com/foo(bar))<a href='http://example.com/bar(foo)'>bar(foo)</a>" =>
|
140
|
+
"Hello World, enjoy <http://example.com/foo(bar)|foo(bar)><http://example.com/bar(foo)|bar(foo)>"
|
141
|
+
}
|
142
|
+
|
143
|
+
input_output.each do |input, output|
|
144
|
+
expect(described_class.format(input)).to eq output
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
74
148
|
context "with a configured stack" do
|
75
149
|
it "only formats html if html is the only item in formats" do
|
76
150
|
formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.", formats: [:html])
|
@@ -84,14 +84,15 @@ 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"),
|
91
91
|
payload: '{"test":"stack"}'
|
92
92
|
)
|
93
93
|
|
94
|
-
subject.post
|
94
|
+
responses = subject.post
|
95
|
+
expect(responses).to eq([:posted])
|
95
96
|
end
|
96
97
|
end
|
97
98
|
end
|
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.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Sloan
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-07 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
|
@@ -46,7 +48,7 @@ homepage: http://github.com/stevenosloan/slack-notifier
|
|
46
48
|
licenses:
|
47
49
|
- MIT
|
48
50
|
metadata: {}
|
49
|
-
post_install_message:
|
51
|
+
post_install_message:
|
50
52
|
rdoc_options: []
|
51
53
|
require_paths:
|
52
54
|
- lib
|
@@ -61,9 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
63
|
- !ruby/object:Gem::Version
|
62
64
|
version: '0'
|
63
65
|
requirements: []
|
64
|
-
|
65
|
-
|
66
|
-
signing_key:
|
66
|
+
rubygems_version: 3.2.15
|
67
|
+
signing_key:
|
67
68
|
specification_version: 4
|
68
69
|
summary: A slim ruby wrapper for posting to slack webhooks
|
69
70
|
test_files:
|
@@ -72,6 +73,7 @@ test_files:
|
|
72
73
|
- spec/lib/slack-notifier/config_spec.rb
|
73
74
|
- spec/lib/slack-notifier/payload_middleware/at_spec.rb
|
74
75
|
- spec/lib/slack-notifier/payload_middleware/base_spec.rb
|
76
|
+
- spec/lib/slack-notifier/payload_middleware/channels_spec.rb
|
75
77
|
- spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
|
76
78
|
- spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
|
77
79
|
- spec/lib/slack-notifier/payload_middleware/stack_spec.rb
|