slack-notifier 0.5.0 → 1.0.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 +4 -4
- data/lib/slack-notifier/{http_post.rb → default_http_client.rb} +3 -3
- data/lib/slack-notifier/link_formatter.rb +1 -1
- data/lib/slack-notifier/version.rb +2 -2
- data/lib/slack-notifier.rb +9 -25
- data/spec/integration/ping_integration_test.rb +1 -1
- data/spec/lib/slack-notifier/{http_post_spec.rb → default_http_client_spec.rb} +5 -5
- data/spec/lib/slack-notifier/link_formatter_spec.rb +18 -0
- data/spec/lib/slack-notifier_spec.rb +26 -35
- metadata +5 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a423a9d3657e72895726732371162ca6999c1cb6
|
|
4
|
+
data.tar.gz: d929fe769de577f03331eebac18796eb2ab06b28
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 74aa5dfc1e49ab07922382bc69e8357ac5a7f5cca7ca9df110bee939da04a77eccb719db27c49864d65a317a4f3d224876b210bb5cebf2a5b14018064d950139
|
|
7
|
+
data.tar.gz: a4716f34f9e618a016d691830a82ee6b589e51af8b53a3d44625585b042b0c47b17f57db8426bcabaed5a798839a4d984a31dc213bc530e8a4da2a10a970f606
|
data/lib/slack-notifier.rb
CHANGED
|
@@ -2,31 +2,25 @@ require 'net/http'
|
|
|
2
2
|
require 'uri'
|
|
3
3
|
require 'json'
|
|
4
4
|
|
|
5
|
-
require_relative 'slack-notifier/
|
|
5
|
+
require_relative 'slack-notifier/default_http_client'
|
|
6
6
|
require_relative 'slack-notifier/link_formatter'
|
|
7
7
|
|
|
8
8
|
module Slack
|
|
9
9
|
class Notifier
|
|
10
|
-
attr_reader :
|
|
11
|
-
|
|
12
|
-
def initialize
|
|
13
|
-
@
|
|
14
|
-
@
|
|
15
|
-
|
|
16
|
-
if hook_name.is_a? Hash
|
|
17
|
-
@hook_name = default_hook_name
|
|
18
|
-
@default_payload = hook_name
|
|
19
|
-
else
|
|
20
|
-
@hook_name = hook_name
|
|
21
|
-
@default_payload = default_payload
|
|
22
|
-
end
|
|
10
|
+
attr_reader :endpoint, :http_client, :default_payload
|
|
11
|
+
|
|
12
|
+
def initialize webhook_url, options={}
|
|
13
|
+
@endpoint = URI.parse webhook_url
|
|
14
|
+
@http_client = options.delete(:http_client) || DefaultHTTPClient
|
|
15
|
+
@default_payload = options
|
|
23
16
|
end
|
|
24
17
|
|
|
25
18
|
def ping message, options={}
|
|
26
19
|
message = LinkFormatter.format(message)
|
|
27
20
|
payload = { text: message }.merge(default_payload).merge(options)
|
|
28
21
|
|
|
29
|
-
|
|
22
|
+
|
|
23
|
+
http_client.post endpoint, payload: payload.to_json
|
|
30
24
|
end
|
|
31
25
|
|
|
32
26
|
def channel
|
|
@@ -45,15 +39,5 @@ module Slack
|
|
|
45
39
|
default_payload[:username] = username
|
|
46
40
|
end
|
|
47
41
|
|
|
48
|
-
private
|
|
49
|
-
|
|
50
|
-
def default_hook_name
|
|
51
|
-
'incoming-webhook'
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def endpoint
|
|
55
|
-
URI.parse "https://#{team}.slack.com/services/hooks/#{hook_name}?token=#{token}"
|
|
56
|
-
end
|
|
57
|
-
|
|
58
42
|
end
|
|
59
43
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require_relative '../../lib/slack-notifier'
|
|
2
2
|
|
|
3
|
-
notifier = Slack::Notifier.new ENV['
|
|
3
|
+
notifier = Slack::Notifier.new ENV['SLACK_WEBHOOK_URL'], username: 'notifier'
|
|
4
4
|
puts "testing with ruby #{RUBY_VERSION}"
|
|
5
5
|
notifier.ping "hello from notifier test script on ruby: #{RUBY_VERSION}"
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
describe Slack::Notifier::
|
|
3
|
+
describe Slack::Notifier::DefaultHTTPClient do
|
|
4
4
|
|
|
5
|
-
describe "::
|
|
6
|
-
it "initializes
|
|
7
|
-
http_post_double = instance_double("Slack::Notifier::
|
|
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
8
|
|
|
9
9
|
expect( described_class ).to receive(:new)
|
|
10
10
|
.with( 'uri', 'params' )
|
|
11
11
|
.and_return( http_post_double )
|
|
12
12
|
expect( http_post_double ).to receive(:call)
|
|
13
13
|
|
|
14
|
-
described_class.
|
|
14
|
+
described_class.post 'uri', 'params'
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# http_post is really tested in the integration spec,
|
|
@@ -19,6 +19,24 @@ describe Slack::Notifier::LinkFormatter do
|
|
|
19
19
|
expect( formatted ).to include("<http://example.com>")
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
+
it "handles multiple html links" do
|
|
23
|
+
formatted = described_class.format("Hello World, enjoy <a href='http://example.com'>this</a><a href='http://example2.com'>this2</a>.")
|
|
24
|
+
expect( formatted ).to include("<http://example.com|this>")
|
|
25
|
+
expect( formatted ).to include("<http://example2.com|this2>")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "handles multiple markdown links" do
|
|
29
|
+
formatted = described_class.format("Hello World, enjoy [this](http://example.com)[this2](http://example2.com).")
|
|
30
|
+
expect( formatted ).to include("<http://example.com|this>")
|
|
31
|
+
expect( formatted ).to include("<http://example2.com|this2>")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "handles mixed html & markdown links" do
|
|
35
|
+
formatted = described_class.format("Hello World, enjoy [this](http://example.com)<a href='http://example2.com'>this2</a>.")
|
|
36
|
+
expect( formatted ).to include("<http://example.com|this>")
|
|
37
|
+
expect( formatted ).to include("<http://example2.com|this2>")
|
|
38
|
+
end
|
|
39
|
+
|
|
22
40
|
end
|
|
23
41
|
|
|
24
42
|
end
|
|
@@ -1,44 +1,35 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe Slack::Notifier do
|
|
4
|
-
subject { described_class.new '
|
|
4
|
+
subject { described_class.new 'http://example.com' }
|
|
5
5
|
|
|
6
6
|
describe "#initialize" do
|
|
7
|
-
it "sets the given
|
|
8
|
-
expect( subject.
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it "sets the given token" do
|
|
12
|
-
expect( subject.token ).to eq 'token'
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "sets the optional service hook name" do
|
|
16
|
-
subject = described_class.new 'team', 'token', 'custom_hook_name'
|
|
17
|
-
expect( subject.hook_name ).to eq 'custom_hook_name'
|
|
7
|
+
it "sets the given hook_url to the endpoint URI" do
|
|
8
|
+
expect( subject.endpoint ).to eq URI.parse 'http://example.com'
|
|
18
9
|
end
|
|
19
10
|
|
|
20
11
|
it "sets the default_payload options" do
|
|
21
|
-
subject = described_class.new '
|
|
12
|
+
subject = described_class.new 'http://example.com', channel: 'foo'
|
|
22
13
|
expect( subject.channel ).to eq 'foo'
|
|
23
14
|
end
|
|
24
15
|
|
|
25
|
-
it "
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
expect( subject.
|
|
16
|
+
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
|
|
29
20
|
end
|
|
30
21
|
end
|
|
31
22
|
|
|
32
23
|
describe "#ping" do
|
|
33
24
|
before :each do
|
|
34
|
-
allow( Slack::Notifier::
|
|
25
|
+
allow( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
|
|
35
26
|
end
|
|
36
27
|
|
|
37
28
|
it "passes the message through LinkFormatter" do
|
|
38
29
|
expect( Slack::Notifier::LinkFormatter ).to receive(:format)
|
|
39
30
|
.with("the message")
|
|
40
31
|
|
|
41
|
-
described_class.new('
|
|
32
|
+
described_class.new('http://example.com').ping "the message", channel: 'foo'
|
|
42
33
|
end
|
|
43
34
|
|
|
44
35
|
context "with a default channel set" do
|
|
@@ -57,7 +48,7 @@ describe Slack::Notifier do
|
|
|
57
48
|
end
|
|
58
49
|
|
|
59
50
|
it "uses default channel" do
|
|
60
|
-
expect( Slack::Notifier::
|
|
51
|
+
expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
|
|
61
52
|
.with @endpoint_double,
|
|
62
53
|
payload: '{"text":"the message","channel":"#default"}'
|
|
63
54
|
|
|
@@ -65,7 +56,7 @@ describe Slack::Notifier do
|
|
|
65
56
|
end
|
|
66
57
|
|
|
67
58
|
it "allows override channel to be set" do
|
|
68
|
-
expect( Slack::Notifier::
|
|
59
|
+
expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
|
|
69
60
|
.with @endpoint_double,
|
|
70
61
|
payload: '{"text":"the message","channel":"new"}'
|
|
71
62
|
|
|
@@ -78,29 +69,29 @@ describe Slack::Notifier do
|
|
|
78
69
|
it "posts with the correct endpoint & data" do
|
|
79
70
|
@endpoint_double = instance_double "URI::HTTP"
|
|
80
71
|
allow( URI ).to receive(:parse)
|
|
81
|
-
.with("
|
|
72
|
+
.with("http://example.com")
|
|
82
73
|
.and_return(@endpoint_double)
|
|
83
74
|
|
|
84
|
-
expect( Slack::Notifier::
|
|
75
|
+
expect( Slack::Notifier::DefaultHTTPClient ).to receive(:post)
|
|
85
76
|
.with @endpoint_double,
|
|
86
77
|
payload: '{"text":"the message","channel":"channel"}'
|
|
87
78
|
|
|
88
|
-
described_class.new("
|
|
79
|
+
described_class.new("http://example.com").ping "the message", channel: "channel"
|
|
89
80
|
end
|
|
90
81
|
end
|
|
91
82
|
|
|
92
|
-
context "with custom
|
|
93
|
-
it "
|
|
94
|
-
|
|
83
|
+
context "with a custom http_client set" do
|
|
84
|
+
it "uses it" do
|
|
85
|
+
endpoint_double = instance_double "URI::HTTP"
|
|
95
86
|
allow( URI ).to receive(:parse)
|
|
96
|
-
.with("
|
|
97
|
-
.and_return(
|
|
98
|
-
|
|
99
|
-
expect(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
described_class.new(
|
|
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"
|
|
104
95
|
end
|
|
105
96
|
end
|
|
106
97
|
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: 0.
|
|
4
|
+
version: 1.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: 2014-
|
|
11
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: " A slim ruby wrapper for posting to slack webhooks "
|
|
14
14
|
email:
|
|
@@ -18,11 +18,11 @@ extensions: []
|
|
|
18
18
|
extra_rdoc_files: []
|
|
19
19
|
files:
|
|
20
20
|
- lib/slack-notifier.rb
|
|
21
|
-
- lib/slack-notifier/
|
|
21
|
+
- lib/slack-notifier/default_http_client.rb
|
|
22
22
|
- lib/slack-notifier/link_formatter.rb
|
|
23
23
|
- lib/slack-notifier/version.rb
|
|
24
24
|
- spec/integration/ping_integration_test.rb
|
|
25
|
-
- spec/lib/slack-notifier/
|
|
25
|
+
- spec/lib/slack-notifier/default_http_client_spec.rb
|
|
26
26
|
- spec/lib/slack-notifier/link_formatter_spec.rb
|
|
27
27
|
- spec/lib/slack-notifier_spec.rb
|
|
28
28
|
- spec/spec_helper.rb
|
|
@@ -52,8 +52,7 @@ specification_version: 4
|
|
|
52
52
|
summary: A slim ruby wrapper for posting to slack webhooks
|
|
53
53
|
test_files:
|
|
54
54
|
- spec/integration/ping_integration_test.rb
|
|
55
|
-
- spec/lib/slack-notifier/
|
|
55
|
+
- spec/lib/slack-notifier/default_http_client_spec.rb
|
|
56
56
|
- spec/lib/slack-notifier/link_formatter_spec.rb
|
|
57
57
|
- spec/lib/slack-notifier_spec.rb
|
|
58
58
|
- spec/spec_helper.rb
|
|
59
|
-
has_rdoc:
|