rack-exception_notifier 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c74dd74a2958cfa551a3ef9b173147e027b35f2
4
- data.tar.gz: 3d83cd0869babccda39cac01dfdb4f84737a77ee
3
+ metadata.gz: d74186879da5241fb7327492cb6374f6b5172360
4
+ data.tar.gz: f2c670e0354515d0ed102d6c7c874ec4fd74194a
5
5
  SHA512:
6
- metadata.gz: bc17a8caf8ffd765ac0cc0a41f9870b6257782618e2e1163e11ae7a30721af061103c478b9713c21e31b12aa91ae5e02b1504b6cebb7d4e2121268f38cd88c93
7
- data.tar.gz: 7beefdccac76e1ec1f84152673334916b0d9559a95cbe456e54a43ce6276d178d4f7176b00cfc661aab99cb6959c5cc6f16810fbce771c729331556b35d8540e
6
+ metadata.gz: 9a5c62ac24cb6a9cf0d63472119caf7fdd847f9b001621aa3a9451e94ce4b2b5dc4421456d2bc5fff60923524e57a78c768743b40c2d16e0249ebcae94448d4e
7
+ data.tar.gz: 6a4de62c65be5c9ce3a4c139209a00e8fff01dd552857d048c85e5e44852efa1546a61cda1c61035fd4cfdc32cf97859943a0b66c6477e792b6c6ddf88e4583e
data/README.md CHANGED
@@ -22,6 +22,13 @@ use Rack::ExceptionNotifier,
22
22
  :subject => '[ERROR] %s'
23
23
  ```
24
24
 
25
+ ## Options
26
+
27
+ * `:to` - destination for email exceptions (required)
28
+ * `:from` - address to originate email exceptions from (default: running username)
29
+ * `:subject` - subject line for emails, %s will be interpolated with the exception #to_s (default: [ERROR] %s)
30
+ * `:include_body` - should the body of the request be included in the email (default: false)
31
+
25
32
  ## License
26
33
 
27
34
  Rack-exception_notifier is released under the [MIT license](http://www.opensource.org/licenses/MIT).
@@ -7,11 +7,16 @@ module Rack
7
7
  default_options = {
8
8
  :to => nil,
9
9
  :from => ENV['USER'] || 'rack@localhost',
10
- :subject => '[ERROR] %s'
10
+ :subject => '[ERROR] %s',
11
+ :include_body => false
11
12
  }
12
13
  @app = app
13
14
  @options = default_options.merge(options)
14
15
  @template = ERB.new(Template)
16
+
17
+ if @options[:to].nil?
18
+ raise ArgumentError.new('to address is required')
19
+ end
15
20
  end
16
21
 
17
22
  def call(env)
@@ -30,26 +35,29 @@ module Rack
30
35
  mail.deliver!
31
36
  end
32
37
 
38
+ def _body_present?(env)
39
+ env['rack.input'].size > 0
40
+ end
41
+
42
+ def _render_body?(env)
43
+ _body_present?(env) && @options[:include_body]
44
+ end
45
+
33
46
  def _extract_body(env)
34
47
  io = env['rack.input']
35
48
  io.rewind if io.respond_to?(:rewind)
36
- contents = io.read
37
- if contents.empty?
38
- false
39
- else
40
- contents
41
- end
49
+ io.read
42
50
  end
43
51
 
44
52
  Template = (<<-'EMAIL').gsub(/^ {4}/, '')
45
53
  A <%= exception.class.to_s %> occured: <%= exception.to_s %>
46
- <% if body = _extract_body(env) %>
54
+ <% if _render_body?(env) %>
47
55
 
48
56
  ===================================================================
49
57
  Request Body:
50
58
  ===================================================================
51
59
 
52
- <%= body.gsub(/^/, ' ') %>
60
+ <%= _extract_body(env).gsub(/^/, ' ') %>
53
61
  <% end %>
54
62
 
55
63
  ===================================================================
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class ExceptionNotifier
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -10,6 +10,14 @@ describe Rack::ExceptionNotifier do
10
10
  let(:env) { Rack::MockRequest.env_for("/foo", :method => 'GET') }
11
11
  let(:env_with_body) { Rack::MockRequest.env_for("/foo", :method => 'POST', :input => StringIO.new('somethingspecial')) }
12
12
 
13
+ describe 'initialize' do
14
+ it 'requires a to option' do
15
+ expect do
16
+ Rack::ExceptionNotifier.new(good_app, {})
17
+ end.to raise_error(ArgumentError, 'to address is required')
18
+ end
19
+ end
20
+
13
21
  describe 'call' do
14
22
  it 'does not send mail on success' do
15
23
  notifier = Rack::ExceptionNotifier.new(good_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
@@ -27,7 +35,6 @@ describe Rack::ExceptionNotifier do
27
35
  mail.to.should == ['bar@example.com']
28
36
  mail.from.should == ['noreply@example.com']
29
37
  mail.subject.should == 'testing - Test Message'
30
- mail.body.raw_source.should_not include('Request Body')
31
38
  end
32
39
 
33
40
  it 'sends mail as user by default' do
@@ -40,13 +47,34 @@ describe Rack::ExceptionNotifier do
40
47
  mail.from.should == [ENV['USER']]
41
48
  end
42
49
 
43
- it 'includes the body' do
50
+ it 'does not include body by default' do
44
51
  notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s')
45
52
  expect do
46
53
  notifier.call(env_with_body)
47
54
  end.to raise_error(TestError)
48
55
 
49
56
  mail = Mail::TestMailer.deliveries.first
57
+ mail.body.raw_source.should_not include('Request Body')
58
+ end
59
+
60
+ it 'does not include body if not present in request' do
61
+ notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s', :include_body => true)
62
+ expect do
63
+ notifier.call(env)
64
+ end.to raise_error(TestError)
65
+
66
+ mail = Mail::TestMailer.deliveries.first
67
+ mail.body.raw_source.should_not include('Request Body')
68
+ end
69
+
70
+ it 'includes the body if configured' do
71
+ notifier = Rack::ExceptionNotifier.new(bad_app, :to => 'bar@example.com', :from => 'noreply@example.com', :subject => 'testing - %s', :include_body => true)
72
+ expect do
73
+ notifier.call(env_with_body)
74
+ end.to raise_error(TestError)
75
+
76
+ mail = Mail::TestMailer.deliveries.first
77
+ mail.body.raw_source.should include('somethingspecial')
50
78
  mail.body.raw_source.should include('somethingspecial')
51
79
  end
52
80
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-exception_notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Downey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-12 00:00:00.000000000 Z
11
+ date: 2013-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail