slack-notification 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 +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/slack_notification.rb +92 -0
- data/slack-notification.gemspec +33 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 83e0b0242c87913f71ddfc6ce4ddba328055844e497aa8d724128a413d76c31a
|
4
|
+
data.tar.gz: 7e1986ba946e6020d877534825f76077cca78242a3c4e2faaea72395a20329e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2416cea694677140932daa0c019334c8e6602eace72ee01930ef0d7d595161deb3b2b1938932f6d95b7d7e4dd333fd8f3cae426bf1a6d73a9d3f997d779098e
|
7
|
+
data.tar.gz: dc0d2ab30971b435e7b971b24745b113ec7cef31518dfc9e481dbfe0b096a16dca1f6e5451b06d81a72f3be7cb99d8791b65de46569c189c5c477350d4786f3d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Julian Fiander
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# SlackNotification
|
2
|
+
|
3
|
+
This is a simplified API for sending Slack messages from (e.g.) a Rails application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'slack-notification'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
gem install slack-notification
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
SlackNotification.new(
|
29
|
+
channel: 'channel-name-here', type: :success, title: 'Something Happened',
|
30
|
+
fallback: "Plain-text version of the message.",
|
31
|
+
fields: [
|
32
|
+
{ title: 'Field one', value: value_one, short: true },
|
33
|
+
{ title: 'Field two', value: value_two, short: true },
|
34
|
+
{ title: 'Longer field that needs more space.', value: value_three, short: false }
|
35
|
+
]
|
36
|
+
).notify!
|
37
|
+
```
|
38
|
+
|
39
|
+
Options:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
:type # Notification type (see below)
|
43
|
+
:dryrun # Do not actually submit the message
|
44
|
+
:title # Title for the message
|
45
|
+
:fallback # Plain-text version for if fields won't work
|
46
|
+
:fields # Regular data for the message
|
47
|
+
:channel # The channel webhook to submit to
|
48
|
+
```
|
49
|
+
|
50
|
+
Available notification types:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
%i[success info warning failure]
|
54
|
+
```
|
55
|
+
|
56
|
+
Fields can be formatted in several ways:
|
57
|
+
|
58
|
+
String: The fields value becomes the message title, and no fields are submitted.
|
59
|
+
Hash: Format: `{ title_1: :value_1, [...] }`
|
60
|
+
Array: Array of hashes, each of format: `{ title: 'Field name', value: field_value, short: true }`
|
61
|
+
|
62
|
+
Channels can be made available by setting ENV variables beginning with `SLACK_URL_`.
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "slack/notification"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'slack-notifier'
|
4
|
+
|
5
|
+
class SlackNotification
|
6
|
+
attr_accessor :type, :dryrun, :title, :fallback, :fields, :channel
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@channel = validated_channel(options[:channel].to_s)
|
10
|
+
@type = validated_type(options[:type])
|
11
|
+
@title = options[:title]
|
12
|
+
@fallback = options[:fallback]
|
13
|
+
@dryrun = options[:dryrun] || (defined?(Rails) && Rails.env.test?)
|
14
|
+
@fields = validated_fields(options[:fields])
|
15
|
+
end
|
16
|
+
|
17
|
+
def data
|
18
|
+
@data = {}
|
19
|
+
@data['title'] = @title || 'Notification'
|
20
|
+
@data['fallback'] = @fallback || @data['title']
|
21
|
+
@data['fields'] = @fields if @fields.present?
|
22
|
+
@data['color'] = slack_color
|
23
|
+
@data['footer'] = Rails.env if defined?(Rails)
|
24
|
+
@data
|
25
|
+
end
|
26
|
+
|
27
|
+
def notify!
|
28
|
+
@dryrun ? data : notifier.post(attachments: [data])
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def notifier
|
34
|
+
raise "Missing notifier url for #{@channel}." if slack_urls[@channel].blank?
|
35
|
+
|
36
|
+
Slack::Notifier.new(slack_urls[@channel])
|
37
|
+
end
|
38
|
+
|
39
|
+
def validated_fields(fields)
|
40
|
+
if fields.is_a?(Hash)
|
41
|
+
fields = fields_from_hash(fields)
|
42
|
+
elsif fields.is_a?(String)
|
43
|
+
@title = fields
|
44
|
+
fields = []
|
45
|
+
elsif !fields.is_a?(Array)
|
46
|
+
raise ArgumentError, 'Unsupported fields format.'
|
47
|
+
end
|
48
|
+
|
49
|
+
fields.is_a?(Hash) ? fields_from_hash(fields) : fields
|
50
|
+
end
|
51
|
+
|
52
|
+
def fields_from_hash(hash)
|
53
|
+
hash.map do |title, value|
|
54
|
+
{ 'title' => title, 'value' => value, 'short' => true }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def slack_color
|
59
|
+
return '#CCCCCC' if @type.blank?
|
60
|
+
|
61
|
+
{
|
62
|
+
success: '#1086FF', info: '#99CEFF',
|
63
|
+
warning: '#FF6600', failure: '#BF0D3E'
|
64
|
+
}[@type]
|
65
|
+
end
|
66
|
+
|
67
|
+
def validated_channel(channel = nil)
|
68
|
+
channel = channel.delete('#')
|
69
|
+
unknown_channel if channel.present? && !channel.in?(slack_urls.keys)
|
70
|
+
|
71
|
+
channel = 'notifications' if channel.blank?
|
72
|
+
channel = 'test' unless Rails.env.production?
|
73
|
+
channel
|
74
|
+
end
|
75
|
+
|
76
|
+
def unknown_channel
|
77
|
+
raise ArgumentError, 'Unknown channel.'
|
78
|
+
end
|
79
|
+
|
80
|
+
def slack_urls
|
81
|
+
ENV.select { |k, _| k.match?(/SLACK_URL_/) }.map do |key, url|
|
82
|
+
{ key.gsub('SLACK_URL_', '').downcase => url }
|
83
|
+
end.reduce({}, :merge)
|
84
|
+
end
|
85
|
+
|
86
|
+
def validated_type(type = nil)
|
87
|
+
valid_types = %i[success info warning failure]
|
88
|
+
return type if type.blank? || type.in?(valid_types)
|
89
|
+
|
90
|
+
raise ArgumentError, 'Unrecognized notification type.'
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'slack-notification'
|
8
|
+
spec.version = '0.1.0'
|
9
|
+
spec.authors = ['Julian Fiander']
|
10
|
+
spec.email = ['julian@fiander.one']
|
11
|
+
|
12
|
+
spec.summary = 'A simplified Slack API.'
|
13
|
+
spec.description = 'A simplified Slack API for integration into Rails applications.'
|
14
|
+
spec.homepage = 'https://github.com/jfiander/slack-notification'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/jfiander/slack-notification'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
|
32
|
+
spec.add_dependency 'slack-notifier', '~> 2.3'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slack-notification
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julian Fiander
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-16 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
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.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: slack-notifier
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.3'
|
55
|
+
description: A simplified Slack API for integration into Rails applications.
|
56
|
+
email:
|
57
|
+
- julian@fiander.one
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- lib/slack_notification.rb
|
70
|
+
- slack-notification.gemspec
|
71
|
+
homepage: https://github.com/jfiander/slack-notification
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/jfiander/slack-notification
|
76
|
+
source_code_uri: https://github.com/jfiander/slack-notification
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.7.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A simplified Slack API.
|
97
|
+
test_files: []
|