Module: HoptoadNotifier

Defined in:
lib/hoptoad_notifier.rb

Overview

Plugin for applications to automatically post errors to the Hoptoad of their choice.

Defined Under Namespace

Modules: Catcher Classes: Sender

Constant Summary collapse

IGNORE_DEFAULT =
['ActiveRecord::RecordNotFound',
'ActionController::RoutingError',
'ActionController::InvalidAuthenticityToken',
'CGI::Session::CookieStore::TamperedWithCookie',
'ActionController::UnknownAction']
IGNORE_USER_AGENT_DEFAULT =
[]
VERSION =
"1.2.0"
LOG_PREFIX =
"** [Hoptoad] "

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def api_key
  @api_key
end

.application_pathObject

The file path of the application. If RAILS_ROOT is defined it will default to that, otherwise the default will be FileUtils.pwd.



65
66
67
# File 'lib/hoptoad_notifier.rb', line 65

def application_path
  @application_path
end

.current_environmentObject

Returns the value of attribute current_environment.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def current_environment
  @current_environment
end

.hostObject

The host to connect to.



59
60
61
# File 'lib/hoptoad_notifier.rb', line 59

def host
  @host
end

.http_open_timeoutObject

The HTTP open timeout (defaults to 2 seconds).



85
86
87
# File 'lib/hoptoad_notifier.rb', line 85

def http_open_timeout
  @http_open_timeout
end

.http_read_timeoutObject

The HTTP read timeout (defaults to 5 seconds).



90
91
92
# File 'lib/hoptoad_notifier.rb', line 90

def http_read_timeout
  @http_read_timeout
end

.outputObject

Returns the value of attribute output.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def output
  @output
end

.portObject

The port on which your Hoptoad server runs.



54
55
56
# File 'lib/hoptoad_notifier.rb', line 54

def port
  @port
end

.proxy_hostObject

Returns the value of attribute proxy_host.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def proxy_host
  @proxy_host
end

.proxy_passObject

Returns the value of attribute proxy_pass.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def proxy_pass
  @proxy_pass
end

.proxy_portObject

Returns the value of attribute proxy_port.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def proxy_port
  @proxy_port
end

.proxy_userObject

Returns the value of attribute proxy_user.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def proxy_user
  @proxy_user
end

.public_environmentsObject

An array of environments that are considered ‘public’ Default is:

‘production’, ‘staging’


71
72
73
# File 'lib/hoptoad_notifier.rb', line 71

def public_environments
  @public_environments
end

.secureObject

Returns the value of attribute secure.



25
26
27
# File 'lib/hoptoad_notifier.rb', line 25

def secure
  @secure
end

Class Method Details

.add_default_filtersObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/hoptoad_notifier.rb', line 210

def add_default_filters
  self.backtrace_filters.clear

  filter_backtrace do |line|
    line.gsub(/#{application_path}/, "[RAILS_ROOT]")
  end

  filter_backtrace do |line|
    line.gsub(/^\.\//, "")
  end

  filter_backtrace do |line|
    if defined?(Gem)
      Gem.path.inject(line) do |line, path|
        line.gsub(/#{path}/, "[GEM_ROOT]")
      end
    end
  end

  filter_backtrace do |line|
    line if line !~ /lib\/#{File.basename(__FILE__)}/
  end
end

.backtrace_filtersObject



29
30
31
# File 'lib/hoptoad_notifier.rb', line 29

def backtrace_filters
  @backtrace_filters ||= []
end

.configure {|_self| ... } ⇒ Object

Call this method to modify defaults in your initializers.

HoptoadNotifier.configure do |config|

config.api_key = '1234567890abcdef'
config.secure  = false

end

NOTE: secure connections are not yet supported.

Yields:

  • (_self)

Yield Parameters:



168
169
170
171
172
173
174
175
# File 'lib/hoptoad_notifier.rb', line 168

def configure
  add_default_filters
  yield self
  if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
    ActionController::Base.send(:include, HoptoadNotifier::Catcher)
  end
  report_ready
end

.default_notice_optionsObject

:nodoc:



185
186
187
188
189
190
191
192
193
194
# File 'lib/hoptoad_notifier.rb', line 185

def default_notice_options #:nodoc:
  {
    :api_key       => HoptoadNotifier.api_key,
    :error_message => 'Notification',
    :backtrace     => caller,
    :request       => {},
    :session       => {},
    :environment   => ENV.to_hash
  }
end

.environment_filtersObject



126
127
128
# File 'lib/hoptoad_notifier.rb', line 126

def environment_filters
  @environment_filters ||= %w()
end

.environment_infoObject



142
143
144
145
# File 'lib/hoptoad_notifier.rb', line 142

def environment_info
  info = "[Ruby: #{RUBY_VERSION}]"
  info << " [Rails: #{::Rails::VERSION::STRING}] [RailsEnv: #{RAILS_ENV}]" if defined?(Rails)
end

.filter_backtrace(&block) ⇒ Object

Takes a block and adds it to the list of backtrace filters. When the filters run, the block will be handed each line of the backtrace and can modify it as necessary. For example, by default a path matching the RAILS_ROOT constant will be transformed into “[RAILS_ROOT]”



49
50
51
# File 'lib/hoptoad_notifier.rb', line 49

def filter_backtrace &block
  self.backtrace_filters << block
end

.ignoreObject

Returns the list of errors that are being ignored. The array can be appended to.



95
96
97
98
99
# File 'lib/hoptoad_notifier.rb', line 95

def ignore
  @ignore ||= (HoptoadNotifier::IGNORE_DEFAULT.dup)
  @ignore.flatten!
  @ignore
end

.ignore_by_filter(&block) ⇒ Object

Takes a block and adds it to the list of ignore filters. When the filters run, the block will be handed the exception. If the block yields a value equivalent to “true,” the exception will be ignored, otherwise it will be processed by hoptoad.



41
42
43
# File 'lib/hoptoad_notifier.rb', line 41

def ignore_by_filter &block
  self.ignore_by_filters << block
end

.ignore_by_filtersObject



33
34
35
# File 'lib/hoptoad_notifier.rb', line 33

def ignore_by_filters
  @ignore_by_filters ||= []
end

.ignore_only=(names) ⇒ Object

Sets the list of ignored errors to only what is passed in here. This method can be passed a single error or a list of errors.



103
104
105
# File 'lib/hoptoad_notifier.rb', line 103

def ignore_only=(names)
  @ignore = [names].flatten
end

.ignore_user_agentObject

Returns the list of user agents that are being ignored. The array can be appended to.



108
109
110
111
112
# File 'lib/hoptoad_notifier.rb', line 108

def ignore_user_agent
  @ignore_user_agent ||= (HoptoadNotifier::IGNORE_USER_AGENT_DEFAULT.dup)
  @ignore_user_agent.flatten!
  @ignore_user_agent
end

.ignore_user_agent_only=(names) ⇒ Object

Sets the list of ignored user agents to only what is passed in here. This method can be passed a single user agent or a list of user agents.



116
117
118
# File 'lib/hoptoad_notifier.rb', line 116

def ignore_user_agent_only=(names)
  @ignore_user_agent = [names].flatten
end

.loggerObject

Checking for the logger in hopes we can get rid of the ugly syntax someday



152
153
154
155
156
157
158
# File 'lib/hoptoad_notifier.rb', line 152

def logger
  if defined?(Rails.logger)
    Rails.logger
  elsif defined?(RAILS_DEFAULT_LOGGER)
    RAILS_DEFAULT_LOGGER
  end
end

.notify(notice = {}) ⇒ Object

You can send an exception manually using this method, even when you are not in a controller. You can pass an exception or a hash that contains the attributes that would be sent to Hoptoad:

  • api_key: The API key for this project. The API key is a unique identifier that Hoptoad uses for identification.

  • error_message: The error returned by the exception (or the message you want to log).

  • backtrace: A backtrace, usually obtained with caller.

  • request: The controller’s request object.

  • session: The contents of the user’s session.

  • environment: ENV merged with the contents of the request’s environment.



206
207
208
# File 'lib/hoptoad_notifier.rb', line 206

def notify notice = {}
  Sender.new.notify_hoptoad( notice )
end

.params_filtersObject

Returns a list of parameters that should be filtered out of what is sent to Hoptoad. By default, all “password” attributes will have their contents replaced.



122
123
124
# File 'lib/hoptoad_notifier.rb', line 122

def params_filters
  @params_filters ||= %w(password)
end

.protocolObject

:nodoc:



177
178
179
# File 'lib/hoptoad_notifier.rb', line 177

def protocol #:nodoc:
  secure ? "https" : "http"
end

.report_environment_infoObject



134
135
136
# File 'lib/hoptoad_notifier.rb', line 134

def report_environment_info
  write_verbose_log("Environment Info: #{environment_info}")
end

.report_readyObject



130
131
132
# File 'lib/hoptoad_notifier.rb', line 130

def report_ready
  write_verbose_log("Notifier #{VERSION} ready to catch errors")
end

.report_response_body(response) ⇒ Object



138
139
140
# File 'lib/hoptoad_notifier.rb', line 138

def report_response_body(response)
  write_verbose_log("Response from Hoptoad: \n#{response}")
end

.urlObject

:nodoc:



181
182
183
# File 'lib/hoptoad_notifier.rb', line 181

def url #:nodoc:
  URI.parse("#{protocol}://#{host}:#{port}/notices/")
end

.write_verbose_log(message) ⇒ Object



147
148
149
# File 'lib/hoptoad_notifier.rb', line 147

def write_verbose_log(message)
  logger.info LOG_PREFIX + message if logger
end