safely 0.1.0pre → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,4 @@
1
+ 0.1 (WIP)
2
+
3
+ * Initial hoptoad support
4
+ * Initial email support
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ source "http://rubygems.org"
8
8
 
9
9
  group :development do
10
10
  gem "toadhopper"
11
+ gem "mail"
11
12
 
12
13
  gem "rspec", "~> 2.5.0"
13
14
  gem "yard", "~> 0.6.0"
data/Gemfile.lock CHANGED
@@ -1,13 +1,22 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.0.6)
4
5
  diff-lcs (1.1.2)
5
6
  git (1.2.5)
7
+ i18n (0.5.0)
6
8
  jeweler (1.5.2)
7
9
  bundler (~> 1.0.0)
8
10
  git (>= 1.2.5)
9
11
  rake
12
+ mail (2.2.15)
13
+ activesupport (>= 2.3.6)
14
+ i18n (>= 0.4.0)
15
+ mime-types (~> 1.16)
16
+ treetop (~> 1.4.8)
17
+ mime-types (1.16)
10
18
  mocha (0.9.12)
19
+ polyglot (0.3.1)
11
20
  rake (0.8.7)
12
21
  rcov (0.9.9)
13
22
  rspec (2.5.0)
@@ -19,6 +28,8 @@ GEM
19
28
  diff-lcs (~> 1.1.2)
20
29
  rspec-mocks (2.5.0)
21
30
  toadhopper (1.1)
31
+ treetop (1.4.9)
32
+ polyglot (>= 0.3.1)
22
33
  yard (0.6.7)
23
34
 
24
35
  PLATFORMS
@@ -27,6 +38,7 @@ PLATFORMS
27
38
  DEPENDENCIES
28
39
  bundler (~> 1.0.0)
29
40
  jeweler (~> 1.5.2)
41
+ mail
30
42
  mocha
31
43
  rcov
32
44
  rspec (~> 2.5.0)
data/README.rdoc CHANGED
@@ -1,46 +1,17 @@
1
1
  = safely
2
2
 
3
- Safely is a simple exception handling and alerting mechanism extract from the daemon-kit[http://github.com/kennethkalmer/daemon-kit] project.
3
+ Safely is a simple exception handling and alerting mechanism extract from the daemon-kit[https://github.com/kennethkalmer/daemon-kit] project.
4
4
 
5
- Safely currently supports error reporting to Hoptoad via the Toadhopper[http://github.com/toolmantim/toadhopper] gem. Future support will
6
- include reporting errors to log files and via email.
5
+ Safely currently supports error reporting to Hoptoad, or via email.
7
6
 
8
- == Installation
7
+ == Installation and Usage
9
8
 
10
- In your Gemfile
11
-
12
- gem "toadhopper", "~> 1.1"
13
- gem "safely", "~> 0.1"
14
-
15
- Or directly
16
-
17
- gem install toadhopper -v "~> 1.1"
18
- gem install safely -v "~> 0.1"
19
-
20
- Although Safely currently only supports Toadhopper, it doesn't specify it as a dependency. This allows for
21
- future strategies to be added and giving users the choice on which strategy to use without bloating their
22
- projects.
23
-
24
- == Usage
25
-
26
- require 'toadhopper'
27
- require 'safely'
28
-
29
- # Configure
30
- Safely.configure do |c|
31
- c.hoptoad_key = 'XXXXXXXX'
32
- end
33
-
34
- # Run your code
35
- safely do
36
- do_something_dangerous!
37
- end
9
+ Please refer to the wiki[https://github.com/kennethkalmer/safely/wiki] for installation and usage information.
38
10
 
39
11
  == TODO
40
12
 
41
- * Support for mail exceptions
42
13
  * Support for logging exceptions
43
- * Fine-tine strategies
14
+ * Fine-tune strategies
44
15
  * Support for trapping exceptions on exit
45
16
 
46
17
  == Contributing to safely
data/lib/safely/config.rb CHANGED
@@ -1,6 +1,24 @@
1
1
  module Safely
2
2
  class Config
3
- # Hoptoad API key
4
- attr_accessor :hoptoad_key
3
+
4
+ # List of strategies to use
5
+ attr_reader :strategies
6
+
7
+ def initialize
8
+ @strategies = [
9
+ Safely::Strategy::Hoptoad,
10
+ Safely::Strategy::Mail
11
+ ]
12
+ end
13
+
14
+ def hoptoad_key
15
+ $stderr.write "DEPRECATED: Call Safely::Strategy::Hoptoad.hoptoad_key"
16
+ Safely::Strategy::Hoptoad.hoptoad_key
17
+ end
18
+
19
+ def hoptoad_key=( key )
20
+ $stderr.write "DEPRECATED: Call Safely::Strategy::Hoptoad.hoptoad_key="
21
+ Safely::Strategy::Hoptoad.hoptoad_key=( key )
22
+ end
5
23
  end
6
24
  end
@@ -0,0 +1,28 @@
1
+ module Safely
2
+ module Strategy
3
+ class Hoptoad
4
+
5
+ class << self
6
+
7
+ # Hoptoad API key to use for reporting errors
8
+ attr_accessor :hoptoad_key
9
+
10
+ def load!
11
+ begin
12
+ require 'toadhopper'
13
+ rescue LoadError
14
+ $stderr.write( "'toadhopper' not available, safely cannot use the Hoptoad stategy" )
15
+ end
16
+ end
17
+
18
+ def report!( exception )
19
+ if defined?( Toadhopper ) && !self.hoptoad_key.nil?
20
+ Toadhopper( self.hoptoad_key ).post!( exception )
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,55 @@
1
+ module Safely
2
+ module Strategy
3
+ class Mail
4
+
5
+ class << self
6
+
7
+ # Recipient of the exception mails
8
+ attr_accessor :recipient
9
+
10
+ # The sender
11
+ attr_accessor :sender
12
+
13
+ # Subject prefix
14
+ attr_accessor :subject_prefix
15
+
16
+ def load!
17
+ begin
18
+ require 'mail'
19
+ rescue LoadError
20
+ $stderr.write( "'mail' not available, safely cannot use the email stategy" )
21
+ end
22
+
23
+ if self.sender.nil?
24
+ require 'socket'
25
+ self.sender = "safely@#{Socket.gethostname}"
26
+ end
27
+
28
+ self.subject_prefix ||= "[SAFELY]"
29
+ end
30
+
31
+ def report!( exception )
32
+ return if self.sender.nil? || self.recipient.nil?
33
+
34
+ mail = ::Mail.new
35
+ mail.from = self.sender
36
+ mail.to = self.recipient
37
+ mail.subject = "#{subject_prefix} - #{exception.message}"
38
+
39
+ mail.body = <<-EOF
40
+ Safely caught an unhandled exception in your application, details below:
41
+
42
+ Type: #{exception.class.name}
43
+ Message: #{exception.message}
44
+
45
+ Backtrace:
46
+ #{exception.backtrace.join("\n ")}
47
+ EOF
48
+
49
+ mail.deliver
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,6 @@
1
+ module Safely
2
+ module Strategy
3
+ autoload :Hoptoad, 'safely/strategy/hoptoad'
4
+ autoload :Mail, 'safely/strategy/mail'
5
+ end
6
+ end
data/lib/safely.rb CHANGED
@@ -2,10 +2,11 @@ $:.unshift File.expand_path('../', __FILE__)
2
2
 
3
3
  module Safely
4
4
 
5
- VERSION = "0.1.0pre"
5
+ VERSION = "0.1.0"
6
6
 
7
- autoload :Config, 'safely/config'
8
- autoload :Mixin, 'safely/mixin'
7
+ autoload :Config, 'safely/config'
8
+ autoload :Mixin, 'safely/mixin'
9
+ autoload :Strategy, 'safely/strategy'
9
10
 
10
11
  class << self
11
12
 
@@ -22,19 +23,13 @@ module Safely
22
23
  end
23
24
 
24
25
  def load_strategies!
25
- begin
26
- require 'toadhopper'
27
- rescue LoadError
28
- # Print warning
29
- end
26
+ @config.strategies.each { |s| s.load! }
30
27
  end
31
28
 
32
29
  def report!( exception )
33
- if defined?( Toadhopper ) && !config.hoptoad_key.nil?
34
- Toadhopper( config.hoptoad_key ).post!( exception )
35
- else
36
- STDERR.puts "Toadhopper not available or not configured!"
37
- end
30
+ load_strategies! if @config.nil?
31
+
32
+ @config.strategies.each { |s| s.report! exception }
38
33
  end
39
34
 
40
35
  private
data/spec/config_spec.rb CHANGED
@@ -2,7 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Safely::Config do
4
4
  describe "Hoptoad" do
5
- it { should respond_to(:hoptoad_key) }
6
- it { should respond_to(:hoptoad_key=) }
5
+ it { should respond_to(:strategies) }
7
6
  end
8
7
  end
data/spec/safely_spec.rb CHANGED
@@ -27,15 +27,18 @@ describe Safely do
27
27
 
28
28
  defined?( Toadhopper ).should be_true
29
29
  end
30
+
31
+ it "should load Mail if present" do
32
+ Safely.load_strategies!
33
+
34
+ defined?( Mail ).should be_true
35
+ end
30
36
  end
31
37
 
32
38
  describe "usage" do
33
39
  it "should report exceptions to hoptoad" do
34
- Safely.configure do |c|
35
- c.hoptoad_key = "foo"
36
- end
37
-
38
- Toadhopper.any_instance.expects(:post!)
40
+ Safely::Strategy::Hoptoad.expects(:report!)
41
+ Safely::Strategy::Mail.expects(:report!)
39
42
 
40
43
  safely do
41
44
  raise "Hello"
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,16 @@ require 'safely'
7
7
  # in ./support/ and its subdirectories.
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
+ require 'mail'
11
+ Mail.defaults do
12
+ delivery_method :test
13
+ end
14
+
15
+
10
16
  RSpec.configure do |config|
11
17
  config.mock_with(:mocha)
18
+
19
+ config.before(:each) do
20
+ Mail::TestMailer.deliveries.clear
21
+ end
12
22
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Safely::Strategy::Hoptoad do
4
+ it "should be configurable" do
5
+ Safely::Strategy::Hoptoad.should respond_to(:hoptoad_key)
6
+ Safely::Strategy::Hoptoad.should respond_to(:hoptoad_key=)
7
+ end
8
+
9
+ it "should report when configured" do
10
+ Safely::Strategy::Hoptoad.hoptoad_key = "foo"
11
+
12
+ Toadhopper.any_instance.expects(:post!)
13
+
14
+ safely do
15
+ raise "Argh"
16
+ end
17
+ end
18
+
19
+ it "should not bother when not configured" do
20
+ Safely::Strategy::Hoptoad.hoptoad_key = nil
21
+
22
+ Toadhopper.any_instance.expects(:post!).never
23
+
24
+ safely do
25
+ raise "Argh"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Safely::Strategy::Mail do
4
+ describe "configuration" do
5
+ %w( recipient sender subject_prefix ).each do |attr|
6
+ it "should support configuring #{attr}" do
7
+ Safely::Strategy::Mail.should respond_to( attr )
8
+ Safely::Strategy::Mail.should respond_to( "#{attr}=" )
9
+ end
10
+ end
11
+ end
12
+
13
+ describe "reporting" do
14
+ it "should be enabled when configured" do
15
+ Safely::Strategy::Mail.recipient = "kenneth.kalmer@gmail.com"
16
+
17
+ safely do
18
+ raise "Argh"
19
+ end
20
+
21
+ Mail::TestMailer.deliveries.should_not be_empty
22
+
23
+ mail = Mail::TestMailer.deliveries.last
24
+
25
+ mail.to_addrs.should include("kenneth.kalmer@gmail.com")
26
+ mail.body.should match(/Backtrace/)
27
+ end
28
+
29
+ it "should not bother if not configured" do
30
+ Safely::Strategy::Mail.recipient = nil
31
+
32
+ expect {
33
+ safely do
34
+ raise "Argh"
35
+ end
36
+ }.to_not change( Mail::TestMailer.deliveries, :length )
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safely
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: 5
5
- version: 0.1.0pre
4
+ prerelease:
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kenneth Kalmer
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-14 00:00:00 Z
13
+ date: 2011-05-07 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: toadhopper
@@ -24,8 +24,19 @@ dependencies:
24
24
  prerelease: false
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: rspec
27
+ name: mail
28
28
  requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &id003 !ruby/object:Gem::Requirement
29
40
  none: false
30
41
  requirements:
31
42
  - - ~>
@@ -33,10 +44,10 @@ dependencies:
33
44
  version: 2.5.0
34
45
  type: :development
35
46
  prerelease: false
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: yard
39
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirement: &id004 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ~>
@@ -44,10 +55,10 @@ dependencies:
44
55
  version: 0.6.0
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *id003
58
+ version_requirements: *id004
48
59
  - !ruby/object:Gem::Dependency
49
60
  name: bundler
50
- requirement: &id004 !ruby/object:Gem::Requirement
61
+ requirement: &id005 !ruby/object:Gem::Requirement
51
62
  none: false
52
63
  requirements:
53
64
  - - ~>
@@ -55,10 +66,10 @@ dependencies:
55
66
  version: 1.0.0
56
67
  type: :development
57
68
  prerelease: false
58
- version_requirements: *id004
69
+ version_requirements: *id005
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: jeweler
61
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ requirement: &id006 !ruby/object:Gem::Requirement
62
73
  none: false
63
74
  requirements:
64
75
  - - ~>
@@ -66,10 +77,10 @@ dependencies:
66
77
  version: 1.5.2
67
78
  type: :development
68
79
  prerelease: false
69
- version_requirements: *id005
80
+ version_requirements: *id006
70
81
  - !ruby/object:Gem::Dependency
71
82
  name: rcov
72
- requirement: &id006 !ruby/object:Gem::Requirement
83
+ requirement: &id007 !ruby/object:Gem::Requirement
73
84
  none: false
74
85
  requirements:
75
86
  - - ">="
@@ -77,10 +88,10 @@ dependencies:
77
88
  version: "0"
78
89
  type: :development
79
90
  prerelease: false
80
- version_requirements: *id006
91
+ version_requirements: *id007
81
92
  - !ruby/object:Gem::Dependency
82
93
  name: mocha
83
- requirement: &id007 !ruby/object:Gem::Requirement
94
+ requirement: &id008 !ruby/object:Gem::Requirement
84
95
  none: false
85
96
  requirements:
86
97
  - - ">="
@@ -88,7 +99,7 @@ dependencies:
88
99
  version: "0"
89
100
  type: :development
90
101
  prerelease: false
91
- version_requirements: *id007
102
+ version_requirements: *id008
92
103
  description: Safely run your code in a loving error reporting embrace
93
104
  email: kenneth.kalmer@gmail.com
94
105
  executables: []
@@ -101,6 +112,7 @@ extra_rdoc_files:
101
112
  files:
102
113
  - .document
103
114
  - .rspec
115
+ - CHANGES
104
116
  - Gemfile
105
117
  - Gemfile.lock
106
118
  - LICENSE.txt
@@ -110,10 +122,15 @@ files:
110
122
  - lib/safely.rb
111
123
  - lib/safely/config.rb
112
124
  - lib/safely/mixin.rb
125
+ - lib/safely/strategy.rb
126
+ - lib/safely/strategy/hoptoad.rb
127
+ - lib/safely/strategy/mail.rb
113
128
  - safely.gemspec
114
129
  - spec/config_spec.rb
115
130
  - spec/safely_spec.rb
116
131
  - spec/spec_helper.rb
132
+ - spec/strategies/hoptoad_spec.rb
133
+ - spec/strategies/mail_spec.rb
117
134
  homepage: http://github.com/kennethkalmer/safely
118
135
  licenses:
119
136
  - MIT
@@ -127,16 +144,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
144
  requirements:
128
145
  - - ">="
129
146
  - !ruby/object:Gem::Version
130
- hash: -1229273114892724216
147
+ hash: -2395968726711885681
131
148
  segments:
132
149
  - 0
133
150
  version: "0"
134
151
  required_rubygems_version: !ruby/object:Gem::Requirement
135
152
  none: false
136
153
  requirements:
137
- - - ">"
154
+ - - ">="
138
155
  - !ruby/object:Gem::Version
139
- version: 1.3.1
156
+ version: "0"
140
157
  requirements: []
141
158
 
142
159
  rubyforge_project:
@@ -148,3 +165,5 @@ test_files:
148
165
  - spec/config_spec.rb
149
166
  - spec/safely_spec.rb
150
167
  - spec/spec_helper.rb
168
+ - spec/strategies/hoptoad_spec.rb
169
+ - spec/strategies/mail_spec.rb