tocsin 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tocsin/config.rb CHANGED
@@ -1,18 +1,15 @@
1
1
  module Tocsin
2
2
  class Config
3
- attr_accessor :exception_level, :from_address, :logger, :queue, :recipient_groups
3
+ attr_accessor :mailer, :mailer_method, :exception_level, :from_address, :logger, :queue, :recipient_groups
4
4
 
5
5
  def initialize
6
6
  @exception_level = StandardError
7
7
  @logger = select_logger
8
+ @mailer = select_mailer
9
+ @mailer_method = select_mailer_method
8
10
  @queue = :high
9
11
  end
10
12
 
11
- def select_logger
12
- rails = defined?(Rails) && Rails.respond_to?(:logger)
13
- rails ? Rails.logger : Logger.new($stderr)
14
- end
15
-
16
13
  # notify [r1, r2], :of => filters, :by => notifier
17
14
  def notify(recipients, parameters)
18
15
  self.recipient_groups ||= []
@@ -25,5 +22,22 @@ module Tocsin
25
22
  :notifier => notifier}.merge(filters)
26
23
  self.recipient_groups.push(group_config)
27
24
  end
25
+
26
+ private
27
+
28
+ def select_logger
29
+ rails = defined?(Rails) && Rails.respond_to?(:logger)
30
+ rails ? Rails.logger : Logger.new($stderr)
31
+ end
32
+
33
+ def select_mailer
34
+ action_mailer = defined?(ActionMailer::Base) && ActionMailer::Base.respond_to?(:mail)
35
+ action_mailer ? ActionMailer::Base : Mail
36
+ end
37
+
38
+ def select_mailer_method
39
+ action_mailer = defined?(ActionMailer::Base) && mailer.ancestors.include?(ActionMailer::Base)
40
+ action_mailer ? :mail : :new
41
+ end
28
42
  end
29
43
  end
@@ -4,24 +4,50 @@ module Tocsin
4
4
  module Notifiers
5
5
  class EmailNotifier
6
6
  def self.notify(recipients, alert)
7
- _body = "
8
- Alert raised by Tocsin on your site:
9
- \n
10
- Message: #{alert.message}
11
- Category: #{alert.category}
12
- Severity: #{alert.severity}
13
- Exception: #{alert.exception}
14
- Backtrace: #{alert.backtrace}
15
- ";
16
-
17
- from_address = Tocsin.config.from_address || recipients.first
18
-
19
- Mail.deliver do
20
- from from_address
21
- to recipients.join(", ")
22
- subject "[Tocsin] [#{alert.severity}] #{alert.message} (#{alert.category})"
23
- body _body
24
- end
7
+ attrs = { :from => sender(recipients),
8
+ :to => recipients,
9
+ :subject => subject(alert),
10
+ :body => compose(alert) }
11
+
12
+ message = mail(attrs)
13
+ message.deliver
14
+ end
15
+
16
+ private
17
+
18
+ def self.mail(attrs)
19
+ mailer.send mailer_method, attrs
20
+ end
21
+
22
+ def self.config
23
+ Tocsin.config
24
+ end
25
+
26
+ def self.mailer
27
+ config.mailer
28
+ end
29
+
30
+ def self.mailer_method
31
+ config.mailer_method
32
+ end
33
+
34
+ def self.sender(recipients)
35
+ config.from_address || recipients.first
36
+ end
37
+
38
+ def self.subject(alert)
39
+ "[Tocsin] [#{alert.severity}] #{alert.message} (#{alert.category})"
40
+ end
41
+
42
+ def self.compose(alert)
43
+ _body = []
44
+ _body << "Alert raised by Tocsin on your site:\n"
45
+ _body << "Message: #{alert.message}"
46
+ _body << "Category: #{alert.category}"
47
+ _body << "Severity: #{alert.severity}"
48
+ _body << "Exception: #{alert.exception}"
49
+ _body << "Backtrace: #{alert.backtrace}"
50
+ _body.join("\n")
25
51
  end
26
52
  end
27
53
 
@@ -1,3 +1,3 @@
1
1
  module Tocsin
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -7,6 +7,65 @@ describe Tocsin::Notifiers::EmailNotifier do
7
7
  :backtrace => 'backtrace')
8
8
  }
9
9
 
10
+ context "configurable mailer" do
11
+
12
+ let(:action_mailer_class) { ActionMailer::Base }
13
+ let(:alt_mailer_class) { Foo }
14
+ let(:message) { stub('message') }
15
+
16
+ before(:all) do
17
+ unless defined?(ActionMailer::Base)
18
+ ACTION_MAILER_LOCALLY_DEFINED = true
19
+ module ActionMailer
20
+ class Base
21
+ def self.mail(*args)
22
+ Mail.new(*args)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ class Foo < ActionMailer::Base; end
29
+ end
30
+
31
+ after(:all) do
32
+ if defined?(ACTION_MAILER_LOCALLY_DEFINED)
33
+ # reset to default configuration after everything.
34
+ ActionMailer.send(:remove_const, :Base)
35
+ Object.send(:remove_const, :ActionMailer)
36
+ Tocsin.configure {}
37
+ end
38
+ end
39
+
40
+ it "uses ActionMailer::Base by default when available" do
41
+ Tocsin.configure {}
42
+ message.expects(:deliver).returns(true)
43
+ action_mailer_class.expects(:mail).returns(message)
44
+ described_class.notify(["a@b.com"], alert)
45
+ end
46
+
47
+ it "uses the specified mailer when configured" do
48
+ Tocsin.configure do |c|
49
+ c.mailer = alt_mailer_class
50
+ end
51
+
52
+ message.expects(:deliver).returns(true)
53
+ alt_mailer_class.expects(:mail).returns(message)
54
+ described_class.notify(["a@b.com"], alert)
55
+ end
56
+
57
+ it "uses the specified mail_method when configured" do
58
+ Tocsin.configure do |c|
59
+ c.mailer = alt_mailer_class
60
+ c.mailer_method = :some_dumb_method_name
61
+ end
62
+
63
+ message.expects(:deliver).returns(true)
64
+ alt_mailer_class.expects(:some_dumb_method_name).returns(message)
65
+ described_class.notify(["a@b.com"], alert)
66
+ end
67
+ end
68
+
10
69
  describe "notifying" do
11
70
  let(:origin_email) { "test@test.com" }
12
71
 
data/spec/tocsin_spec.rb CHANGED
@@ -221,11 +221,15 @@ describe Tocsin do
221
221
  end
222
222
 
223
223
  context "common communication errors" do
224
+ let(:mail) { mock('mail') }
225
+
224
226
  before do
225
227
  Tocsin.configure do |c|
226
228
  c.notify ["a@b.com"], :of => { :severity => /.*/ }
227
229
  c.logger = Logger.new('/dev/null')
228
230
  end
231
+
232
+ Mail.expects(:new).returns(mail)
229
233
  end
230
234
 
231
235
  errors = [ Timeout::Error,
@@ -237,7 +241,7 @@ describe Tocsin do
237
241
  errors.each do |err|
238
242
  it "logs #{err.to_s} without exploding" do
239
243
  alert_options.merge!(now: true)
240
- Mail.expects(:deliver).raises(err)
244
+ mail.expects(:deliver).raises(err)
241
245
  Tocsin.logger.expects(:error)
242
246
  expect { alert }.to_not raise_error
243
247
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tocsin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-15 00:00:00.000000000 Z
13
+ date: 2013-04-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec