Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added version info and moved Rails-specific stuff into init.rb

  • Loading branch information...
commit 5f76d2dd3798338a4dc101a3e77db033248502e1 1 parent 6e564a9
Joe Ferris authored
View
11 lib/hoptoad_notifier.rb
@@ -47,7 +47,8 @@ def report_response_body(response)
def environment_info
info = "[Ruby: #{RUBY_VERSION}]"
- info << " [Rails: #{::Rails::VERSION::STRING}] [RailsEnv: #{RAILS_ENV}]" if defined?(Rails)
+ info << " [Rails: #{::Rails::VERSION::STRING}]" if defined?(Rails)
+ info << " [Env: #{configuration.environment_name}]"
end
def write_verbose_log(message)
@@ -70,12 +71,8 @@ def logger
# config.secure = false
# end
def configure
- new_configuration = Configuration.new
- yield new_configuration
- if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
- ActionController::Base.send(:include, HoptoadNotifier::Catcher)
- end
- self.configuration = new_configuration
+ self.configuration ||= Configuration.new
+ yield(configuration)
self.sender = Sender.new(configuration)
report_ready
end
View
21 lib/hoptoad_notifier/configuration.rb
@@ -60,9 +60,21 @@ class Configuration
# A list of environments in which notifications should not be sent.
attr_accessor :development_environments
+ # The name of the environment the application is running in
+ attr_accessor :environment_name
+
# The path to the project in which the error occurred, such as the RAILS_ROOT
attr_accessor :project_root
+ # The name of the notifier library being used to send notifications (such as "Hoptoad Notifier")
+ attr_accessor :notifier_name
+
+ # The version of the notifier library being used to send notifications (such as "1.0.2")
+ attr_accessor :notifier_version
+
+ # The url of the notifier library being used to send notifications
+ attr_accessor :notifier_url
+
DEFAULT_PARAMS_FILTERS = %w(password password_confirmation).freeze
DEFAULT_BACKTRACE_FILTERS = [
@@ -108,6 +120,9 @@ def initialize
@ignore = IGNORE_DEFAULT.dup
@ignore_user_agent = []
@development_environments = %w(development test)
+ @notifier_name = 'Hoptoad Notifier'
+ @notifier_version = VERSION
+ @notifier_url = 'http://hoptoadapp.com'
end
# Takes a block and adds it to the list of backtrace filters. When the filters
@@ -175,11 +190,5 @@ def protocol #:nodoc:
def public?
!development_environments.include?(environment_name)
end
-
- private
-
- def environment_name
- RAILS_ENV if defined?(RAILS_ENV)
- end
end
end
View
7 rails/init.rb
@@ -0,0 +1,7 @@
+if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
+ ActionController::Base.send(:include, HoptoadNotifier::Catcher)
+end
+
+HoptoadNotifier.configure do |config|
+ config.environment_name = RAILS_ENV
+end
View
19 test/configuration_test.rb
@@ -10,6 +10,10 @@ class ConfigurationTest < Test::Unit::TestCase
assert_config_default :proxy_user, nil
assert_config_default :proxy_pass, nil
assert_config_default :project_root, nil
+ assert_config_default :environment_name, nil
+ assert_config_default :notifier_version, HoptoadNotifier::VERSION
+ assert_config_default :notifier_name, 'Hoptoad Notifier'
+ assert_config_default :notifier_url, 'http://hoptoadapp.com'
assert_config_default :secure, false
assert_config_default :host, 'hoptoadapp.com'
assert_config_default :http_open_timeout, 2
@@ -50,6 +54,10 @@ class ConfigurationTest < Test::Unit::TestCase
assert_config_overridable :http_open_timeout
assert_config_overridable :http_read_timeout
assert_config_overridable :project_root
+ assert_config_overridable :notifier_version
+ assert_config_overridable :notifier_name
+ assert_config_overridable :notifier_url
+ assert_config_overridable :environment_name
end
should "have an api key" do
@@ -122,22 +130,21 @@ class ConfigurationTest < Test::Unit::TestCase
assert_same_elements %w(development test), config.development_environments
end
- should "be public in a public RAILS_ENV" do
+ should "be public in a public environment" do
config = HoptoadNotifier::Configuration.new
config.development_environments = %w(development)
- define_constant('RAILS_ENV', 'production')
+ config.environment_name = 'production'
assert config.public?
end
- should "not be public in a development RAILS_ENV" do
+ should "not be public in a development environment" do
config = HoptoadNotifier::Configuration.new
config.development_environments = %w(staging)
- define_constant('RAILS_ENV', 'staging')
+ config.environment_name = 'staging'
assert !config.public?
end
- should "be public when RAILS_ENV is not defined" do
- assert !defined?(RAILS_ENV)
+ should "be public without an environment name" do
config = HoptoadNotifier::Configuration.new
assert config.public?
end
View
157 test/controller_test.rb
@@ -1,157 +0,0 @@
-require File.dirname(__FILE__) + '/helper'
-
-def should_notify_normally
- should "send even ignored exceptions if told manually" do
- @controller.expects(:rescue_action_in_public_without_hoptoad).never
- assert_nothing_raised do
- request("manual_notify_ignored")
- end
- assert_caught_and_sent
- end
-
- should "ignore default exceptions" do
- @controller.expects(:rescue_action_in_public_without_hoptoad)
- assert_nothing_raised do
- request("do_raise_ignored")
- end
- assert_caught_and_not_sent
- end
-end
-
-def should_auto_include_catcher
- should "auto-include for ApplicationController" do
- assert ApplicationController.include?(HoptoadNotifier::Catcher)
- end
-end
-
-class ControllerTest < Test::Unit::TestCase
- def stub_public_request!
- @controller.class.consider_all_requests_local = false
- @controller.stubs(:local_request? => false)
- end
-
- context "when auto-included" do
- setup do
- HoptoadNotifier.configure do |config|
- config.api_key = "1234567890abcdef"
- end
-
- class ::ApplicationController < ActionController::Base
- end
-
- class ::AutoIncludeController < ::ApplicationController
- include TestMethods
- def rescue_action e
- rescue_action_in_public e
- end
- end
-
- @controller = ::AutoIncludeController.new
- stub_public_request!
- HoptoadNotifier.sender = CollectingSender.new
- end
-
- context "when included through the configure block" do
- should_auto_include_catcher
- should_notify_normally
- end
-
- context "when included both through configure and normally" do
- setup do
- class ::AutoIncludeController < ::ApplicationController
- include HoptoadNotifier::Catcher
- end
- end
- should_auto_include_catcher
- should_notify_normally
- end
- end
-
- context "when the logger is overridden for an action" do
- setup do
- class ::IgnoreActionController < ::ActionController::Base
- include TestMethods
- include HoptoadNotifier::Catcher
- def rescue_action e
- rescue_action_in_public e
- end
- def logger
- super unless action_name == "do_raise"
- end
- end
- reset_config
- ::ActionController::Base.logger = Logger.new(STDOUT)
- @controller = ::IgnoreActionController.new
- @controller.stubs(:public_environment?).returns(true)
- @controller.stubs(:rescue_action_in_public_without_hoptoad)
- HoptoadNotifier.stubs(:environment_info)
-
- # stubbing out Net::HTTP as well
- @body = 'body'
- @http = stub(:post => @response, :read_timeout= => nil, :open_timeout= => nil, :use_ssl= => nil)
- Net::HTTP.stubs(:new).returns(@http)
- end
-
- should "work when action is called and request works" do
- @response = stub(:body => @body, :class => Net::HTTPSuccess)
- assert_nothing_raised do
- request("do_raise")
- end
- end
-
- should "work when action is called and request doesn't work" do
- @response = stub(:body => @body, :class => Net::HTTPError)
- assert_nothing_raised do
- request("do_raise")
- end
- end
-
- should "work when action is called and hoptoad times out" do
- @http.stubs(:post).raises(TimeoutError)
- assert_nothing_raised do
- request("do_raise")
- end
- end
- end
-
- context "The hoptoad test controller" do
- setup do
- @controller = ::HoptoadController.new
- class ::HoptoadController
- def rescue_action e
- raise e
- end
- end
- end
-
- context "with no notifier catcher" do
- should "not prevent raises" do
- assert_raises RuntimeError do
- request("do_raise")
- end
- end
-
- should "allow a non-raising action to complete" do
- assert_nothing_raised do
- request("do_not_raise")
- end
- end
- end
-
- context "with the notifier installed" do
- setup do
- class ::HoptoadController
- include HoptoadNotifier::Catcher
- def rescue_action e
- rescue_action_in_public e
- end
- end
- reset_config
- stub_public_request!
- HoptoadNotifier.sender = CollectingSender.new
- end
-
- should_notify_normally
- end
- end
-end
View
1  test/helper.rb
@@ -115,6 +115,7 @@ def create_dummy
end
def reset_config
+ HoptoadNotifier.configuration = nil
HoptoadNotifier.configure do |config|
config.api_key = 'abc123'
end
View
14 test/notifier_test.rb
@@ -16,11 +16,11 @@ def assert_sent(notice, notice_args)
end
def set_public_env
- define_constant('RAILS_ENV', 'production')
+ HoptoadNotifier.configure { |config| config.environment_name = 'production' }
end
def set_development_env
- define_constant('RAILS_ENV', 'development')
+ HoptoadNotifier.configure { |config| config.environment_name = 'development' }
end
# TODO: what does this test?
@@ -45,6 +45,16 @@ def set_development_env
assert_equal yielded_configuration, HoptoadNotifier.configuration
end
+ should "not remove existing config options when configuring twice" do
+ first_config = nil
+ HoptoadNotifier.configure do |config|
+ first_config = config
+ end
+ HoptoadNotifier.configure do |config|
+ assert_equal first_config, config
+ end
+ end
+
should_eventually "use standard rails logging filters on params and env" do
::HoptoadController.class_eval do
filter_parameter_logging :ghi
Please sign in to comment.
Something went wrong with that request. Please try again.