merb-exceptions 0.9.13 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -1
- data/lib/merb-exceptions.rb +12 -9
- data/lib/merb-exceptions/notification.rb +26 -29
- data/spec/notification_spec.rb +16 -31
- data/spec/spec_helper.rb +7 -6
- metadata +3 -3
data/README.markdown
CHANGED
@@ -16,8 +16,10 @@ Configuration goes in your projects `config/init.rb` file inside `Merb::BootLoad
|
|
16
16
|
:web_hooks => ['http://example.com'],
|
17
17
|
:email_addresses => ['hello@exceptions.com', 'user@myapp.com'],
|
18
18
|
:app_name => "My App Name",
|
19
|
+
:environments => ['production', 'staging'],
|
19
20
|
:email_from => "exceptions@myapp.com",
|
20
|
-
:
|
21
|
+
:mailer_config => nil,
|
22
|
+
:mailer_delivery_method => :sendmail
|
21
23
|
}
|
22
24
|
|
23
25
|
The plugin now automatically includes itself into your Exceptions controller. If you are using an old version of this plugin, you can remove the include from your Exceptions controller.
|
@@ -40,6 +42,10 @@ Settings
|
|
40
42
|
|
41
43
|
`environments`: Notifications will only be sent for environments in this list, defaults to `production`
|
42
44
|
|
45
|
+
`mailer_delivery_method`: The delivery method for notifications mailer, see merb-mailer documentation.
|
46
|
+
|
47
|
+
`mailer_config`: A hash of configuration options for the notifications mailer, see merb-mailer documentation.
|
48
|
+
|
43
49
|
Advanced usage
|
44
50
|
--------------
|
45
51
|
merb-exceptions will deliver exceptions for any unhandled exceptions (exceptions that do not have views defined in the `Exceptions` controller)
|
data/lib/merb-exceptions.rb
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
# make sure we're running inside Merb
|
2
2
|
if defined?(Merb::Plugins)
|
3
3
|
|
4
|
-
# Default configuration
|
5
|
-
Merb::Plugins.config[:exceptions] = {
|
6
|
-
:web_hooks => [],
|
7
|
-
:email_addresses => [],
|
8
|
-
:app_name => "Merb awesome Application",
|
9
|
-
:email_from => "exceptions@app.com",
|
10
|
-
:environments => ['production']
|
11
|
-
}.merge(Merb::Plugins.config[:exceptions] || {})
|
12
|
-
|
13
4
|
Merb::BootLoader.before_app_loads do
|
14
5
|
|
15
6
|
end
|
16
7
|
|
17
8
|
Merb::BootLoader.after_app_loads do
|
9
|
+
|
10
|
+
# Default configuration
|
11
|
+
Merb::Plugins.config[:exceptions] = {
|
12
|
+
:web_hooks => [],
|
13
|
+
:email_addresses => [],
|
14
|
+
:app_name => "Merb awesome Application",
|
15
|
+
:environments => ['production'],
|
16
|
+
:email_from => "exceptions@myapp.com",
|
17
|
+
:mailer_config => nil,
|
18
|
+
:mailer_delivery_method => :sendmail
|
19
|
+
}.merge(Merb::Plugins.config[:exceptions] || {})
|
20
|
+
|
18
21
|
if Object.const_defined?(:Exceptions)
|
19
22
|
Exceptions.send(:include, MerbExceptions::ExceptionsHelper)
|
20
23
|
end
|
@@ -1,14 +1,20 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
|
-
require
|
3
|
+
require 'erb'
|
4
|
+
require 'merb-mailer'
|
4
5
|
|
5
6
|
module MerbExceptions
|
6
7
|
class Notification
|
8
|
+
|
9
|
+
class Mailer < Merb::Mailer
|
10
|
+
end
|
11
|
+
|
7
12
|
attr_reader :details
|
8
13
|
|
9
14
|
def initialize(details = nil)
|
10
|
-
@details = details ||
|
11
|
-
|
15
|
+
@details = details || []
|
16
|
+
Mailer.config = Merb::Plugins.config[:exceptions][:mailer_config]
|
17
|
+
Mailer.delivery_method = Merb::Plugins.config[:exceptions][:mailer_delivery_method]
|
12
18
|
end
|
13
19
|
|
14
20
|
def deliver!
|
@@ -17,7 +23,6 @@ module MerbExceptions
|
|
17
23
|
end
|
18
24
|
|
19
25
|
def deliver_web_hooks!
|
20
|
-
return unless should_deliver_notifications?
|
21
26
|
Merb.logger.info "DELIVERING EXCEPTION WEB HOOKS"
|
22
27
|
web_hooks.each do |address|
|
23
28
|
post_hook(address)
|
@@ -25,10 +30,9 @@ module MerbExceptions
|
|
25
30
|
end
|
26
31
|
|
27
32
|
def deliver_emails!
|
28
|
-
return unless should_deliver_notifications?
|
29
33
|
Merb.logger.info "DELIVERING EXCEPTION EMAILS"
|
30
34
|
email_addresses.each do |address|
|
31
|
-
|
35
|
+
send_email(address)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
@@ -38,13 +42,8 @@ module MerbExceptions
|
|
38
42
|
|
39
43
|
def environments; option_as_array(:environments); end
|
40
44
|
|
41
|
-
|
42
|
-
def should_deliver_notifications?
|
43
|
-
environments.include? Merb.env
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
45
|
def params
|
46
|
+
@params ||=
|
48
47
|
{
|
49
48
|
'request_url' => details['url'],
|
50
49
|
'request_controller' => details['params'][:controller],
|
@@ -52,7 +51,7 @@ module MerbExceptions
|
|
52
51
|
'request_params' => details['params'],
|
53
52
|
'environment' => details['environment'],
|
54
53
|
'exceptions' => details['exceptions'],
|
55
|
-
'app_name' =>
|
54
|
+
'app_name' => Merb::Plugins.config[:exceptions][:app_name]
|
56
55
|
}
|
57
56
|
end
|
58
57
|
|
@@ -65,31 +64,29 @@ module MerbExceptions
|
|
65
64
|
Net::HTTP.post_form( uri, params ).body
|
66
65
|
end
|
67
66
|
|
68
|
-
def
|
67
|
+
def email_body
|
68
|
+
@body ||= begin
|
69
|
+
path = File.join(File.dirname(__FILE__), 'templates', 'email.erb')
|
70
|
+
template = Erubis::Eruby.new(File.open(path,'r') { |f| f.read })
|
71
|
+
template.result(binding)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def send_email(address)
|
69
76
|
Merb.logger.info "- emailing to #{address}"
|
70
|
-
email =
|
77
|
+
email = Mailer.new({
|
71
78
|
:to => address,
|
72
|
-
:from =>
|
73
|
-
:subject => "[#{
|
74
|
-
:text =>
|
79
|
+
:from => Merb::Plugins.config[:exceptions][:email_from],
|
80
|
+
:subject => "[#{Merb::Plugins.config[:exceptions][:app_name]} EXCEPTION]",
|
81
|
+
:text => email_body
|
75
82
|
})
|
76
83
|
email.deliver!
|
77
84
|
end
|
78
85
|
|
79
|
-
def send_notification_email(address)
|
80
|
-
send_email(address, plain_text_mail_body)
|
81
|
-
end
|
82
|
-
|
83
|
-
def plain_text_mail_body
|
84
|
-
path = File.join(File.dirname(__FILE__), 'templates', 'email.erb')
|
85
|
-
template = Erubis::Eruby.new(File.open(path,'r') { |f| f.read })
|
86
|
-
template.result(binding)
|
87
|
-
end
|
88
|
-
|
89
86
|
# Used so that we can accept either a single value or array (e.g. of
|
90
87
|
# webhooks) in our YAML file.
|
91
88
|
def option_as_array(option)
|
92
|
-
value =
|
89
|
+
value = Merb::Plugins.config[:exceptions][option]
|
93
90
|
case value
|
94
91
|
when Array
|
95
92
|
value.reject { |e| e.nil? } # Don't accept nil values
|
data/spec/notification_spec.rb
CHANGED
@@ -8,37 +8,40 @@ describe MerbExceptions::Notification do
|
|
8
8
|
it "should create a new notification without errors" do
|
9
9
|
lambda { Notification.new(mock_details) }.should_not raise_error
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "should set the detail values to those provided" do
|
13
13
|
Notification.new(mock_details).details.should == mock_details
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
describe ".deliver!" do
|
18
18
|
before :each do
|
19
19
|
@notification = Notification.new(mock_details)
|
20
|
-
@notification.stub!('deliver_emails!')
|
21
20
|
@notification.stub!('deliver_web_hooks!')
|
22
21
|
end
|
23
|
-
|
22
|
+
|
23
|
+
after :each do
|
24
|
+
Notification::Mailer.deliveries.clear
|
25
|
+
end
|
26
|
+
|
24
27
|
it "should deliver web hooks" do
|
25
28
|
@notification.should_receive('deliver_web_hooks!')
|
26
29
|
@notification.deliver!
|
27
30
|
end
|
28
31
|
|
29
32
|
it "should deliver emails" do
|
30
|
-
|
33
|
+
Notification::Mailer.deliveries.length.should == 0
|
31
34
|
@notification.deliver!
|
35
|
+
Notification::Mailer.deliveries.length.should == 2
|
32
36
|
end
|
33
37
|
end
|
34
|
-
|
38
|
+
|
35
39
|
describe ".deliver_web_hooks!" do
|
36
40
|
before :each do
|
37
|
-
mock_merb_config({:web_hooks => ['http://www.test1.com', 'http://www.test2.com']})
|
38
41
|
@notification = Notification.new(mock_details)
|
39
42
|
@notification.stub!(:post_hook)
|
40
43
|
end
|
41
|
-
|
44
|
+
|
42
45
|
it "should call post_hook for each url" do
|
43
46
|
@notification.should_receive(:post_hook).
|
44
47
|
once.with('http://www.test1.com')
|
@@ -50,35 +53,17 @@ describe MerbExceptions::Notification do
|
|
50
53
|
|
51
54
|
describe ".deliver_emails!" do
|
52
55
|
before :each do
|
53
|
-
mock_merb_config({:email_addresses => ['user1@test.com', 'user2@test.com']})
|
54
56
|
@notification = Notification.new(mock_details)
|
55
|
-
|
57
|
+
Notification::Mailer.deliveries.clear
|
56
58
|
end
|
57
59
|
|
58
60
|
it "should call send_notification_email for each address" do
|
59
|
-
@notification.should_receive(:send_notification_email).
|
60
|
-
once.with('user1@test.com')
|
61
|
-
@notification.should_receive(:send_notification_email).
|
62
|
-
once.with('user2@test.com')
|
63
61
|
@notification.deliver_emails!
|
62
|
+
Notification::Mailer.deliveries.first.to.should include("user1@test.com")
|
63
|
+
Notification::Mailer.deliveries.first.from.should include("exceptions@myapp.com")
|
64
|
+
Notification::Mailer.deliveries.last.to.should include("user2@test.com")
|
65
|
+
Notification::Mailer.deliveries.first.text.should == Notification::Mailer.deliveries.last.text
|
64
66
|
end
|
65
67
|
end
|
66
|
-
|
67
|
-
# Running tests with test environment
|
68
|
-
describe ".should_deliver_notifications?" do
|
69
|
-
it "should return true if the current environment is on the config[:environments] list of one item" do
|
70
|
-
mock_merb_config({ :environments => 'test' })
|
71
|
-
Notification.new(mock_details).should_deliver_notifications?.should be_true
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should return true if the current environment is on the config[:environments] list as an array" do
|
75
|
-
mock_merb_config({ :environments => ['staging', 'test'] })
|
76
|
-
Notification.new(mock_details).should_deliver_notifications?.should be_true
|
77
|
-
end
|
78
68
|
|
79
|
-
it "should return false if the current environment is not on the config[:environments] list" do
|
80
|
-
mock_merb_config({ :environments => ['staging', 'development'] })
|
81
|
-
Notification.new(mock_details).should_deliver_notifications?.should be_false
|
82
|
-
end
|
83
|
-
end
|
84
69
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -24,7 +24,12 @@ class Exceptions < Application
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
Merb::Plugins.config[:exceptions]
|
27
|
+
Merb::Plugins.config[:exceptions] = {
|
28
|
+
:email_addresses => ['user1@test.com', 'user2@test.com'],
|
29
|
+
:web_hooks => ['http://www.test1.com', 'http://www.test2.com'],
|
30
|
+
:environments => ['test'],
|
31
|
+
:mailer_delivery_method => :test_send
|
32
|
+
}
|
28
33
|
Merb.start :environment => 'test'
|
29
34
|
|
30
35
|
module Merb
|
@@ -81,14 +86,10 @@ end
|
|
81
86
|
module NotificationSpecHelper
|
82
87
|
def mock_details(opts={})
|
83
88
|
{
|
84
|
-
'
|
89
|
+
'exceptions' => [],
|
85
90
|
'params' => { :controller=>'errors', :action=>'show' },
|
86
91
|
'environment' => { 'key1'=>'value1', 'key2'=>'value2' },
|
87
92
|
'url' => 'http://www.my-app.com/errors/1'
|
88
93
|
}.merge(opts)
|
89
94
|
end
|
90
|
-
|
91
|
-
def mock_merb_config(opts={})
|
92
|
-
Merb::Plugins.config[:exceptions].merge!(opts)
|
93
|
-
end
|
94
95
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-exceptions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0
|
4
|
+
version: "1.0"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Kent
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-08 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0
|
23
|
+
version: "1.0"
|
24
24
|
version:
|
25
25
|
description: Email and web hook exceptions for Merb.
|
26
26
|
email: andy@new-bamboo.co.uk
|