slack-notify 0.3.3 → 0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 20a2b6939a5568e6d47b85ce5324cd5625027a1c
4
- data.tar.gz: 7215519c083be5901151756f7a099f12589dfd07
3
+ metadata.gz: 7258bd95339bbf7ce21f40177fe44b2cefe94a06
4
+ data.tar.gz: a1ff2e2acfcab61138ae1d3558f91616fae29883
5
5
  SHA512:
6
- metadata.gz: 4588aff57f04f9d036b5e200e195f59c75d1d2d4110dd1e2d37be95add91901cb8ba9c5d2abaed8fda10b04779e5ab0225030899f65f33d2031a3730d0033372
7
- data.tar.gz: 1d0b1f9fac8129006e1e3c6b85e3548610ccb798c7a2f210a57536c8f23130761cc3fc55cc3b717edfa6f53f275820b5bc68a4d06ef096612cd88dbc3cd0cd17
6
+ metadata.gz: 0a25b9e37a3e7e74baf9e81567640f0c6bb93a4f2b9a497a6c78ba5b0908c4e2ed04e8ef2a4f58dc43cbc90e753a4b0d696741171ea7a66924e5d2896570e335
7
+ data.tar.gz: a9bbde639753f8077256c3d2c80dd1696e1a031bed8f03c95d73ca82ed3b3068b6fb5283efbc5108e3b47b97a81dc1d62152f068c87b723386896d6e8ed96b31
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # slack-notify
2
2
 
3
- Send notifications to [Slack](http://slack.com/)
3
+ Send notifications to [Slack](http://slack.com/) via webhooks.
4
4
 
5
5
  [![Build Status](http://img.shields.io/travis/sosedoff/slack-notify/master.svg?style=flat)](https://travis-ci.org/sosedoff/slack-notify)
6
6
  [![Code Climate](http://img.shields.io/codeclimate/github/sosedoff/slack-notify.svg?style=flat)](https://codeclimate.com/github/sosedoff/slack-notify)
@@ -37,13 +37,13 @@ require "slack-notify"
37
37
  Initialize client:
38
38
 
39
39
  ```ruby
40
- client = SlackNotify::Client.new("team", "token")
40
+ client = SlackNotify::Client.new("slack webhook url")
41
41
  ```
42
42
 
43
43
  Initialize with options:
44
44
 
45
45
  ```ruby
46
- client = SlackNotify::Client.new("team", "token", {
46
+ client = SlackNotify::Client.new("slack webhook url", {
47
47
  channel: "#development",
48
48
  username: "mybot",
49
49
  icon_url: "http://mydomain.com/myimage.png",
@@ -52,6 +52,12 @@ client = SlackNotify::Client.new("team", "token", {
52
52
  })
53
53
  ```
54
54
 
55
+ Initialize via shorthand method:
56
+
57
+ ```ruby
58
+ client = SlackNotify.new(options)
59
+ ```
60
+
55
61
  Send test request:
56
62
 
57
63
  ```ruby
@@ -78,13 +84,6 @@ You can also test gem via rake console:
78
84
  rake console
79
85
  ```
80
86
 
81
- ## Gotchas
82
-
83
- Current issues with Slack API:
84
-
85
- - No message raised if team subdomain is invalid
86
- - 500 server error is raised on bad requests
87
-
88
87
  ## License
89
88
 
90
89
  Copyright (c) 2013-2014 Dan Sosedoff, <dan.sosedoff@gmail.com>
@@ -2,4 +2,10 @@ require "slack-notify/version"
2
2
  require "slack-notify/error"
3
3
  require "slack-notify/connection"
4
4
  require "slack-notify/payload"
5
- require "slack-notify/client"
5
+ require "slack-notify/client"
6
+
7
+ module SlackNotify
8
+ def self.new(options = {})
9
+ SlackNotify::Client.new(options)
10
+ end
11
+ end
@@ -5,9 +5,8 @@ module SlackNotify
5
5
  class Client
6
6
  include SlackNotify::Connection
7
7
 
8
- def initialize(team, token, options = {})
9
- @team = team
10
- @token = token
8
+ def initialize(options = {})
9
+ @webhook_url = options[:webhook_url]
11
10
  @username = options[:username]
12
11
  @channel = options[:channel]
13
12
  @icon_url = options[:icon_url]
@@ -15,7 +14,9 @@ module SlackNotify
15
14
  @link_names = options[:link_names]
16
15
  @unfurl_links = options[:unfurl_links] || "1"
17
16
 
18
- validate_arguments
17
+ if @webhook_url.nil?
18
+ raise ArgumentError, "Webhook URL required"
19
+ end
19
20
  end
20
21
 
21
22
  def test
@@ -42,16 +43,6 @@ module SlackNotify
42
43
 
43
44
  private
44
45
 
45
- def validate_arguments
46
- raise ArgumentError, "Team name required" if @team.nil?
47
- raise ArgumentError, "Token required" if @token.nil?
48
- raise ArgumentError, "Invalid team name" unless valid_team_name?
49
- end
50
-
51
- def valid_team_name?
52
- @team =~ /^[a-z\d\-]+$/i ? true : false
53
- end
54
-
55
46
  def delivery_channels(channel)
56
47
  [channel || @channel || "#general"].flatten.compact.uniq
57
48
  end
@@ -1,7 +1,7 @@
1
1
  module SlackNotify
2
2
  module Connection
3
3
  def send_payload(payload)
4
- conn = Faraday.new(hook_url) do |c|
4
+ conn = Faraday.new(@webhook_url) do |c|
5
5
  c.use(Faraday::Request::UrlEncoded)
6
6
  c.adapter(Faraday.default_adapter)
7
7
  c.options.timeout = 5
@@ -24,13 +24,5 @@ module SlackNotify
24
24
  end
25
25
  end
26
26
  end
27
-
28
- def hook_url
29
- "#{base_url}/services/hooks/incoming-webhook?token=#{@token}"
30
- end
31
-
32
- def base_url
33
- "https://#{@team}.slack.com"
34
- end
35
27
  end
36
28
  end
@@ -1,3 +1,3 @@
1
1
  module SlackNotify
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -2,36 +2,17 @@ require "spec_helper"
2
2
 
3
3
  describe SlackNotify::Client do
4
4
  describe "#initialize" do
5
- it "requires team name" do
6
- expect { described_class.new(nil, nil) }.
7
- to raise_error ArgumentError, "Team name required"
5
+ it "requires webhook_url" do
6
+ expect { described_class.new }.to raise_error ArgumentError, "Webhook URL required"
8
7
  end
9
8
 
10
- it "requires token" do
11
- expect { described_class.new("foobar", nil) }.
12
- to raise_error ArgumentError, "Token required"
13
- end
14
-
15
- it "raises error on invalid team name" do
16
- names = ["foo bar", "foo $bar", "foo.bar"]
17
-
18
- names.each do |name|
19
- expect { described_class.new(name, "token") }.
20
- to raise_error "Invalid team name"
21
- end
22
- end
23
-
24
- it "does not raise error on valid team name" do
25
- names = ["foo", "Foo", "foo-bar"]
26
-
27
- names.each do |name|
28
- expect { described_class.new(name, "token") }.not_to raise_error
29
- end
9
+ it "does not raise error when webhook_url is set" do
10
+ expect { described_class.new(webhook_url: "foobar") }.not_to raise_error
30
11
  end
31
12
  end
32
13
 
33
14
  describe "#test" do
34
- let(:client) { described_class.new("foo", "token") }
15
+ let(:client) { described_class.new(webhook_url: "foobar") }
35
16
 
36
17
  before do
37
18
  client.stub(:notify)
@@ -44,46 +25,31 @@ describe SlackNotify::Client do
44
25
  end
45
26
 
46
27
  describe "#notify" do
47
- let(:client) { described_class.new("foo", "token") }
28
+ let(:client) do
29
+ described_class.new(webhook_url: "https://hooks.slack.com/services/foo/bar")
30
+ end
48
31
 
49
- it "delivers payload" do
50
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
51
- with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"unfurl_links\":\"1\"}"=>true},
52
- :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
53
- to_return(:status => 200, :body => "", :headers => {})
32
+ before do
33
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
34
+ with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"unfurl_links\":\"1\"}"=>true},
35
+ :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
36
+ to_return(:status => 200, :body => "", :headers => {})
37
+ end
54
38
 
39
+ it "delivers payload" do
55
40
  expect(client.notify("Message")).to eq true
56
41
  end
57
42
 
58
- context "with settings from environment variables" do
59
- let(:vars) { ["SLACK_TEAM", "SLACK_TOKEN", "SLACK_CHANNEL", "SLACK_USER"] }
60
-
43
+ context "when icon_url is set" do
61
44
  let(:client) do
62
- described_class.new(ENV["SLACK_TEAM"], ENV["SLACK_TOKEN"], {
63
- channel: ENV["SLACK_CHANNEL"],
64
- username: ENV["SLACK_USER"]
65
- })
45
+ described_class.new(
46
+ webhook_url: "https://hooks.slack.com/services/foo/bar",
47
+ icon_url: "foobar"
48
+ )
66
49
  end
67
50
 
68
51
  before do
69
- vars.each { |v| ENV[v] = "foobar" }
70
- client.stub(:send_payload) { true }
71
- end
72
-
73
- after do
74
- vars.each { |v| ENV.delete(v) }
75
- end
76
-
77
- it "sends data to channel specified by environment variables" do
78
- client.notify("Message")
79
- end
80
- end
81
-
82
- context "when icon_url is set" do
83
- let(:client) { described_class.new("foo", "bar", icon_url: "foobar") }
84
-
85
- before do
86
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=bar").
52
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
87
53
  with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"icon_url\":\"foobar\",\"unfurl_links\":\"1\"}"=>true},
88
54
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.0'}).
89
55
  to_return(:status => 200, :body => "", :headers => {})
@@ -95,10 +61,15 @@ describe SlackNotify::Client do
95
61
  end
96
62
 
97
63
  context "when icon_emoji is set" do
98
- let(:client) { described_class.new("foo", "bar", icon_emoji: "foobar") }
64
+ let(:client) do
65
+ described_class.new(
66
+ webhook_url: "https://hooks.slack.com/services/foo/bar",
67
+ icon_emoji: "foobar"
68
+ )
69
+ end
99
70
 
100
71
  before do
101
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=bar").
72
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
102
73
  with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"icon_emoji\":\"foobar\",\"unfurl_links\":\"1\"}"=>true},
103
74
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.0'}).
104
75
  to_return(:status => 200, :body => "", :headers => {})
@@ -110,10 +81,15 @@ describe SlackNotify::Client do
110
81
  end
111
82
 
112
83
  context "when link_names is set" do
113
- let(:client) { described_class.new("foo", "bar", link_names: 1) }
84
+ let(:client) do
85
+ described_class.new(
86
+ webhook_url: "https://hooks.slack.com/services/foo/bar",
87
+ link_names: 1
88
+ )
89
+ end
114
90
 
115
91
  before do
116
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=bar").
92
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
117
93
  with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"link_names\":1,\"unfurl_links\":\"1\"}"=>true},
118
94
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.9.0'}).
119
95
  to_return(:status => 200, :body => "", :headers => {})
@@ -137,7 +113,7 @@ describe SlackNotify::Client do
137
113
 
138
114
  context "when team name is invalid" do
139
115
  before do
140
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
116
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
141
117
  with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"unfurl_links\":\"1\"}"=>true},
142
118
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
143
119
  to_return(:status => 404, :body => "Line 1\nLine 2\nLine 3", :headers => {})
@@ -150,7 +126,7 @@ describe SlackNotify::Client do
150
126
 
151
127
  context "when token is invalid" do
152
128
  before do
153
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
129
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
154
130
  with(:body => {"{\"text\":\"Message\",\"username\":\"webhookbot\",\"channel\":\"#general\",\"unfurl_links\":\"1\"}"=>true},
155
131
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
156
132
  to_return(:status => 500, :body => "No hooks", :headers => {})
@@ -164,7 +140,7 @@ describe SlackNotify::Client do
164
140
 
165
141
  context "when channel is invalid" do
166
142
  before do
167
- stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
143
+ stub_request(:post, "https://hooks.slack.com/services/foo/bar").
168
144
  with(:body => {"{\"text\":\"message\",\"username\":\"webhookbot\",\"channel\":\"#foobar\",\"unfurl_links\":\"1\"}"=>true},
169
145
  :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded'}).
170
146
  to_return(:status => 500, :body => "Invalid channel specified", :headers => {})
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe SlackNotify do
4
+ describe ".new" do
5
+ it "returns a client instance" do
6
+ expect(SlackNotify.new(webhook_url: "foobar")).to be_a SlackNotify::Client
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-notify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Sosedoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-08 00:00:00.000000000 Z
11
+ date: 2014-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,6 +131,7 @@ files:
131
131
  - slack-notify.gemspec
132
132
  - spec/slack-notify/client_spec.rb
133
133
  - spec/slack-notify/payload_spec.rb
134
+ - spec/slack_notify_spec.rb
134
135
  - spec/spec_helper.rb
135
136
  homepage: https://github.com/sosedoff/slack-notify
136
137
  licenses:
@@ -159,4 +160,5 @@ summary: Send notifications to a Slack channel
159
160
  test_files:
160
161
  - spec/slack-notify/client_spec.rb
161
162
  - spec/slack-notify/payload_spec.rb
163
+ - spec/slack_notify_spec.rb
162
164
  - spec/spec_helper.rb