slack-notifier 1.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.
Files changed (33) hide show
  1. checksums.yaml +5 -5
  2. data/lib/slack-notifier/config.rb +43 -0
  3. data/lib/slack-notifier/payload_middleware/at.rb +36 -0
  4. data/lib/slack-notifier/payload_middleware/base.rb +34 -0
  5. data/lib/slack-notifier/payload_middleware/channels.rb +21 -0
  6. data/lib/slack-notifier/payload_middleware/format_attachments.rb +44 -0
  7. data/lib/slack-notifier/payload_middleware/format_message.rb +20 -0
  8. data/lib/slack-notifier/payload_middleware/stack.rb +50 -0
  9. data/lib/slack-notifier/payload_middleware.rb +24 -0
  10. data/lib/slack-notifier/util/escape.rb +16 -0
  11. data/lib/slack-notifier/util/http_client.rb +64 -0
  12. data/lib/slack-notifier/util/link_formatter.rb +80 -0
  13. data/lib/slack-notifier/version.rb +3 -1
  14. data/lib/slack-notifier.rb +40 -31
  15. data/spec/end_to_end_spec.rb +95 -0
  16. data/spec/integration/ping_integration_test.rb +14 -4
  17. data/spec/lib/slack-notifier/config_spec.rb +71 -0
  18. data/spec/lib/slack-notifier/payload_middleware/at_spec.rb +25 -0
  19. data/spec/lib/slack-notifier/payload_middleware/base_spec.rb +76 -0
  20. data/spec/lib/slack-notifier/payload_middleware/channels_spec.rb +20 -0
  21. data/spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb +48 -0
  22. data/spec/lib/slack-notifier/payload_middleware/format_message_spec.rb +27 -0
  23. data/spec/lib/slack-notifier/payload_middleware/stack_spec.rb +119 -0
  24. data/spec/lib/slack-notifier/payload_middleware_spec.rb +33 -0
  25. data/spec/lib/slack-notifier/util/http_client_spec.rb +55 -0
  26. data/spec/lib/slack-notifier/util/link_formatter_spec.rb +163 -0
  27. data/spec/lib/slack-notifier_spec.rb +65 -79
  28. data/spec/spec_helper.rb +20 -5
  29. metadata +42 -16
  30. data/lib/slack-notifier/default_http_client.rb +0 -51
  31. data/lib/slack-notifier/link_formatter.rb +0 -60
  32. data/spec/lib/slack-notifier/default_http_client_spec.rb +0 -37
  33. data/spec/lib/slack-notifier/link_formatter_spec.rb +0 -63
@@ -1,112 +1,98 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe Slack::Notifier do
4
- subject { described_class.new 'http://example.com' }
3
+ RSpec.describe Slack::Notifier do
4
+ let(:mock_http) do
5
+ class_double("Slack::Notifier::Util::HTTPClient", post: :posted)
6
+ end
7
+
8
+ subject { described_class.new "http://example.com", http_client: mock_http }
5
9
 
6
10
  describe "#initialize" do
7
11
  it "sets the given hook_url to the endpoint URI" do
8
- expect( subject.endpoint ).to eq URI.parse 'http://example.com'
12
+ expect(subject.endpoint).to eq URI.parse("http://example.com")
9
13
  end
10
14
 
11
15
  it "sets the default_payload options" do
12
- subject = described_class.new 'http://example.com', channel: 'foo'
13
- expect( subject.channel ).to eq 'foo'
16
+ subject = described_class.new "http://example.com", channel: "foo"
17
+ expect(subject.config.defaults[:channel]).to eq "foo"
14
18
  end
15
19
 
16
20
  it "sets a custom http client" do
17
- client = double("CustomClient")
18
- subject = described_class.new 'http://example.com', http_client: client
19
- expect( subject.http_client ).to eq client
20
- end
21
- end
22
-
23
- describe "#ping" do
24
- before :each do
25
- allow( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
21
+ subject = described_class.new "http://example.com", http_client: mock_http
22
+ expect(subject.config.http_client).to eq mock_http
26
23
  end
27
24
 
28
- it "passes the message through LinkFormatter" do
29
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
30
- .with("the message")
25
+ describe "when given a block" do
26
+ it "yields the config object" do
27
+ test_double = double("Slack::Notifier::Config", defaults: {}, middleware: [])
28
+ allow_any_instance_of(Slack::Notifier).to receive(:config).and_return(test_double)
31
29
 
32
- described_class.new('http://example.com').ping "the message", channel: 'foo'
33
- end
34
-
35
- context "with a default channel set" do
30
+ expect(test_double).to receive(:test_init_method).with("foo")
36
31
 
37
- before :each do
38
- @endpoint_double = instance_double "URI::HTTP"
39
- allow( URI ).to receive(:parse)
40
- .and_return(@endpoint_double)
41
- subject.channel = '#default'
32
+ described_class.new "http://example.com" do
33
+ test_init_method "foo"
34
+ end
42
35
  end
36
+ end
37
+ end
43
38
 
44
- it "does not require a channel to ping" do
45
- expect{
46
- subject.ping "the message"
47
- }.not_to raise_error
48
- end
39
+ describe "#ping" do
40
+ it "calls #post with the message as the text key in #post" do
41
+ subject = described_class.new "http://example.com"
42
+ expect(subject).to receive(:post).with text: "message"
49
43
 
50
- it "uses default channel" do
51
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
52
- .with @endpoint_double,
53
- payload: '{"channel":"#default","text":"the message"}'
44
+ subject.ping "message"
45
+ end
46
+ end
54
47
 
55
- subject.ping "the message"
48
+ describe "#post" do
49
+ def notifier_with_defaults
50
+ mock_client = mock_http
51
+ described_class.new "http://example.com" do
52
+ defaults channel: "default",
53
+ user: "rocket"
54
+ http_client mock_client
56
55
  end
56
+ end
57
57
 
58
- it "allows override channel to be set" do
59
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
60
- .with @endpoint_double,
61
- payload: '{"channel":"new","text":"the message"}'
58
+ it "uses the defaults set on initialization" do
59
+ subject = notifier_with_defaults
62
60
 
63
- subject.ping "the message", channel: "new"
64
- end
61
+ expect(mock_http).to receive(:post).with(
62
+ URI.parse("http://example.com"),
63
+ payload: '{"channel":"default","user":"rocket","text":"hello"}'
64
+ )
65
65
 
66
+ subject.post text: "hello"
66
67
  end
67
68
 
68
- context "with default webhook" do
69
- it "posts with the correct endpoint & data" do
70
- @endpoint_double = instance_double "URI::HTTP"
71
- allow( URI ).to receive(:parse)
72
- .with("http://example.com")
73
- .and_return(@endpoint_double)
69
+ it "allows overriding the set defaults" do
70
+ subject = notifier_with_defaults
74
71
 
75
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
76
- .with @endpoint_double,
77
- payload: '{"channel":"channel","text":"the message"}'
72
+ expect(mock_http).to receive(:post).with(
73
+ URI.parse("http://example.com"),
74
+ payload: '{"channel":"new","user":"ship","text":"hello"}'
75
+ )
78
76
 
79
- described_class.new("http://example.com").ping "the message", channel: "channel"
80
- end
77
+ subject.post text: "hello", channel: "new", user: "ship"
81
78
  end
82
79
 
83
- context "with a custom http_client set" do
84
- it "uses it" do
85
- endpoint_double = instance_double "URI::HTTP"
86
- allow( URI ).to receive(:parse)
87
- .with("http://example.com")
88
- .and_return(endpoint_double)
89
- client = double("CustomClient")
90
- expect( client ).to receive(:post)
91
- .with endpoint_double,
92
- payload: '{"text":"the message"}'
93
-
94
- described_class.new('http://example.com',http_client: client).ping "the message"
95
- end
96
- end
97
- end
80
+ it "calls the middleware stack with the payload" do
81
+ subject = notifier_with_defaults
82
+ stack = instance_double("Slack::Notifier::PayloadMiddleware::Stack")
83
+ subject.instance_variable_set(:@middleware, stack)
98
84
 
99
- describe "#channel=" do
100
- it "sets the given channel" do
101
- subject.channel = "#foo"
102
- expect( subject.channel ).to eq "#foo"
103
- end
104
- end
85
+ expect(stack).to receive(:call)
86
+ .with(channel: "default", user: "rocket")
87
+ .and_return([test: "stack"])
88
+
89
+ expect(mock_http).to receive(:post).with(
90
+ URI.parse("http://example.com"),
91
+ payload: '{"test":"stack"}'
92
+ )
105
93
 
106
- describe "#username=" do
107
- it "sets the given username" do
108
- subject.username = "foo"
109
- expect( subject.username ).to eq "foo"
94
+ responses = subject.post
95
+ expect(responses).to eq([:posted])
110
96
  end
111
97
  end
112
98
  end
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,28 @@
1
- require 'rspec'
2
- require 'slack-notifier'
1
+ # frozen_string_literal: true
3
2
 
4
- if ENV['DEBUG']
5
- require 'pry'
6
- end
3
+ require "rspec"
4
+ require "slack-notifier"
5
+ require "pry" if ENV["DEBUG"]
7
6
 
8
7
  RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
9
12
  config.mock_with :rspec do |mocks|
10
13
  mocks.verify_doubled_constant_names = true
11
14
  mocks.verify_partial_doubles = true
12
15
  end
16
+
17
+ config.filter_run :focus
18
+ config.run_all_when_everything_filtered = true
19
+ config.disable_monkey_patching!
20
+
21
+ config.example_status_persistence_file_path = "spec/examples.txt"
22
+ config.warnings = ENV["DEBUG"] ? false : true
23
+
24
+ config.default_formatter = "doc" if config.files_to_run.one?
25
+
26
+ config.order = :random
27
+ Kernel.srand config.seed
13
28
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
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: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2021-05-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: ' A slim ruby wrapper for posting to slack webhooks '
13
+ description: " A slim ruby wrapper for posting to slack webhooks "
14
14
  email:
15
15
  - stevenosloan@gmail.com
16
16
  executables: []
@@ -18,41 +18,67 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/slack-notifier.rb
21
- - lib/slack-notifier/default_http_client.rb
22
- - lib/slack-notifier/link_formatter.rb
21
+ - lib/slack-notifier/config.rb
22
+ - lib/slack-notifier/payload_middleware.rb
23
+ - lib/slack-notifier/payload_middleware/at.rb
24
+ - lib/slack-notifier/payload_middleware/base.rb
25
+ - lib/slack-notifier/payload_middleware/channels.rb
26
+ - lib/slack-notifier/payload_middleware/format_attachments.rb
27
+ - lib/slack-notifier/payload_middleware/format_message.rb
28
+ - lib/slack-notifier/payload_middleware/stack.rb
29
+ - lib/slack-notifier/util/escape.rb
30
+ - lib/slack-notifier/util/http_client.rb
31
+ - lib/slack-notifier/util/link_formatter.rb
23
32
  - lib/slack-notifier/version.rb
33
+ - spec/end_to_end_spec.rb
24
34
  - spec/integration/ping_integration_test.rb
25
- - spec/lib/slack-notifier/default_http_client_spec.rb
26
- - spec/lib/slack-notifier/link_formatter_spec.rb
35
+ - spec/lib/slack-notifier/config_spec.rb
36
+ - spec/lib/slack-notifier/payload_middleware/at_spec.rb
37
+ - spec/lib/slack-notifier/payload_middleware/base_spec.rb
38
+ - spec/lib/slack-notifier/payload_middleware/channels_spec.rb
39
+ - spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
40
+ - spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
41
+ - spec/lib/slack-notifier/payload_middleware/stack_spec.rb
42
+ - spec/lib/slack-notifier/payload_middleware_spec.rb
43
+ - spec/lib/slack-notifier/util/http_client_spec.rb
44
+ - spec/lib/slack-notifier/util/link_formatter_spec.rb
27
45
  - spec/lib/slack-notifier_spec.rb
28
46
  - spec/spec_helper.rb
29
47
  homepage: http://github.com/stevenosloan/slack-notifier
30
48
  licenses:
31
49
  - MIT
32
50
  metadata: {}
33
- post_install_message:
51
+ post_install_message:
34
52
  rdoc_options: []
35
53
  require_paths:
36
54
  - lib
37
55
  required_ruby_version: !ruby/object:Gem::Requirement
38
56
  requirements:
39
- - - '>='
57
+ - - ">="
40
58
  - !ruby/object:Gem::Version
41
59
  version: '0'
42
60
  required_rubygems_version: !ruby/object:Gem::Requirement
43
61
  requirements:
44
- - - '>='
62
+ - - ">="
45
63
  - !ruby/object:Gem::Version
46
64
  version: '0'
47
65
  requirements: []
48
- rubyforge_project:
49
- rubygems_version: 2.4.5
50
- signing_key:
66
+ rubygems_version: 3.2.15
67
+ signing_key:
51
68
  specification_version: 4
52
69
  summary: A slim ruby wrapper for posting to slack webhooks
53
70
  test_files:
71
+ - spec/end_to_end_spec.rb
54
72
  - spec/integration/ping_integration_test.rb
55
- - spec/lib/slack-notifier/default_http_client_spec.rb
56
- - spec/lib/slack-notifier/link_formatter_spec.rb
73
+ - spec/lib/slack-notifier/config_spec.rb
74
+ - spec/lib/slack-notifier/payload_middleware/at_spec.rb
75
+ - spec/lib/slack-notifier/payload_middleware/base_spec.rb
76
+ - spec/lib/slack-notifier/payload_middleware/channels_spec.rb
77
+ - spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
78
+ - spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
79
+ - spec/lib/slack-notifier/payload_middleware/stack_spec.rb
80
+ - spec/lib/slack-notifier/payload_middleware_spec.rb
81
+ - spec/lib/slack-notifier/util/http_client_spec.rb
82
+ - spec/lib/slack-notifier/util/link_formatter_spec.rb
57
83
  - spec/lib/slack-notifier_spec.rb
58
84
  - spec/spec_helper.rb
@@ -1,51 +0,0 @@
1
- module Slack
2
- class Notifier
3
-
4
- class DefaultHTTPClient
5
-
6
- class << self
7
- def post uri, params
8
- DefaultHTTPClient.new( uri, params ).call
9
- end
10
- end
11
-
12
- attr_reader :uri, :params, :http_options
13
-
14
- def initialize uri, params
15
- @uri = uri
16
- @http_options = params.delete(:http_options) || {}
17
- @params = params
18
- end
19
-
20
- def call
21
- http_obj.request request_obj
22
- end
23
-
24
- private
25
-
26
- def request_obj
27
- req = Net::HTTP::Post.new uri.request_uri
28
- req.set_form_data params
29
-
30
- return req
31
- end
32
-
33
- def http_obj
34
- http = Net::HTTP.new uri.host, uri.port
35
- http.use_ssl = (uri.scheme == "https")
36
-
37
- http_options.each do |opt, val|
38
- if http.respond_to? "#{opt}="
39
- http.send "#{opt}=", val
40
- else
41
- warn "Net::HTTP doesn't respond to `#{opt}=`, ignoring that option"
42
- end
43
- end
44
-
45
- return http
46
- end
47
-
48
- end
49
-
50
- end
51
- end
@@ -1,60 +0,0 @@
1
- module Slack
2
- class Notifier
3
- class LinkFormatter
4
-
5
- class << self
6
-
7
- def format string
8
- LinkFormatter.new(string).formatted
9
- end
10
-
11
- end
12
-
13
- def initialize string
14
- @orig = if string.respond_to? :scrub
15
- string.scrub
16
- else
17
- string
18
- end
19
- end
20
-
21
- def formatted
22
- @orig.gsub( html_pattern ) do |match|
23
- link = Regexp.last_match[1]
24
- text = Regexp.last_match[2]
25
- slack_link link, text
26
- end.gsub( markdown_pattern ) do |match|
27
- link = Regexp.last_match[2]
28
- text = Regexp.last_match[1]
29
- slack_link link, text
30
- end
31
-
32
- rescue => e
33
- if RUBY_VERSION < '2.1' && e.message.include?('invalid byte sequence')
34
- raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
35
- else
36
- raise e
37
- end
38
- end
39
-
40
- private
41
-
42
- def slack_link link, text=nil
43
- out = "<#{link}"
44
- out << "|#{text}" if text && !text.empty?
45
- out << ">"
46
-
47
- return out
48
- end
49
-
50
- def html_pattern
51
- / <a (?:.*?) href=['"](.+?)['"] (?:.*?)> (.+?) <\/a> /x
52
- end
53
-
54
- def markdown_pattern
55
- /\[(.*?)\]\((.+?)\)/
56
- end
57
-
58
- end
59
- end
60
- end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Slack::Notifier::DefaultHTTPClient do
4
-
5
- describe "::post" do
6
- it "initializes DefaultHTTPClient with the given uri and params then calls" do
7
- http_post_double = instance_double("Slack::Notifier::DefaultHTTPClient")
8
-
9
- expect( described_class ).to receive(:new)
10
- .with( 'uri', 'params' )
11
- .and_return( http_post_double )
12
- expect( http_post_double ).to receive(:call)
13
-
14
- described_class.post 'uri', 'params'
15
- end
16
-
17
- # http_post is really tested in the integration spec,
18
- # where the internals are run through
19
- end
20
-
21
- describe "#initialize" do
22
- it "allows setting of options for Net::HTTP" do
23
- net_http_double = instance_double("Net::HTTP")
24
- http_client = described_class.new( URI.parse('http://example.com'), http_options: { open_timeout: 5 })
25
-
26
- allow( Net::HTTP ).to receive(:new)
27
- .and_return(net_http_double)
28
- allow( net_http_double ).to receive(:use_ssl=)
29
- allow( net_http_double ).to receive(:request)
30
-
31
- expect( net_http_double ).to receive(:open_timeout=).with(5)
32
-
33
- http_client.call
34
- end
35
- end
36
-
37
- end
@@ -1,63 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Slack::Notifier::LinkFormatter do
5
-
6
- describe "::format" do
7
-
8
- it "formats html links" do
9
- formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a>.")
10
- expect( formatted ).to include("<http://example.com|this>")
11
- end
12
-
13
- it "formats markdown links" do
14
- formatted = described_class.format("Hello World, enjoy [this](http://example.com).")
15
- expect( formatted ).to include("<http://example.com|this>")
16
- end
17
-
18
- it "formats markdown links with no title" do
19
- formatted = described_class.format("Hello World, enjoy [](http://example.com).")
20
- expect( formatted ).to include("<http://example.com>")
21
- end
22
-
23
- it "handles multiple html links" do
24
- formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a><a href='http://example2.com'>this2</a>.")
25
- expect( formatted ).to include("<http://example.com|this>")
26
- expect( formatted ).to include("<http://example2.com|this2>")
27
- end
28
-
29
- it "handles multiple markdown links" do
30
- formatted = described_class.format("Hello World, enjoy [this](http://example.com)[this2](http://example2.com).")
31
- expect( formatted ).to include("<http://example.com|this>")
32
- expect( formatted ).to include("<http://example2.com|this2>")
33
- end
34
-
35
- it "handles mixed html & markdown links" do
36
- formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.")
37
- expect( formatted ).to include("<http://example.com|this>")
38
- expect( formatted ).to include("<http://example2.com|this2>")
39
- end
40
-
41
- if "".respond_to? :scrub
42
- context "when on ruby 2.1+ or have string-scrub installed" do
43
- it "handles invalid unicode sequences" do
44
- expect {
45
- described_class.format("This sequence is invalid: \255")
46
- }.not_to raise_error
47
- end
48
-
49
- it "replaces invalid unicode sequences with the unicode replacement character" do
50
- formatted = described_class.format("\255")
51
- expect(formatted).to eq "\uFFFD"
52
- end
53
- end
54
- end
55
-
56
- it "doesn't replace valid Japanese" do
57
- formatted = described_class.format("こんにちは")
58
- expect(formatted).to eq "こんにちは"
59
- end
60
-
61
- end
62
-
63
- end