Permalink
Browse files

Added hoptoad_notifier

git-svn-id: https://svn.thoughtbot.com/plugins/hoptoad_notifier/trunk@272 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
  • Loading branch information...
0 parents commit 34a31064fb9b90c2f97125ac2b2fb6931c26eb77 jyurek committed Dec 18, 2007
Showing with 139 additions and 0 deletions.
  1. +16 −0 README
  2. +22 −0 Rakefile
  3. +92 −0 lib/hoptoad_notifier.rb
  4. +4 −0 tasks/hoptoad_notifier_tasks.rake
  5. +5 −0 test/hoptoad_notifier_test.rb
16 README
@@ -0,0 +1,16 @@
+HoptoadNotifier
+===============
+
+This is the notifier plugin for integrating apps with Hoptoad. When an
+uncaught exception occurs, HoptoadNotifier will POST the relevant data
+to the Hoptoad server specified in your environment.rb
+
+ HoptoadNotifier.url = "http://192.168.1.33:3000/notices/"
+ HoptoadNotifier.app_name = "my_sweet_app"
+
+Then make sure you have
+
+ include HoptoadNotifier::Catcher
+
+in your ApplicationController, and all exceptions will be logged to Hoptoad
+where they can be aggregated, filtered, and searched.
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the hoptoad_notifier plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the hoptoad_notifier plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HoptoadNotifier'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
92 lib/hoptoad_notifier.rb
@@ -0,0 +1,92 @@
+# Plugin for applications to automatically post errors to Hoptoad.
+module HoptoadNotifier
+
+ def self.url; @url; end
+ def self.url= url; @url = URI.parse(url); end
+
+ def self.app_name; @app_name; end
+ def self.app_name= app_name; @app_name = app_name; end
+
+ module Catcher
+ def self.included target
+ target.send( :include, Handlers )
+ end
+
+ module Handlers
+
+ private
+
+ def rescue_action_in_public exception
+ if is_a_404?(exception)
+ render_not_found_page
+ else
+ render_error_page
+ data = {
+ 'project_name' => HoptoadNotifier.app_name,
+ 'error_message' => exception.message,
+ 'backtrace' => exception.backtrace.to_json,
+ 'request' => {
+ 'params' => request.parameters.to_hash,
+ 'rails_root' => File.expand_path(RAILS_ROOT),
+ 'url' => "#{request.protocol}#{request.host}#{request.request_uri}"
+ }.to_json,
+ 'session' => {
+ 'key' => session.instance_variable_get("@session_id"),
+ 'data' => session.instance_variable_get("@data")
+ }.to_json,
+ 'environment' => ENV.to_hash.to_json
+ }
+ inform_hoptoad(data)
+ end
+ end
+
+ def render_not_found_page
+ respond_to do |wants|
+ wants.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => :not_found }
+ wants.all { render :nothing => true, :status => :not_found }
+ end
+ end
+
+ def render_error_page
+ respond_to do |wants|
+ wants.html { render :file => "#{RAILS_ROOT}/public/500.html", :status => :internal_server_error }
+ wants.all { render :nothing => true, :status => :internal_server_error }
+ end
+ end
+
+ def inform_hoptoad data
+ url = HoptoadNotifier.url
+ Net::HTTP.start(url.host, url.port) do |http|
+ response = http.post(url.path, to_params(data), {'Accept', 'text/xml, application/xml'})
+ case response
+ when Net::HTTPSuccess then
+ logger.info "Hoptoad Success: #{response.class}"
+ when Net::HTTPRedirect then
+ logger.info "Hoptoad Success: #{response.class}"
+ else
+ logger.error "Hoptoad Failure: #{response.class}"
+ end
+ end
+ end
+
+ def is_a_404? exception
+ [
+ ActiveRecord::RecordNotFound,
+ ActionController::UnknownController,
+ ActionController::UnknownAction
+ ].include?( exception )
+ end
+
+ def to_params thing, context = "notice"
+ case thing
+ when Hash
+ thing.map{|key, val| to_params(val, "#{context}[#{key}]") }.join("&")
+ when Array
+ thing.map{|val| to_params(val, "#{context}[]") }.join("&")
+ else
+ "#{CGI.escape(context)}=#{CGI.escape(thing.to_s)}"
+ end
+ end
+ end
+ end
+end
4 tasks/hoptoad_notifier_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :hoptoad_notifier do
+# # Task goes here
+# end
5 test/hoptoad_notifier_test.rb
@@ -0,0 +1,5 @@
+require 'test/unit'
+
+class HoptoadNotifierTest < Test::Unit::TestCase
+
+end

0 comments on commit 34a3106

Please sign in to comment.