slack-notifier 1.5.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/slack-notifier.rb +35 -60
  3. data/lib/slack-notifier/config.rb +37 -0
  4. data/lib/slack-notifier/payload_middleware.rb +21 -0
  5. data/lib/slack-notifier/payload_middleware/base.rb +35 -0
  6. data/lib/slack-notifier/payload_middleware/format_attachments.rb +37 -0
  7. data/lib/slack-notifier/payload_middleware/format_message.rb +19 -0
  8. data/lib/slack-notifier/payload_middleware/stack.rb +35 -0
  9. data/lib/slack-notifier/util/escape.rb +15 -0
  10. data/lib/slack-notifier/util/http_client.rb +53 -0
  11. data/lib/slack-notifier/util/link_formatter.rb +63 -0
  12. data/lib/slack-notifier/version.rb +2 -1
  13. data/spec/end_to_end_spec.rb +84 -0
  14. data/spec/integration/ping_integration_test.rb +12 -5
  15. data/spec/lib/slack-notifier/config_spec.rb +71 -0
  16. data/spec/lib/slack-notifier/payload_middleware/base_spec.rb +75 -0
  17. data/spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb +35 -0
  18. data/spec/lib/slack-notifier/payload_middleware/format_message_spec.rb +26 -0
  19. data/spec/lib/slack-notifier/payload_middleware/stack_spec.rb +93 -0
  20. data/spec/lib/slack-notifier/payload_middleware_spec.rb +32 -0
  21. data/spec/lib/slack-notifier/util/http_client_spec.rb +34 -0
  22. data/spec/lib/slack-notifier/{link_formatter_spec.rb → util/link_formatter_spec.rb} +30 -19
  23. data/spec/lib/slack-notifier_spec.rb +62 -128
  24. data/spec/spec_helper.rb +20 -5
  25. metadata +30 -9
  26. data/lib/slack-notifier/default_http_client.rb +0 -51
  27. data/lib/slack-notifier/link_formatter.rb +0 -62
  28. data/spec/lib/slack-notifier/default_http_client_spec.rb +0 -37
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ RSpec.describe Slack::Notifier::Util::HTTPClient do
3
+ describe "::post" do
4
+ it "initializes Util::HTTPClient with the given uri and params then calls" do
5
+ http_post_double = instance_double("Slack::Notifier::Util::HTTPClient")
6
+
7
+ expect(described_class)
8
+ .to receive(:new).with("uri", "params")
9
+ .and_return(http_post_double)
10
+ expect(http_post_double).to receive(:call)
11
+
12
+ described_class.post "uri", "params"
13
+ end
14
+
15
+ # http_post is really tested in the integration spec,
16
+ # where the internals are run through
17
+ end
18
+
19
+ describe "#initialize" do
20
+ it "allows setting of options for Net::HTTP" do
21
+ net_http_double = instance_double("Net::HTTP")
22
+ http_client = described_class.new URI.parse("http://example.com"),
23
+ http_options: { open_timeout: 5 }
24
+
25
+ allow(Net::HTTP).to receive(:new).and_return(net_http_double)
26
+ allow(net_http_double).to receive(:use_ssl=)
27
+ allow(net_http_double).to receive(:request)
28
+
29
+ expect(net_http_double).to receive(:open_timeout=).with(5)
30
+
31
+ http_client.call
32
+ end
33
+ end
34
+ end
@@ -1,54 +1,52 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Slack::Notifier::LinkFormatter do
5
-
3
+ # rubocop:disable Metrics/LineLength
4
+ RSpec.describe Slack::Notifier::Util::LinkFormatter do
6
5
  describe "::format" do
7
-
8
6
  it "formats html links" do
9
7
  formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a>.")
10
- expect( formatted ).to include("<http://example.com|this>")
8
+ expect(formatted).to include("<http://example.com|this>")
11
9
  end
12
10
 
13
11
  it "formats markdown links" do
14
12
  formatted = described_class.format("Hello World, enjoy [this](http://example.com).")
15
- expect( formatted ).to include("<http://example.com|this>")
13
+ expect(formatted).to include("<http://example.com|this>")
16
14
  end
17
15
 
18
16
  it "formats markdown links in brackets" do
19
17
  formatted = described_class.format("Hello World, enjoy [[this](http://example.com) in brackets].")
20
- expect( formatted ).to eq("Hello World, enjoy [<http://example.com|this> in brackets].")
18
+ expect(formatted).to eq("Hello World, enjoy [<http://example.com|this> in brackets].")
21
19
  end
22
20
 
23
21
  it "formats markdown links with no title" do
24
22
  formatted = described_class.format("Hello World, enjoy [](http://example.com).")
25
- expect( formatted ).to include("<http://example.com>")
23
+ expect(formatted).to include("<http://example.com>")
26
24
  end
27
25
 
28
26
  it "handles multiple html links" do
29
27
  formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a><a href='http://example2.com'>this2</a>.")
30
- expect( formatted ).to include("<http://example.com|this>")
31
- expect( formatted ).to include("<http://example2.com|this2>")
28
+ expect(formatted).to include("<http://example.com|this>")
29
+ expect(formatted).to include("<http://example2.com|this2>")
32
30
  end
33
31
 
34
32
  it "handles multiple markdown links" do
35
33
  formatted = described_class.format("Hello World, enjoy [this](http://example.com)[this2](http://example2.com).")
36
- expect( formatted ).to include("<http://example.com|this>")
37
- expect( formatted ).to include("<http://example2.com|this2>")
34
+ expect(formatted).to include("<http://example.com|this>")
35
+ expect(formatted).to include("<http://example2.com|this2>")
38
36
  end
39
37
 
40
38
  it "handles mixed html & markdown links" do
41
39
  formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.")
42
- expect( formatted ).to include("<http://example.com|this>")
43
- expect( formatted ).to include("<http://example2.com|this2>")
40
+ expect(formatted).to include("<http://example.com|this>")
41
+ expect(formatted).to include("<http://example2.com|this2>")
44
42
  end
45
43
 
46
44
  if "".respond_to? :scrub
47
45
  context "when on ruby 2.1+ or have string-scrub installed" do
48
46
  it "handles invalid unicode sequences" do
49
- expect {
47
+ expect do
50
48
  described_class.format("This sequence is invalid: \255")
51
- }.not_to raise_error
49
+ end.not_to raise_error
52
50
  end
53
51
 
54
52
  it "replaces invalid unicode sequences with the unicode replacement character" do
@@ -58,7 +56,7 @@ describe Slack::Notifier::LinkFormatter do
58
56
  end
59
57
  end
60
58
 
61
- it "doesn't replace valid Japanese" do
59
+ it 'doesn\'t replace valid Japanese' do
62
60
  formatted = described_class.format("こんにちは")
63
61
  expect(formatted).to eq "こんにちは"
64
62
  end
@@ -73,6 +71,19 @@ describe Slack::Notifier::LinkFormatter do
73
71
  expect(formatted).to eq "<mailto:john@example.com|John>"
74
72
  end
75
73
 
74
+ context "with a configured stack" do
75
+ it "only formats html if html is the only item in formats" do
76
+ formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.", formats: [:html])
77
+ expect(formatted).to eq "Hello World, enjoy [this](http://example.com)<http://example2.com|this2>."
78
+ end
79
+ it "only formats markdown if markdown is the only item in formats" do
80
+ formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.", formats: [:markdown])
81
+ expect(formatted).to eq "Hello World, enjoy <http://example.com|this><a href='http://example2.com'>this2</a>."
82
+ end
83
+ it "doesn't format if formats is empty" do
84
+ formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.", formats: [])
85
+ expect(formatted).to eq "Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>."
86
+ end
87
+ end
76
88
  end
77
-
78
89
  end
@@ -1,163 +1,97 @@
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
21
+ subject = described_class.new "http://example.com", http_client: mock_http
22
+ expect(subject.config.http_client).to eq mock_http
20
23
  end
21
- end
22
24
 
23
- describe "#ping" do
24
- before :each do
25
- allow( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
26
- end
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)
27
29
 
28
- it "passes the message through LinkFormatter" do
29
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
30
- .with("the message")
30
+ expect(test_double).to receive(:test_init_method).with("foo")
31
31
 
32
- described_class.new('http://example.com').ping "the message", channel: 'foo'
33
- end
34
-
35
- it "passes attachment messages through LinkFormatter" do
36
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
37
- .with("the message")
38
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
39
- .with("attachment message")
40
-
41
- described_class.new('http://example.com').ping "the message", channel: 'foo',
42
- attachments: [{
43
- color: "#000",
44
- text: "attachment message",
45
- fallback: "fallback message"
46
- }]
32
+ described_class.new "http://example.com" do
33
+ test_init_method "foo"
34
+ end
35
+ end
47
36
  end
37
+ end
48
38
 
49
- it "allows sending only an attachment" do
50
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post).with(
51
- URI.parse('http://example.com'),
52
- payload: '{"channel":"foo","attachments":[{"text":"attachment","fallback":"fallback"}]}'
53
- )
54
-
55
- expect{
56
- described_class.new('http://example.com')
57
- .ping channel: 'foo',
58
- attachments: [{
59
- text: 'attachment',
60
- fallback: 'fallback'
61
- }]
62
- }.not_to raise_error
63
- 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"
64
43
 
65
- it "passes attachment messages through LinkFormatter, even if a single value is passed" do
66
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
67
- .with("a random message")
68
- expect( Slack::Notifier::LinkFormatter ).to receive(:format)
69
- .with("attachment message")
70
- attachment = {
71
- color: "#000",
72
- text: "attachment message",
73
- fallback: "fallback message"
74
- }
75
- subject.ping "a random message", attachments: attachment
44
+ subject.ping "message"
76
45
  end
46
+ end
77
47
 
78
- context "with a default channel set" do
79
-
80
- before :each do
81
- @endpoint_double = instance_double "URI::HTTP"
82
- allow( URI ).to receive(:parse)
83
- .and_return(@endpoint_double)
84
- subject.channel = '#default'
85
- end
86
-
87
- it "does not require a channel to ping" do
88
- expect{
89
- subject.ping "the message"
90
- }.not_to raise_error
91
- end
92
-
93
- it "uses default channel" do
94
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
95
- .with @endpoint_double,
96
- payload: '{"channel":"#default","text":"the message"}'
97
-
98
- 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
99
55
  end
56
+ end
100
57
 
101
- it "allows override channel to be set" do
102
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
103
- .with @endpoint_double,
104
- payload: '{"channel":"new","text":"the message"}'
58
+ it "uses the defaults set on initialization" do
59
+ subject = notifier_with_defaults
105
60
 
106
- subject.ping "the message", channel: "new"
107
- end
61
+ expect(mock_http).to receive(:post).with(
62
+ URI.parse("http://example.com"),
63
+ payload: '{"channel":"default","user":"rocket","text":"hello"}'
64
+ )
108
65
 
66
+ subject.post text: "hello"
109
67
  end
110
68
 
111
- context "with default webhook" do
112
- it "posts with the correct endpoint & data" do
113
- @endpoint_double = instance_double "URI::HTTP"
114
- allow( URI ).to receive(:parse)
115
- .with("http://example.com")
116
- .and_return(@endpoint_double)
69
+ it "allows overriding the set defaults" do
70
+ subject = notifier_with_defaults
117
71
 
118
- expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
119
- .with @endpoint_double,
120
- 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
+ )
121
76
 
122
- described_class.new("http://example.com").ping "the message", channel: "channel"
123
- end
77
+ subject.post text: "hello", channel: "new", user: "ship"
124
78
  end
125
79
 
126
- context "with a custom http_client set" do
127
- it "uses it" do
128
- endpoint_double = instance_double "URI::HTTP"
129
- allow( URI ).to receive(:parse)
130
- .with("http://example.com")
131
- .and_return(endpoint_double)
132
- client = double("CustomClient")
133
- expect( client ).to receive(:post)
134
- .with endpoint_double,
135
- payload: '{"text":"the message"}'
136
-
137
- described_class.new('http://example.com',http_client: client).ping "the message"
138
- end
139
- end
140
- 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)
141
84
 
142
- describe "#channel=" do
143
- it "sets the given channel" do
144
- subject.channel = "#foo"
145
- expect( subject.channel ).to eq "#foo"
146
- end
147
- end
85
+ expect(stack).to receive(:call)
86
+ .with(channel: "default", user: "rocket")
87
+ .and_return(test: "stack")
148
88
 
149
- describe "#username=" do
150
- it "sets the given username" do
151
- subject.username = "foo"
152
- expect( subject.username ).to eq "foo"
153
- end
154
- end
89
+ expect(mock_http).to receive(:post).with(
90
+ URI.parse("http://example.com"),
91
+ payload: '{"test":"stack"}'
92
+ )
155
93
 
156
- describe "#escape" do
157
- it "escapes sequences of < > &, but not quotes" do
158
- message = %q(I've heard "Do > with <" & that sounds ridiculous.)
159
- expected = %q(I've heard "Do &gt; with &lt;" &amp; that sounds ridiculous.)
160
- expect( subject.escape(message) ).to eq expected
94
+ subject.post
161
95
  end
162
96
  end
163
97
  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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 2.0.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: 2015-12-01 00:00:00.000000000 Z
11
+ date: 2017-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " A slim ruby wrapper for posting to slack webhooks "
14
14
  email:
@@ -18,12 +18,26 @@ 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/base.rb
24
+ - lib/slack-notifier/payload_middleware/format_attachments.rb
25
+ - lib/slack-notifier/payload_middleware/format_message.rb
26
+ - lib/slack-notifier/payload_middleware/stack.rb
27
+ - lib/slack-notifier/util/escape.rb
28
+ - lib/slack-notifier/util/http_client.rb
29
+ - lib/slack-notifier/util/link_formatter.rb
23
30
  - lib/slack-notifier/version.rb
31
+ - spec/end_to_end_spec.rb
24
32
  - 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
33
+ - spec/lib/slack-notifier/config_spec.rb
34
+ - spec/lib/slack-notifier/payload_middleware/base_spec.rb
35
+ - spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
36
+ - spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
37
+ - spec/lib/slack-notifier/payload_middleware/stack_spec.rb
38
+ - spec/lib/slack-notifier/payload_middleware_spec.rb
39
+ - spec/lib/slack-notifier/util/http_client_spec.rb
40
+ - spec/lib/slack-notifier/util/link_formatter_spec.rb
27
41
  - spec/lib/slack-notifier_spec.rb
28
42
  - spec/spec_helper.rb
29
43
  homepage: http://github.com/stevenosloan/slack-notifier
@@ -46,13 +60,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
60
  version: '0'
47
61
  requirements: []
48
62
  rubyforge_project:
49
- rubygems_version: 2.4.5.1
63
+ rubygems_version: 2.5.2
50
64
  signing_key:
51
65
  specification_version: 4
52
66
  summary: A slim ruby wrapper for posting to slack webhooks
53
67
  test_files:
68
+ - spec/end_to_end_spec.rb
54
69
  - 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
70
+ - spec/lib/slack-notifier/config_spec.rb
71
+ - spec/lib/slack-notifier/payload_middleware/base_spec.rb
72
+ - spec/lib/slack-notifier/payload_middleware/format_attachments_spec.rb
73
+ - spec/lib/slack-notifier/payload_middleware/format_message_spec.rb
74
+ - spec/lib/slack-notifier/payload_middleware/stack_spec.rb
75
+ - spec/lib/slack-notifier/payload_middleware_spec.rb
76
+ - spec/lib/slack-notifier/util/http_client_spec.rb
77
+ - spec/lib/slack-notifier/util/link_formatter_spec.rb
57
78
  - spec/lib/slack-notifier_spec.rb
58
79
  - spec/spec_helper.rb