slack-notifier 2.3.0 → 2.4.0

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
- SHA1:
3
- metadata.gz: 68824ffcac979f733a61c7b5a77d0d21e8ed1dfe
4
- data.tar.gz: '08d9cee6b3a56238304f54756640794f443c78ec'
2
+ SHA256:
3
+ metadata.gz: 2b0deceb91f70d0812f01b82db70eacb8dbf01d4d1985ca02375ac13b36d829e
4
+ data.tar.gz: 5ec7d0eb36e91bbee6f510411646b4c57935aa661706b791e1dc0941fa66dbd9
5
5
  SHA512:
6
- metadata.gz: 65efd0c9618f26051b0f562f3ee46b5e6c697c479a6fe50d2014f77da3fdfb13c1ad6009f255cf6481cb4922d7bcc33b49ce8d4a65fb3372734f422f87189e7d
7
- data.tar.gz: 9b636e5fcd4d0bc0c815fb6d3c0625a9f1fd62a366bd24e4f0cd3fbe368eadc0405410ce0786a87b12ccd5fbd4f9968b1d89239cd955e9ed62f6407982a76276
6
+ metadata.gz: 0d42ebede5966444cf3101f9410f442e3fad0cdd3a9a7a25f67ff8c068c474bbd49f08d120070cca8652ffd351d68e99f90f648090b97469e3519761b167f513
7
+ data.tar.gz: 950d6e0ebc557de14b56b8ea5982e6057eb54530ec7b5b406f3c28ca22c3aaf26744c2ce6d69654b39bc75c8525d43204687b8adb97c331cf21c91f9629bd97f
@@ -1,15 +1,16 @@
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
- :format_message,
10
- :format_attachments,
11
- :at,
12
- :channels,
9
+ @middleware = %i[
10
+ format_message
11
+ format_attachments
12
+ at
13
+ channels
13
14
  ]
14
15
  end
15
16
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Slack
3
4
  class Notifier
4
5
  class PayloadMiddleware
@@ -1,16 +1,17 @@
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: [:html, :markdown]
9
+ options formats: %i[html markdown]
9
10
 
10
11
  def call payload={}
11
12
  payload = payload.dup
12
13
  attachments = payload.delete(:attachments)
13
- attachments = payload.delete("attachments") unless attachments
14
+ attachments ||= payload.delete("attachments")
14
15
 
15
16
  attachments = wrap_array(attachments).map do |attachment|
16
17
  ["text", :text].each do |key|
@@ -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: [:html, :markdown]
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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Slack
3
4
  class Notifier
4
5
  class PayloadMiddleware
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Slack
3
4
  class Notifier
4
5
  module Util
@@ -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,40 +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 = / <a (?:.*?) href=['"](.+?)['"] (?:.*?)> (.+?) <\/a> /x
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
- # http://rubular.com/r/guJbTK6x1f
10
- MARKDOWN_PATTERN = /\[ ([^\[\]]*?) \] \( ((https?:\/\/.*?) | (mailto:.*?)) \) /x
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: [:html, :markdown]
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 Style/GuardClause
43
+ # rubocop:disable Lint/RescueWithoutErrorClass
26
44
  def formatted
27
45
  return @orig unless @orig.respond_to?(:gsub)
28
46
 
29
47
  sub_markdown_links(sub_html_links(@orig))
30
48
  rescue => e
31
- if RUBY_VERSION < "2.1" && e.message.include?("invalid byte sequence")
32
- raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
33
- else
34
- raise e
35
- 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"
36
51
  end
37
- # rubocop:enable Style/GuardClause
52
+ # rubocop:enable Lint/RescueWithoutErrorClass
38
53
 
39
54
  private
40
55
 
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Slack
3
4
  class Notifier
4
- VERSION = "2.3.0".freeze # rubocop:disable Style/RedundantFreeze
5
+ VERSION = "2.4.0".freeze # rubocop:disable Style/RedundantFreeze
5
6
  end
6
7
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "uri"
3
4
  require "json"
4
5
 
@@ -43,7 +44,7 @@ module Slack
43
44
 
44
45
  params[:http_options] = payload.delete(:http_options) if payload.key?(:http_options)
45
46
 
46
- middleware.call(payload).each do |pld|
47
+ middleware.call(payload).map do |pld|
47
48
  params[:payload] = pld.to_json
48
49
  client.post endpoint, params
49
50
  end
@@ -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
@@ -54,15 +55,15 @@ RSpec.describe Slack::Notifier do
54
55
  text: "attachment message [hodor](http://winterfell.com)",
55
56
  fallback: "fallback message" } } =>
56
57
  { payload: { attachments: [{ color: "#000",
57
- text: "attachment message <http://winterfell.com|hodor>",
58
- fallback: "fallback message" }] } },
58
+ text: "attachment message <http://winterfell.com|hodor>",
59
+ fallback: "fallback message" }] } },
59
60
 
60
61
  { attachments: { color: "#000",
61
62
  text: nil,
62
63
  fallback: "fallback message" } } =>
63
64
  { payload: { attachments: [{ color: "#000",
64
- text: nil,
65
- fallback: "fallback message" }] } },
65
+ text: nil,
66
+ fallback: "fallback message" }] } },
66
67
 
67
68
  { text: "hello", http_options: { timeout: 5 } } =>
68
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)
@@ -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 [:format_message, :format_attachments, :at, :channels]
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 [:layer, :two]
56
+ expect(subject.middleware).to eq %i[layer two]
57
57
 
58
- subject.middleware [:one, :layer]
59
- expect(subject.middleware).to eq [:one, :layer]
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: [:john, :ken, :here] }
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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  RSpec.describe Slack::Notifier::PayloadMiddleware::Base do
3
4
  before(:each) do
4
5
  @registry_backup = Slack::Notifier::PayloadMiddleware.registry.dup
@@ -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,30 +11,30 @@ 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: [:html, :markdown])
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: [:html, :markdown])
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: [:html, :markdown])
26
+ .with("hello", formats: %i[html markdown])
26
27
  subject.call(attachments: { text: "hello" })
27
28
  end
28
29
 
29
30
  it "wraps attachment into array if given as a single hash" do
30
- params = {
31
+ params = {
31
32
  attachments: { text: "hello" }
32
33
  }
33
34
  payload = {
34
35
  attachments: [{ text: "hello" }]
35
36
  }
36
- subject = described_class.new(:notifier);
37
+ subject = described_class.new(:notifier)
37
38
 
38
39
  expect(subject.call(params)).to eq payload
39
40
  end
@@ -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: [:html, :markdown])
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,4 +1,5 @@
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)
@@ -113,7 +114,6 @@ RSpec.describe Slack::Notifier::PayloadMiddleware::Stack do
113
114
  subject.set(:return_one_twice, :return_one_twice, :return_two)
114
115
 
115
116
  expect(subject.call(5)).to eq [2, 2, 2, 2]
116
-
117
117
  end
118
118
  end
119
119
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  RSpec.describe Slack::Notifier::PayloadMiddleware do
3
4
  before(:each) do
4
5
  @registry_backup = described_class.registry.dup
@@ -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
- .and_return(http_post_double)
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 'doesn\'t replace valid Japanese' do
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])
@@ -91,7 +91,8 @@ RSpec.describe Slack::Notifier do
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.3.0
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: 2017-08-02 00:00:00.000000000 Z
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:
@@ -48,7 +48,7 @@ homepage: http://github.com/stevenosloan/slack-notifier
48
48
  licenses:
49
49
  - MIT
50
50
  metadata: {}
51
- post_install_message:
51
+ post_install_message:
52
52
  rdoc_options: []
53
53
  require_paths:
54
54
  - lib
@@ -63,9 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubyforge_project:
67
- rubygems_version: 2.5.2
68
- signing_key:
66
+ rubygems_version: 3.2.15
67
+ signing_key:
69
68
  specification_version: 4
70
69
  summary: A slim ruby wrapper for posting to slack webhooks
71
70
  test_files: