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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ed944b7001667e3e533abc25325fec4d7bfa99b
4
- data.tar.gz: 4d0ae7eb667f225b42b5621afdc2ffbac463f471
3
+ metadata.gz: a423a9d3657e72895726732371162ca6999c1cb6
4
+ data.tar.gz: d929fe769de577f03331eebac18796eb2ab06b28
5
5
  SHA512:
6
- metadata.gz: 53703f1a9413e12505c0d2338776738eb25c5383c1448fd4e2efeb65f71eaccd4b6bf101263639e25bc957c588af4d4b094c77ae45f61ffb3adf0e8707db20d0
7
- data.tar.gz: f35eb71b3e57950f2e648e241682a054fbe98d6a7c96719f766d478c704d8afdefd96b853d8237f9dcbef2ebbf3a69195196b717a4242128f58db5ef2e84436a
6
+ metadata.gz: 74aa5dfc1e49ab07922382bc69e8357ac5a7f5cca7ca9df110bee939da04a77eccb719db27c49864d65a317a4f3d224876b210bb5cebf2a5b14018064d950139
7
+ data.tar.gz: a4716f34f9e618a016d691830a82ee6b589e51af8b53a3d44625585b042b0c47b17f57db8426bcabaed5a798839a4d984a31dc213bc530e8a4da2a10a970f606
@@ -1,11 +1,11 @@
1
1
  module Slack
2
2
  class Notifier
3
3
 
4
- class HTTPPost
4
+ class DefaultHTTPClient
5
5
 
6
6
  class << self
7
- def to uri, params
8
- HTTPPost.new( uri, params ).call
7
+ def post uri, params
8
+ DefaultHTTPClient.new( uri, params ).call
9
9
  end
10
10
  end
11
11
 
@@ -37,7 +37,7 @@ module Slack
37
37
  end
38
38
 
39
39
  def html_pattern
40
- / <a (?:.*?) href=['"](.+)['"] (?:.*)> (.+?) <\/a> /x
40
+ / <a (?:.*?) href=['"](.+?)['"] (?:.*?)> (.+?) <\/a> /x
41
41
  end
42
42
 
43
43
  def markdown_pattern
@@ -1,5 +1,5 @@
1
1
  module Slack
2
2
  class Notifier
3
- VERSION = "0.5.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
- end
5
+ end
@@ -2,31 +2,25 @@ require 'net/http'
2
2
  require 'uri'
3
3
  require 'json'
4
4
 
5
- require_relative 'slack-notifier/http_post'
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 :team, :token, :hook_name, :default_payload
11
-
12
- def initialize team, token, hook_name=default_hook_name, default_payload={}
13
- @team = team
14
- @token = token
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
- HTTPPost.to endpoint, payload: payload.to_json
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['SLACK_TEAM'], ENV['SLACK_TOKEN'], username: 'notifier'
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::HTTPPost do
3
+ describe Slack::Notifier::DefaultHTTPClient do
4
4
 
5
- describe "::to" do
6
- it "initializes HTTPPost with the given uri and params then calls" do
7
- http_post_double = instance_double("Slack::Notifier::HTTPPost")
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.to 'uri', 'params'
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 'team', 'token' }
4
+ subject { described_class.new 'http://example.com' }
5
5
 
6
6
  describe "#initialize" do
7
- it "sets the given team" do
8
- expect( subject.team ).to eq 'team'
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 'team', 'token', channel: 'foo'
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 "can set service hook & default_payload options" do
26
- subject = described_class.new 'team', 'token', 'hook_name', channel: 'foo'
27
- expect( subject.channel ).to eq 'foo'
28
- expect( subject.hook_name ).to eq 'hook_name'
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::HTTPPost ).to receive(:to)
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('team','token').ping "the message", channel: 'foo'
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::HTTPPost ).to receive(:to)
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::HTTPPost ).to receive(:to)
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("https://team.slack.com/services/hooks/incoming-webhook?token=token")
72
+ .with("http://example.com")
82
73
  .and_return(@endpoint_double)
83
74
 
84
- expect( Slack::Notifier::HTTPPost ).to receive(:to)
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("team","token").ping "the message", channel: "channel"
79
+ described_class.new("http://example.com").ping "the message", channel: "channel"
89
80
  end
90
81
  end
91
82
 
92
- context "with custom webhook name" do
93
- it "posts with the correct endpoint & data" do
94
- @endpoint_double = instance_double "URI::HTTP"
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("https://team.slack.com/services/hooks/custom_hook_name?token=token")
97
- .and_return(@endpoint_double)
98
-
99
- expect( Slack::Notifier::HTTPPost ).to receive(:to)
100
- .with @endpoint_double,
101
- payload: '{"text":"the message","channel":"channel"}'
102
-
103
- described_class.new("team","token","custom_hook_name").ping "the message", channel: "channel"
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.5.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-05-14 00:00:00.000000000 Z
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/http_post.rb
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/http_post_spec.rb
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/http_post_spec.rb
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: