aanand-merb_hoptoad_notifier 1.0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Corey Donohoe<atmos@atmos.org>, Joakim Kolsjö<trejje@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,59 @@
1
+ merb_hoptoad_notifier
2
+ ---------------------------------------------
3
+ This is merb plugin for exception notification with hoptoad. It should work with
4
+ any merb app that's based on merb 1.0 and higher
5
+
6
+ This plugin: http://github.com/atmos/merb_hoptoad_notifier/tree/master
7
+ The original: http://github.com/thoughtbot/hoptoad_notifier/tree/master
8
+
9
+ Usage:
10
+
11
+ 1) Get your api key for your app from hoptoadapp.com
12
+
13
+ 2) Add the api key to config/hoptoad.yml with a similar syntax as the following
14
+ ---
15
+ :development: &defaults
16
+ :api_key: ZOMGLOLROFLMAO
17
+
18
+ :rake:
19
+ <<: *defaults
20
+
21
+ :test:
22
+ <<: *defaults
23
+
24
+ :production:
25
+ :api_key: UBERSECRETSHIT
26
+
27
+
28
+ 3) Require hoptoad in init.rb
29
+ require 'merb_hoptoad_notifier'
30
+
31
+ 4) Add the following method to your Exceptions controller. Depending on your merb version you'll need to use the exceptions,standard_error, or internal_server error as the action name. Kinda weak, but the API changed a lot in 0.9.x
32
+
33
+ class Exceptions < Merb::Controller
34
+ if %w( staging production ).include?(Merb.env)
35
+ def standard_error
36
+ HoptoadNotifier.notify_hoptoad(request, session)
37
+ render
38
+ end
39
+ end
40
+ end
41
+
42
+ 5) Restart the server, trigger an error(in staging or prod) and check that it arrived at hoptoad :)
43
+
44
+
45
+ Filtersing your post environment
46
+ --------------------------------
47
+ If you have environmental variables set in your ruby process that should not be sent to hoptoad,
48
+ there's a mechanism for filtering those attributes now. Throw something like this in
49
+ config/init.rb
50
+
51
+ Merb::BootLoader.after_app_loads do
52
+ HoptoadNotifier.environment_filters = %w(^AWS ^EC2 SECRET PRIVATE KEY)
53
+ end
54
+
55
+ Each of these words will be compiled into a regex so you should be able to use anchors if needed.
56
+
57
+ Thanks to the following GitHubbers
58
+ ----------------------------------
59
+ joakimk, fairchild and cv.
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'merb-core/version'
6
+ require 'merb-core/tasks/merb_rake_helper'
7
+ require 'spec/rake/spectask'
8
+
9
+ NAME = "merb_hoptoad_notifier"
10
+ GEM_VERSION = "1.0.5"
11
+ AUTHOR = "Corey Donohoe"
12
+ EMAIL = "atmos@atmos.org"
13
+ HOMEPAGE = "http://github.com/atmos"
14
+ SUMMARY = "Merb plugin that provides hoptoad exception notification"
15
+
16
+ spec = Gem::Specification.new do |s|
17
+ s.rubyforge_project = 'merb'
18
+ s.name = NAME
19
+ s.version = GEM_VERSION
20
+ s.platform = Gem::Platform::RUBY
21
+ s.has_rdoc = true
22
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
23
+ s.summary = SUMMARY
24
+ s.description = s.summary
25
+ s.author = AUTHOR
26
+ s.email = EMAIL
27
+ s.homepage = HOMEPAGE
28
+ # s.add_dependency('merb', '>= 0.9.7')
29
+ # s.add_dependency('extlib', '>= 0.9.6')
30
+ s.require_path = 'lib'
31
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ desc "install the plugin locally"
39
+ task :install => [:package] do
40
+ sh %{#{sudo} gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
41
+ end
42
+
43
+ desc "create a gemspec file"
44
+ task :make_spec do
45
+ File.open("#{NAME}.gemspec", "w") do |file|
46
+ file.puts spec.to_ruby
47
+ end
48
+ end
49
+
50
+ namespace :jruby do
51
+ desc "Run :package and install the resulting .gem with jruby"
52
+ task :install => :package do
53
+ sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
54
+ end
55
+ end
56
+
57
+ Spec::Rake::SpecTask.new(:default) do |t|
58
+ t.spec_opts << %w(-fs --color) << %w(-O spec/spec.opts)
59
+ t.spec_opts << '--loadby' << 'random'
60
+ t.spec_files = Dir["spec/*_spec.rb"]
61
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
62
+ t.rcov_opts << '--exclude' << '.gem/'
63
+
64
+ t.rcov_opts << '--text-summary'
65
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
66
+ t.rcov_opts << '--only-uncovered'
67
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO:
2
+ See if there's any more rich information we could provide to hoptoad
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__)+'/merb_hoptoad_notifier/hoptoad_notifier')
2
+ require File.expand_path(File.dirname(__FILE__)+'/merb_hoptoad_notifier/hoptoad_mixin')
3
+
4
+ # make sure we're running inside Merb
5
+ if defined?(Merb::Plugins)
6
+ Merb::BootLoader.after_app_loads do
7
+ HoptoadNotifier.configure
8
+ end
9
+ Merb::Plugins.add_rakefiles "merb_hoptoad_notifier/merbtasks"
10
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__)+'/hoptoad_notifier')
2
+
3
+ module HoptoadMixin
4
+ def notify_hoptoad(request=nil, session=nil)
5
+ request ||= self.request
6
+ session ||= self.session
7
+
8
+ HoptoadNotifier.notify_hoptoad(request, session)
9
+ end
10
+
11
+ def warn_hoptoad(message, request=nil, session=nil, options={})
12
+ request ||= self.request
13
+ session ||= self.session
14
+
15
+ HoptoadNotifier.warn_hoptoad(message, request, session, options)
16
+ end
17
+ end
@@ -0,0 +1,132 @@
1
+ require 'net/http'
2
+
3
+ module HoptoadNotifier
4
+ class << self
5
+ attr_accessor :api_key, :logger
6
+
7
+ def configure
8
+ key = YAML.load_file(Merb.root / 'config' / 'hoptoad.yml')
9
+ if key
10
+ env = key[Merb.env.to_sym]
11
+ env ? @api_key = env[:api_key] : raise(ArgumentError, "No hoptoad key for Merb environment #{Merb.env}")
12
+ end
13
+ end
14
+
15
+ def logger
16
+ @logger || Merb.logger
17
+ end
18
+
19
+ def environment_filters
20
+ @environment_filters ||= %w(AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY AWS_ACCOUNT SSH_AUTH_SOCK)
21
+ end
22
+
23
+ def warn_hoptoad(message, request, session, options={})
24
+ return if request.nil?
25
+ params = request.params
26
+
27
+ data = {
28
+ :api_key => HoptoadNotifier.api_key,
29
+ :error_class => options[:error_class] || message,
30
+ :error_message => message,
31
+ :backtrace => caller,
32
+ :environment => ENV.to_hash
33
+ }
34
+
35
+ data[:request] = {
36
+ :params => params
37
+ }
38
+
39
+ data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env))
40
+ data[:environment][:RAILS_ENV] = Merb.env
41
+
42
+ data[:session] = {
43
+ :key => session.instance_variable_get("@session_id"),
44
+ :data => session.to_hash
45
+ }
46
+
47
+ send_to_hoptoad :notice => default_notice_options.merge(data)
48
+ true
49
+ end
50
+
51
+ def notify_hoptoad(request, session)
52
+ return if request.nil?
53
+ params = request.params
54
+
55
+ request.exceptions.each do |exception|
56
+ data = {
57
+ :api_key => HoptoadNotifier.api_key,
58
+ :error_class => Extlib::Inflection.camelize(exception.class.name),
59
+ :error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}",
60
+ :backtrace => exception.backtrace,
61
+ :environment => ENV.to_hash
62
+ }
63
+
64
+ data[:request] = {
65
+ :params => params
66
+ }
67
+
68
+ data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env))
69
+ data[:environment][:RAILS_ENV] = Merb.env
70
+
71
+ data[:session] = {
72
+ :key => session.instance_variable_get("@session_id"),
73
+ :data => session.to_hash
74
+ }
75
+
76
+ send_to_hoptoad :notice => default_notice_options.merge(data)
77
+ end
78
+ true
79
+ end
80
+
81
+ def send_to_hoptoad(data) #:nodoc:
82
+ url = URI.parse("http://hoptoadapp.com:80/notices/")
83
+
84
+ Net::HTTP.start(url.host, url.port) do |http|
85
+ headers = {
86
+ 'Content-type' => 'application/x-yaml',
87
+ 'Accept' => 'text/xml, application/xml'
88
+ }
89
+ http.read_timeout = 5 # seconds
90
+ http.open_timeout = 2 # seconds
91
+ # http.use_ssl = HoptoadNotifier.secure
92
+ response = begin
93
+ http.post(url.path, stringify_keys(data).to_yaml, headers)
94
+ rescue TimeoutError => e
95
+ logger.error "Timeout while contacting the Hoptoad server."
96
+ nil
97
+ end
98
+ case response
99
+ when Net::HTTPSuccess then
100
+ logger.info "Hoptoad Success: #{response.class}"
101
+ else
102
+ logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
103
+ end
104
+ end
105
+ end
106
+
107
+ def default_notice_options #:nodoc:
108
+ {
109
+ :api_key => HoptoadNotifier.api_key,
110
+ :error_message => 'Notification',
111
+ :backtrace => nil,
112
+ :request => {},
113
+ :session => {},
114
+ :environment => {}
115
+ }
116
+ end
117
+
118
+ def stringify_keys(hash) #:nodoc:
119
+ hash.inject({}) do |h, pair|
120
+ h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
121
+ h
122
+ end
123
+ end
124
+ def clean_hoptoad_environment(env) #:nodoc:
125
+ env.each do |k, v|
126
+ env[k] = "[FILTERED]" if HoptoadNotifier.environment_filters.any? do |filter|
127
+ k.to_s.match(/#{filter}/)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,2 @@
1
+ namespace :merb_hoptoad_notifier do
2
+ end
@@ -0,0 +1,9 @@
1
+ ---
2
+ :development: &defaults
3
+ :api_key: ZOMGLOLROFLMAO
4
+
5
+ :test:
6
+ <<: *defaults
7
+
8
+ :production:
9
+ :api_key: UBERSECRETSHIT
@@ -0,0 +1,113 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "HoptoadNotifier" do
4
+ include Merb::Spec::Helpers
5
+
6
+ before(:each) do
7
+ stub(Merb).env { :production }
8
+ stub(Merb).root { Dir.tmpdir }
9
+
10
+ @http = Net::HTTP.new('hoptoadapp.com')
11
+ @headers = { "Content-type" => "application/x-yaml", "Accept" => "text/xml, application/xml" }
12
+ @config = {:development => {:api_key=>"ZOMGLOLROFLMAO"}, :production => {:api_key=>"UBERSECRETSHIT"}, :test => {:api_key=>"ZOMGLOLROFLMAO"}}
13
+ end
14
+
15
+ it "should define a constant" do
16
+ HoptoadNotifier.should_not be_nil
17
+ end
18
+
19
+ describe ".configure" do
20
+ before(:each) do
21
+ mock(YAML).load_file(File.join(Merb.root / 'config' / 'hoptoad.yml')) { @config }
22
+ HoptoadNotifier.configure
23
+ end
24
+ it "should know the api key after configuring" do
25
+ HoptoadNotifier.api_key.should == 'UBERSECRETSHIT'
26
+ end
27
+ end
28
+
29
+ describe ".stringify_key" do
30
+ it "should turn string keys into symbols" do
31
+ HoptoadNotifier.stringify_keys({'foo' => 'bar', :baz => 'foo', :bar => 'foo'}).should == { 'foo' => 'bar', 'baz' => 'foo', 'bar' => 'foo'}
32
+ end
33
+ end
34
+
35
+
36
+ describe "notification" do
37
+ before(:each) do
38
+ stub(Net::HTTP).new('hoptoadapp.com', 80, nil, nil, nil, nil) { @http }
39
+ mock(YAML).load_file(File.join(Merb.root / 'config' / 'hoptoad.yml')) { @config }
40
+ HoptoadNotifier.configure
41
+ end
42
+
43
+ describe ".default_notice_options" do
44
+ it "should return sane defaults" do
45
+ HoptoadNotifier.default_notice_options.should == {
46
+ :api_key => HoptoadNotifier.api_key,
47
+ :error_message => 'Notification',
48
+ :backtrace => nil,
49
+ :request => {},
50
+ :session => {},
51
+ :environment => {}
52
+ }
53
+ end
54
+ end
55
+
56
+ describe ".notify_hoptoad" do
57
+ describe "bad input" do
58
+ it "should handle nil input" do
59
+ HoptoadNotifier.notify_hoptoad(nil, nil).should be_nil
60
+ end
61
+ end
62
+
63
+ describe "good input" do
64
+ before(:each) do
65
+ mock(HoptoadNotifier).send_to_hoptoad(anything).times(2) { true }
66
+ @request = setup_merb_request
67
+ mock(@request).exceptions { [RuntimeError.new('ZOMG'), RuntimeError.new('ORLY')]}
68
+ mock(@request).params { {"q"=>"0017000000SmnJ0"} }
69
+ end
70
+ it "should return true" do
71
+ HoptoadNotifier.notify_hoptoad(@request, {}).should be_true
72
+ end
73
+ end
74
+ end
75
+
76
+ describe ".send_to_hoptoad" do
77
+ describe "any 2XX response" do
78
+ before(:each) do
79
+ response = Net::HTTPOK.new('1.1', 200, 'Wazzup?')
80
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { response }
81
+ end
82
+ it "should log success" do
83
+ mock(HoptoadNotifier.logger).info "Hoptoad Success: Net::HTTPOK"
84
+ HoptoadNotifier.send_to_hoptoad({})
85
+ end
86
+ end
87
+ describe "any non 2XX response" do
88
+ before(:each) do
89
+ response = Net::HTTPInternalServerError.new('1.1', 500, 'Upstream unavailable')
90
+ mock(response).body { 'Upstream unavailable' }
91
+
92
+ stub(@http.instance_variable_get('@socket')).closed? { true }
93
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { response }
94
+ end
95
+ it "should log failure" do
96
+ mock(HoptoadNotifier.logger).error "Hoptoad Failure: Net::HTTPInternalServerError\nUpstream unavailable"
97
+ HoptoadNotifier.send_to_hoptoad({})
98
+ end
99
+ end
100
+ describe "a timeout exception is thrown" do
101
+ before(:each) do
102
+ response = TimeoutError.new("It took too fucking long")
103
+ mock(@http).post("/notices/", "--- {}\n\n", @headers) { raise response }
104
+ end
105
+ it "should log failure" do
106
+ mock(HoptoadNotifier.logger).error("Hoptoad Failure: NilClass\n")
107
+ mock(HoptoadNotifier.logger).error("Timeout while contacting the Hoptoad server.")
108
+ HoptoadNotifier.send_to_hoptoad({})
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
data/spec/spec.opts ADDED
File without changes
@@ -0,0 +1,33 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+ require 'rr'
4
+ require 'merb-core'
5
+ require 'merb_hoptoad_notifier'
6
+ require 'tmpdir'
7
+ require 'pp'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :rr
11
+ end
12
+
13
+ module Merb
14
+ module Spec
15
+ module Helpers
16
+ def setup_merb_request
17
+ rack_env = {"SERVER_NAME"=>"localhost",
18
+ "rack.run_once"=>false, "rack.url_scheme"=>"http", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/526.1+ (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1",
19
+ "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "PATH_INFO"=>"/search/", "HTTP_CACHE_CONTROL"=>"max-age=0",
20
+ "HTTP_ACCEPT_LANGUAGE"=>"en-us", "HTTP_HOST"=>"localhost:4000", "SERVER_PROTOCOL"=>"HTTP/1.1", "SCRIPT_NAME"=>"",
21
+ "REQUEST_PATH"=>"/search/", "SERVER_SOFTWARE"=>"Mongrel 1.1.5", "REMOTE_ADDR"=>"127.0.0.1", "rack.streaming"=>true,
22
+ "rack.version"=>[0, 1], "rack.multithread"=>true, "HTTP_VERSION"=>"HTTP/1.1", "rack.multiprocess"=>false,
23
+ "REQUEST_URI"=>"/search/?q=0017000000SmnJ0", "SERVER_PORT"=>"4000", "QUERY_STRING"=>"q=0017000000SmnJ0",
24
+ "GATEWAY_INTERFACE"=>"CGI/1.2", "HTTP_ACCEPT"=>"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
25
+ "REQUEST_METHOD"=>"GET", "HTTP_CONNECTION"=>"keep-alive"}
26
+
27
+ request = Merb::Request.new(rack_env)
28
+ stub(request).env { rack_env }
29
+ request
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aanand-merb_hoptoad_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2.5
5
+ platform: ruby
6
+ authors:
7
+ - Corey Donohoe
8
+ - Phil Darnowsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-02-16 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Merb plugin that provides hoptoad exception notification
18
+ email: phil@darnowsky.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README
25
+ - LICENSE
26
+ - TODO
27
+ files:
28
+ - LICENSE
29
+ - README
30
+ - Rakefile
31
+ - TODO
32
+ - lib/merb_hoptoad_notifier
33
+ - lib/merb_hoptoad_notifier/hoptoad_notifier.rb
34
+ - lib/merb_hoptoad_notifier/merbtasks.rb
35
+ - lib/merb_hoptoad_notifier/hoptoad_mixin.rb
36
+ - lib/merb_hoptoad_notifier.rb
37
+ - spec/fixtures
38
+ - spec/fixtures/hoptoad.yml
39
+ - spec/merb_hoptoad_notifier_spec.rb
40
+ - spec/spec.opts
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/phildarnowsky
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project: merb
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Merb plugin that provides hoptoad exception notification
68
+ test_files: []
69
+