slack_error_notifier 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 +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/slack_error_notifier.rb +48 -0
- data/lib/slack_error_notifier/helpers/configuration.rb +20 -0
- data/lib/slack_error_notifier/slack_configuration.rb +49 -0
- data/lib/slack_error_notifier/slack_notification.rb +58 -0
- data/lib/slack_error_notifier/version.rb +3 -0
- data/slack_error_notifier.gemspec +35 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11890433ad0182185133f93ec7f7694572575088
|
4
|
+
data.tar.gz: 1e65700e8010ddc09cbd27e846e48c17d184239f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb8e1c64d9d734c15363028141f75e580f5fa52886a1b35e404c86d877291b9823ef88d3d4092dc27d960e06ade208454addd7c0cf7f243d71befc97ea5ae95b
|
7
|
+
data.tar.gz: 05f6e1746f2e1580773b71eb88f423417ff8cd48b56267002de08e7a93df570fb551256eed5102e918bda4443e4371f980d5307017986f4f8c4821578662e684
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Write your name
|
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,55 @@
|
|
1
|
+
# Slack Error Notifier
|
2
|
+
|
3
|
+
Simple Slack exception notification for Ruby scripts. Just wrap your code in `SlackErrorNotifier.with_notifications {}` and off you go.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'slack_error_notifier'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install slack_error_notifier
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Configuration
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
SlackErrorNotifier.configuration do |config|
|
27
|
+
config.app_name = <THE NAME OF YOUR APP OR SCRIPT>
|
28
|
+
config.access_token = <YOUR SLACK TOKEN>
|
29
|
+
config.target_channel = <THE CHANNEL OR USER TO POST MESSAGES TO> # include '@' or '#' as applicable
|
30
|
+
confg.send_as_user = <TRUE/FALSE> # whether you want to send as slackbot or the user whose token you're using.
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Pro-tip: Add a bot to your Slack organization and then use its API Token. You can then configure the avatar and username you want messages to come from.
|
35
|
+
|
36
|
+
### Catching Exceptions in Your Code
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
SlackErrorNotifier.with_notifications do
|
40
|
+
# Commence dangerous operations here:
|
41
|
+
NuclearLaunchCode.run!
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Your target user or channel will then be notified of any unhandled exceptions that occur during the dangerous operation.
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cgrdavies/slack_error_notifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
50
|
+
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
55
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "slack_error_notifier"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'slack-ruby-client'
|
2
|
+
|
3
|
+
require "slack_error_notifier/version"
|
4
|
+
require "slack_error_notifier/helpers/configuration"
|
5
|
+
require "slack_error_notifier/slack_configuration"
|
6
|
+
require "slack_error_notifier/slack_notification"
|
7
|
+
|
8
|
+
|
9
|
+
module SlackErrorNotifier
|
10
|
+
extend Configuration
|
11
|
+
|
12
|
+
define_setting :access_token
|
13
|
+
define_setting :target_channel
|
14
|
+
define_setting :send_as_user
|
15
|
+
define_setting :app_name
|
16
|
+
|
17
|
+
def self.with_notifications
|
18
|
+
check_configuration(block_given?)
|
19
|
+
configure_slack_client
|
20
|
+
yield
|
21
|
+
rescue => error
|
22
|
+
puts "Encountered #{error}. Sending Notification to #{target_channel}."
|
23
|
+
send_notification(error)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure_slack_client
|
27
|
+
@slack_configuration = SlackConfiguration.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.slack_client
|
31
|
+
@slack_configuration.slack_client
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.send_notification(error)
|
35
|
+
SlackNotification.send(error)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.check_configuration(block_given)
|
39
|
+
raise ConfigurationError::NoBlockGivenError unless block_given
|
40
|
+
raise ConfigurationError::MissingConfiguration unless access_token && target_channel
|
41
|
+
end
|
42
|
+
|
43
|
+
module ConfigurationError
|
44
|
+
class NoBlockGivenError < StandardError; end
|
45
|
+
class MissingConfiguration < StandardError; end
|
46
|
+
class InvalidChannel < StandardError; end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Configuration
|
2
|
+
def configuration
|
3
|
+
yield self
|
4
|
+
end
|
5
|
+
def define_setting(name, default = nil)
|
6
|
+
class_variable_set("@@#{name}", default)
|
7
|
+
define_class_method "#{name}=" do |value|
|
8
|
+
class_variable_set("@@#{name}", value)
|
9
|
+
end
|
10
|
+
define_class_method name do
|
11
|
+
class_variable_get("@@#{name}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
private
|
15
|
+
def define_class_method(name, &block)
|
16
|
+
(class << self; self; end).instance_eval do
|
17
|
+
define_method name, &block
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module SlackErrorNotifier
|
2
|
+
class SlackConfiguration
|
3
|
+
attr_reader :slack_token, :target_channel
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@slack_token = SlackErrorNotifier.access_token
|
7
|
+
@target_channel = SlackErrorNotifier.target_channel
|
8
|
+
configure_slack_client
|
9
|
+
check_authentication
|
10
|
+
check_target_channel_validity
|
11
|
+
end
|
12
|
+
|
13
|
+
def slack_client
|
14
|
+
@slack_client ||= Slack::Web::Client.new
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def check_authentication
|
20
|
+
slack_client.auth_test
|
21
|
+
end
|
22
|
+
|
23
|
+
def check_target_channel_validity
|
24
|
+
valid_channel = channel_is_user? ? check_user_exists : check_channel_exists
|
25
|
+
raise ConfigurationError::InvalidChannel unless valid_channel
|
26
|
+
end
|
27
|
+
|
28
|
+
def channel_is_user?
|
29
|
+
target_channel.start_with?('@')
|
30
|
+
end
|
31
|
+
|
32
|
+
def check_user_exists
|
33
|
+
slack_client.users_info(user: target_channel)
|
34
|
+
rescue Slack::Web::Api::Error
|
35
|
+
return false
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_channel_exists
|
39
|
+
channels = slack_client.channels_list.channels
|
40
|
+
matching_channel = channels.detect { |c| c.name == target_channel.gsub('#', '') }
|
41
|
+
end
|
42
|
+
|
43
|
+
def configure_slack_client
|
44
|
+
Slack.configure do |config|
|
45
|
+
config.token = slack_token
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module SlackErrorNotifier
|
2
|
+
class SlackNotification
|
3
|
+
def initialize(error)
|
4
|
+
@error = error
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.send(error)
|
8
|
+
new(error).send
|
9
|
+
end
|
10
|
+
|
11
|
+
def send
|
12
|
+
slack_client.chat_postMessage(
|
13
|
+
channel: target_channel,
|
14
|
+
text: "",
|
15
|
+
attachments: attachments,
|
16
|
+
as_user: true
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def slack_client
|
23
|
+
SlackErrorNotifier.slack_client
|
24
|
+
end
|
25
|
+
|
26
|
+
def target_channel
|
27
|
+
SlackErrorNotifier.target_channel
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_as_user
|
31
|
+
SlackErrorNotifier.send_as_user
|
32
|
+
end
|
33
|
+
|
34
|
+
def attachments
|
35
|
+
[{
|
36
|
+
fallback: "Error in #{app_name}",
|
37
|
+
color: "danger",
|
38
|
+
title: "Error in #{app_name}!",
|
39
|
+
text: assemble_text_body,
|
40
|
+
mrkdwn_in: ['text'],
|
41
|
+
thumb_url: 'http://i.imgur.com/ILN6Klm.gif',
|
42
|
+
fields: [{
|
43
|
+
"title": "Project",
|
44
|
+
"value": "#{app_name}",
|
45
|
+
"short": true
|
46
|
+
}]
|
47
|
+
}]
|
48
|
+
end
|
49
|
+
|
50
|
+
def assemble_text_body
|
51
|
+
"#{Time.new.strftime("At %I:%M%p")}, an error occurred with the following stack trace: ```#{@error.backtrace.join("\n")}```"
|
52
|
+
end
|
53
|
+
|
54
|
+
def app_name
|
55
|
+
SlackErrorNotifier.app_name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slack_error_notifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slack_error_notifier"
|
8
|
+
spec.version = SlackErrorNotifier::VERSION
|
9
|
+
spec.authors = ["Chris Davies"]
|
10
|
+
spec.email = ["cgrdavies@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Sends exceptions to a slack channel or user.}
|
13
|
+
spec.description = %q{Wrap your code in our block and let us send error messages when you screw up. }
|
14
|
+
spec.homepage = "https://github.com/cgrdavies/slack_error_notifier"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "minitest"
|
33
|
+
|
34
|
+
spec.add_runtime_dependency "slack-ruby-client"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slack_error_notifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Davies
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-25 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: slack-ruby-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 'Wrap your code in our block and let us send error messages when you
|
70
|
+
screw up. '
|
71
|
+
email:
|
72
|
+
- cgrdavies@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/slack_error_notifier.rb
|
87
|
+
- lib/slack_error_notifier/helpers/configuration.rb
|
88
|
+
- lib/slack_error_notifier/slack_configuration.rb
|
89
|
+
- lib/slack_error_notifier/slack_notification.rb
|
90
|
+
- lib/slack_error_notifier/version.rb
|
91
|
+
- slack_error_notifier.gemspec
|
92
|
+
homepage: https://github.com/cgrdavies/slack_error_notifier
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata:
|
96
|
+
allowed_push_host: https://rubygems.org
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.4.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Sends exceptions to a slack channel or user.
|
117
|
+
test_files: []
|