forked from cv/merb_hoptoad_notifier
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release 1.0.10 to support the v2 api of hoptoadapp.com
- Loading branch information
Showing
10 changed files
with
81 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ log/* | |
coverage | ||
pkg | ||
*.kpf | ||
bin | ||
vendor | ||
*.gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
bundle_path "vendor" | ||
disable_system_gems | ||
|
||
merb_gem_version = '>=1.0.9' | ||
|
||
only :release do | ||
gem "merb-core", merb_gem_version | ||
gem 'toadhopper', '~>0.9.1' | ||
end | ||
|
||
only :test do | ||
gem 'rake' | ||
gem 'rcov' | ||
gem 'rr' | ||
gem 'ruby-debug' | ||
gem 'bundler' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,19 +3,13 @@ require 'rake/gempackagetask' | |
require 'rubygems/specification' | ||
require 'date' | ||
require 'merb-core/version' | ||
require 'merb-core/tasks/merb_rake_helper' | ||
require 'spec/rake/spectask' | ||
require 'bundler' | ||
|
||
install_home = ENV['GEM_HOME'] ? "-i #{ENV['GEM_HOME']}" : "" | ||
|
||
def sudo | ||
windows = (PLATFORM =~ /win32|cygwin/) rescue nil | ||
ENV['MERB_SUDO'] ||= "sudo" | ||
sudo = windows ? "" : ENV['MERB_SUDO'] | ||
end | ||
|
||
NAME = "merb_hoptoad_notifier" | ||
GEM_VERSION = "1.0.9.1" | ||
GEM_VERSION = "1.0.10" | ||
AUTHOR = "Corey Donohoe" | ||
EMAIL = '[email protected]' | ||
HOMEPAGE = "http://github.com/atmos/merb_hoptoad_notifier" | ||
|
@@ -33,7 +27,13 @@ spec = Gem::Specification.new do |s| | |
s.author = AUTHOR | ||
s.email = EMAIL | ||
s.homepage = HOMEPAGE | ||
s.add_dependency('merb-core', '>= 1.0.9') | ||
|
||
manifest = Bundler::Environment.load(File.dirname(__FILE__) + '/Gemfile') | ||
manifest.dependencies.each do |d| | ||
next unless d.only && d.only.include?('release') | ||
s.add_dependency(d.name, d.version) | ||
end | ||
|
||
s.require_path = 'lib' | ||
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*") | ||
end | ||
|
@@ -42,32 +42,20 @@ Rake::GemPackageTask.new(spec) do |pkg| | |
pkg.gem_spec = spec | ||
end | ||
|
||
desc "install the plugin locally" | ||
task :install => [:package] do | ||
sh %{#{sudo} gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION} --no-update-sources} | ||
end | ||
|
||
desc "create a gemspec file" | ||
task :make_spec do | ||
File.open("#{NAME}.gemspec", "w") do |file| | ||
file.puts spec.to_ruby | ||
end | ||
end | ||
|
||
namespace :jruby do | ||
desc "Run :package and install the resulting .gem with jruby" | ||
task :install => :package do | ||
sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri} | ||
end | ||
end | ||
|
||
Spec::Rake::SpecTask.new(:default) do |t| | ||
t.spec_opts << %w(-fs --color) << %w(-O spec/spec.opts) | ||
t.spec_opts << '--loadby' << 'random' | ||
t.spec_files = Dir["spec/*_spec.rb"] | ||
t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true | ||
t.rcov_opts << '--exclude' << '.gem/' | ||
|
||
t.rcov_opts << '--text-summary' | ||
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse' | ||
t.rcov_opts << '--only-uncovered' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
require File.expand_path(File.dirname(__FILE__)+'/merb_hoptoad_notifier/hoptoad_notifier') | ||
require File.expand_path(File.dirname(__FILE__)+'/merb_hoptoad_notifier/hoptoad_mixin') | ||
|
||
# make sure we're running inside Merb | ||
if defined?(Merb::Plugins) | ||
libdir = File.join(File.dirname(__FILE__), 'merb_hoptoad_notifier') | ||
require 'toadhopper' | ||
require File.join(libdir, 'hoptoad_notifier') | ||
|
||
Merb::BootLoader.after_app_loads do | ||
HoptoadNotifier.configure | ||
end | ||
Merb::Plugins.add_rakefiles "merb_hoptoad_notifier/merbtasks" | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,40 @@ | ||
require 'net/http' | ||
|
||
module HoptoadNotifier | ||
class << self | ||
attr_accessor :api_key, :logger | ||
end | ||
|
||
def configure | ||
key = YAML.load_file(Merb.root / 'config' / 'hoptoad.yml') | ||
if key | ||
env = key[Merb.env.to_sym] | ||
env ? @api_key = env[:api_key] : raise(ArgumentError, "No hoptoad key for Merb environment #{Merb.env}") | ||
end | ||
end | ||
|
||
def logger | ||
@logger || Merb.logger | ||
end | ||
|
||
def environment_filters | ||
@environment_filters ||= %w(AWS_ACCESS_KEY AWS_SECRET_ACCESS_KEY AWS_ACCOUNT SSH_AUTH_SOCK) | ||
end | ||
|
||
def warn_hoptoad(message, request, session, options={}) | ||
return if request.nil? | ||
params = request.params | ||
|
||
data = { | ||
:api_key => HoptoadNotifier.api_key, | ||
:error_class => options[:error_class] || message, | ||
:error_message => message, | ||
:backtrace => caller, | ||
:environment => ENV.to_hash | ||
} | ||
|
||
data[:request] = { | ||
:params => params | ||
} | ||
|
||
data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env)) | ||
data[:environment][:RAILS_ENV] = Merb.env | ||
|
||
data[:session] = { | ||
:key => session.instance_variable_get("@session_id"), | ||
:data => session.to_hash | ||
} | ||
|
||
send_to_hoptoad :notice => default_notice_options.merge(data) | ||
true | ||
end | ||
|
||
def notify_hoptoad(request, session) | ||
return if request.nil? | ||
params = request.params | ||
|
||
request.exceptions.each do |exception| | ||
data = { | ||
:api_key => HoptoadNotifier.api_key, | ||
:error_class => Extlib::Inflection.camelize(exception.class.name), | ||
:error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}", | ||
:backtrace => exception.backtrace, | ||
:environment => ENV.to_hash | ||
} | ||
|
||
data[:request] = { | ||
:params => params | ||
} | ||
|
||
data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env)) | ||
data[:environment][:RAILS_ENV] = Merb.env | ||
|
||
data[:session] = { | ||
:key => session.instance_variable_get("@session_id"), | ||
:data => session.to_hash | ||
} | ||
def self.configure | ||
key = YAML.load_file(Merb.root / 'config' / 'hoptoad.yml') | ||
|
||
send_to_hoptoad :notice => default_notice_options.merge(data) | ||
end | ||
true | ||
end | ||
|
||
def notify_hoptoad_exception(exception) | ||
data = { | ||
:api_key => HoptoadNotifier.api_key, | ||
:error_class => Extlib::Inflection.camelize(exception.class.name), | ||
:error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}", | ||
:backtrace => exception.backtrace, | ||
:environment => ENV.to_hash | ||
} | ||
|
||
data[:environment][:RAILS_ENV] = Merb.env | ||
|
||
send_to_hoptoad :notice => default_notice_options.merge(data) | ||
true | ||
if key | ||
env = key[Merb.env.to_sym] | ||
env ? @api_key = env[:api_key] : raise(ArgumentError, "No hoptoad key for Merb environment #{Merb.env}") | ||
end | ||
end | ||
|
||
def send_to_hoptoad(data) #:nodoc: | ||
url = URI.parse("http://hoptoadapp.com:80/notices/") | ||
def self.logger | ||
@logger || Merb.logger | ||
end | ||
|
||
Net::HTTP.start(url.host, url.port) do |http| | ||
headers = { | ||
'Content-type' => 'application/x-yaml', | ||
'Accept' => 'text/xml, application/xml' | ||
} | ||
http.read_timeout = 5 # seconds | ||
http.open_timeout = 2 # seconds | ||
# http.use_ssl = HoptoadNotifier.secure | ||
response = begin | ||
http.post(url.path, clean_non_serializable_data(data).to_yaml, headers) | ||
rescue TimeoutError => e | ||
logger.error "Timeout while contacting the Hoptoad server." | ||
nil | ||
end | ||
case response | ||
when Net::HTTPSuccess then | ||
logger.info "Hoptoad Success: #{response.class}" | ||
else | ||
logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}" | ||
end | ||
end | ||
end | ||
|
||
def default_notice_options #:nodoc: | ||
{ | ||
:api_key => HoptoadNotifier.api_key, | ||
:error_message => 'Notification', | ||
:backtrace => nil, | ||
:request => {}, | ||
:session => {}, | ||
:environment => {} | ||
def self.notify_hoptoad(request, session) | ||
request.exceptions.each do |exception| | ||
options = { | ||
:api_key => HoptoadNotifier.api_key, | ||
:url => "#{request.protocol}://#{request.host}#{request.path}", | ||
:component => request.params['controller'], | ||
:action => request.params['action'], | ||
:request => request, | ||
:framework_env => Merb.env, | ||
:notifier_name => 'Merb::HoptoadNotifier', | ||
:notifier_version => '1.0.10', | ||
:session => session.to_hash | ||
} | ||
end | ||
|
||
def clean_non_serializable_data(notice) #:nodoc: | ||
notice.select{|k,v| serializable?(v) }.inject({}) do |h, pair| | ||
h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last | ||
h | ||
end | ||
end | ||
|
||
def serializable?(value) #:nodoc: | ||
value.is_a?(Fixnum) || | ||
value.is_a?(Array) || | ||
value.is_a?(String) || | ||
value.is_a?(Hash) || | ||
value.is_a?(Bignum) | ||
dispatcher.post!(exception, options, {'X-Hoptoad-Client-Name' => 'Merb::HoptoadNotifier'}) | ||
end | ||
true | ||
end | ||
|
||
def stringify_keys(hash) #:nodoc: | ||
hash.inject({}) do |h, pair| | ||
h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last | ||
h | ||
end | ||
end | ||
def clean_hoptoad_environment(env) #:nodoc: | ||
env.each do |k, v| | ||
env[k] = "[FILTERED]" if HoptoadNotifier.environment_filters.any? do |filter| | ||
k.to_s.match(/#{filter}/) | ||
end | ||
end | ||
end | ||
def self.dispatcher | ||
@dispatcher ||= ToadHopper.new(api_key) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.