slack-notify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +9 -0
- data/lib/slack-notify.rb +52 -0
- data/lib/slack-notify/error.rb +3 -0
- data/lib/slack-notify/version.rb +3 -0
- data/slack-notify.gemspec +29 -0
- data/spec/slack-notify/client_spec.rb +116 -0
- data/spec/spec_helper.rb +17 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9bb78e8f557ad72bf40e7dada483c74f363eb9b5
|
4
|
+
data.tar.gz: a53d66d0b94c5bbb8b88ddeacfb3bf9abb4cf711
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3c1ffa1661501b852ac68c44dfe1b1a88fb0fbfa240c3e09e024a597bff004e6d1c6a9e4f4244f3e2e3b957e50e8d9bfb2cd9d103ac62a2a160448f327b9b6a8
|
7
|
+
data.tar.gz: ca8cac631640524704dbbe5ef810f3ab8d04ef41f87b263229e8ed148030696dfbe9b0eeab1c127dd354ece20388b01811858438ab67dcfdbbeda9d1cb7cde51
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Dan Sosedoff
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# slack-notify
|
2
|
+
|
3
|
+
Send notifications to [Slack](http://slack.com/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem "slack-notify"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ gem install slack-notify
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Require:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "slack-notify"
|
31
|
+
```
|
32
|
+
|
33
|
+
Initialize client:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
client = SlackNotify::Client.new("subdomain", "token")
|
37
|
+
```
|
38
|
+
|
39
|
+
Initialize with options:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
client = SlackNotify::Client.new("subdomain", "token", {
|
43
|
+
channel: "#development",
|
44
|
+
username: "mybot"
|
45
|
+
})
|
46
|
+
```
|
47
|
+
|
48
|
+
Send test request:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
client.test
|
52
|
+
```
|
53
|
+
|
54
|
+
Send message:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
client.notify("Hello There!")
|
58
|
+
client.notify("Another message", "#channel2")
|
59
|
+
```
|
60
|
+
|
61
|
+
## Gotchas
|
62
|
+
|
63
|
+
Some of the issues while Slack is in beta:
|
64
|
+
|
65
|
+
- No message raised if domain is not valid
|
66
|
+
- Slack raises error 500 on bad request
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
Copyright (c) 2013 Dan Sosedoff
|
71
|
+
|
72
|
+
MIT License
|
data/Rakefile
ADDED
data/lib/slack-notify.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "slack-notify/version"
|
2
|
+
require "slack-notify/error"
|
3
|
+
|
4
|
+
require "json"
|
5
|
+
require "faraday"
|
6
|
+
|
7
|
+
module SlackNotify
|
8
|
+
class Client
|
9
|
+
def initialize(subdomain, token, options={})
|
10
|
+
@subdomain = subdomain
|
11
|
+
@token = token
|
12
|
+
@username = options[:username] || "webhookbot"
|
13
|
+
@channel = options[:channel] || "#general"
|
14
|
+
|
15
|
+
raise ArgumentError, "Subdomain required" if @subdomain.nil?
|
16
|
+
raise ArgumentError, "Token required" if @token.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
def test
|
20
|
+
notify("This is a test message!")
|
21
|
+
end
|
22
|
+
|
23
|
+
def notify(text, channel=nil)
|
24
|
+
send_payload(
|
25
|
+
text: text,
|
26
|
+
channel: channel || @channel,
|
27
|
+
username: @username
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def send_payload(payload)
|
34
|
+
url = "https://#{@subdomain}.slack.com/services/hooks/incoming-webhook"
|
35
|
+
url << "?token=#{@token}"
|
36
|
+
|
37
|
+
response = Faraday.post(url) do |req|
|
38
|
+
req.body = JSON.dump(payload)
|
39
|
+
end
|
40
|
+
|
41
|
+
if response.success?
|
42
|
+
true
|
43
|
+
else
|
44
|
+
if response.body.include?("\n")
|
45
|
+
raise SlackNotify::Error
|
46
|
+
else
|
47
|
+
raise SlackNotify::Error.new(response.body)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slack-notify/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slack-notify"
|
8
|
+
spec.version = SlackNotify::VERSION
|
9
|
+
spec.authors = ["Dan Sosedoff"]
|
10
|
+
spec.email = ["dan.sosedoff@gmail.com"]
|
11
|
+
spec.description = %q{Send notifications to Slack channel}
|
12
|
+
spec.summary = %q{Send notifications to Slack channel}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake", "~> 10"
|
23
|
+
spec.add_development_dependency "simplecov", "~> 0.8"
|
24
|
+
spec.add_development_dependency "rspec", "~> 2.13"
|
25
|
+
spec.add_development_dependency "webmock", "~> 1.0"
|
26
|
+
|
27
|
+
spec.add_dependency "faraday", "~> 0.8"
|
28
|
+
spec.add_dependency "json", "~> 1.8"
|
29
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe SlackNotify::Client do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "requires subdomain" do
|
6
|
+
expect { described_class.new(nil, nil) }.
|
7
|
+
to raise_error ArgumentError, "Subdomain required"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "requires token" do
|
11
|
+
expect { described_class.new("foobar", nil) }.
|
12
|
+
to raise_error ArgumentError, "Token required"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#test" do
|
17
|
+
let(:client) do
|
18
|
+
described_class.new("foo", "token")
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:payload) do
|
22
|
+
{
|
23
|
+
text: "This is a test message!",
|
24
|
+
channel: "#general",
|
25
|
+
username: "webhookbot"
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
client.stub(:send_payload) { true }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sends a test payload" do
|
34
|
+
expect(client).to receive(:send_payload).with(payload)
|
35
|
+
client.test
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#notify" do
|
40
|
+
let(:client) do
|
41
|
+
described_class.new("foo", "token")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "sends message to default channel" do
|
45
|
+
client.stub(:send_payload) { true }
|
46
|
+
|
47
|
+
expect(client).to receive(:send_payload).with(
|
48
|
+
text: "Message",
|
49
|
+
channel: "#general",
|
50
|
+
username: "webhookbot"
|
51
|
+
)
|
52
|
+
|
53
|
+
client.notify("Message")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "sends message as default user" do
|
57
|
+
client.stub(:send_payload) { true }
|
58
|
+
|
59
|
+
expect(client).to receive(:send_payload).with(
|
60
|
+
text: "Message",
|
61
|
+
channel: "#general",
|
62
|
+
username: "webhookbot"
|
63
|
+
)
|
64
|
+
|
65
|
+
client.notify("Message")
|
66
|
+
end
|
67
|
+
|
68
|
+
it "sends message to a specified channel" do
|
69
|
+
client.stub(:send_payload) { true }
|
70
|
+
|
71
|
+
expect(client).to receive(:send_payload).with(
|
72
|
+
text: "Message",
|
73
|
+
channel: "#mychannel",
|
74
|
+
username: "webhookbot"
|
75
|
+
)
|
76
|
+
|
77
|
+
client.notify("Message", "#mychannel")
|
78
|
+
end
|
79
|
+
|
80
|
+
it "delivers payload" do
|
81
|
+
stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
|
82
|
+
with(:body => {"{\"text\":\"Message\",\"channel\":\"#general\",\"username\":\"webhookbot\"}"=>true},
|
83
|
+
: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.8.8'}).
|
84
|
+
to_return(:status => 200, :body => "", :headers => {})
|
85
|
+
|
86
|
+
expect(client.notify("Message")).to eq true
|
87
|
+
end
|
88
|
+
|
89
|
+
context "invalid subdomain" do
|
90
|
+
before do
|
91
|
+
stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
|
92
|
+
with(:body => {"{\"text\":\"Message\",\"channel\":\"#general\",\"username\":\"webhookbot\"}"=>true},
|
93
|
+
: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.8.8'}).
|
94
|
+
to_return(:status => 404, :body => "Multi line\ncontent\nhtml stuff", :headers => {})
|
95
|
+
end
|
96
|
+
|
97
|
+
it "raises error" do
|
98
|
+
expect { client.notify("Message") }.to raise_error SlackNotify::Error
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "invalid token" do
|
103
|
+
before do
|
104
|
+
stub_request(:post, "https://foo.slack.com/services/hooks/incoming-webhook?token=token").
|
105
|
+
with(:body => {"{\"text\":\"Message\",\"channel\":\"#general\",\"username\":\"webhookbot\"}"=>true},
|
106
|
+
: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.8.8'}).
|
107
|
+
to_return(:status => 404, :body => "No hooks", :headers => {})
|
108
|
+
end
|
109
|
+
|
110
|
+
it "raises error" do
|
111
|
+
expect { client.notify("Message") }
|
112
|
+
.to raise_error SlackNotify::Error, "No hooks"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
$:.unshift File.expand_path("../..", __FILE__)
|
5
|
+
|
6
|
+
require "webmock"
|
7
|
+
require "webmock/rspec"
|
8
|
+
require "slack-notify"
|
9
|
+
|
10
|
+
def fixture_path(filename=nil)
|
11
|
+
path = File.expand_path("../fixtures", __FILE__)
|
12
|
+
filename.nil? ? path : File.join(path, filename)
|
13
|
+
end
|
14
|
+
|
15
|
+
def fixture(file)
|
16
|
+
File.read(File.join(fixture_path, file))
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slack-notify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Sosedoff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: faraday
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.8'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.8'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: json
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.8'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.8'
|
111
|
+
description: Send notifications to Slack channel
|
112
|
+
email:
|
113
|
+
- dan.sosedoff@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/slack-notify.rb
|
125
|
+
- lib/slack-notify/error.rb
|
126
|
+
- lib/slack-notify/version.rb
|
127
|
+
- slack-notify.gemspec
|
128
|
+
- spec/slack-notify/client_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
homepage: ''
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.0.5
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Send notifications to Slack channel
|
154
|
+
test_files:
|
155
|
+
- spec/slack-notify/client_spec.rb
|
156
|
+
- spec/spec_helper.rb
|
157
|
+
has_rdoc:
|