module HoptoadNotifier

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

Constants

API_VERSION
HEADERS
LOG_PREFIX
VERSION

Attributes

configuration[RW]

A Hoptoad configuration object. Must act like a hash and return sensible values for all Hoptoad configuration options. See HoptoadNotifier::Configuration.

sender[RW]

The sender object is responsible for delivering formatted data to the Hoptoad server. Must respond to send_to_hoptoad. See HoptoadNotifier::Sender.

Public Instance Methods

build_lookup_hash_for(exception, options = {}) click to toggle source
# File lib/hoptoad_notifier.rb, line 107
def build_lookup_hash_for(exception, options = {})
  notice = build_notice_for(exception, options)

  result = {}
  result[:action]           = notice.action      rescue nil
  result[:component]        = notice.component   rescue nil
  result[:error_class]      = notice.error_class if notice.error_class
  result[:environment_name] = 'production'

  unless notice.backtrace.lines.empty?
    result[:file]        = notice.backtrace.lines.first.file
    result[:line_number] = notice.backtrace.lines.first.number
  end

  result
end
configure(silent = false) { |configuration| ... } click to toggle source

Call this method to modify defaults in your initializers.

@example

HoptoadNotifier.configure do |config|
  config.api_key = '1234567890abcdef'
  config.secure  = false
end
# File lib/hoptoad_notifier.rb, line 78
def configure(silent = false)
  self.configuration ||= Configuration.new
  yield(configuration)
  self.sender = Sender.new(configuration)
  report_ready unless silent
end
environment_info() click to toggle source

Returns the Ruby version, Rails version, and current Rails environment

# File lib/hoptoad_notifier.rb, line 55
def environment_info
  info = "[Ruby: #{RUBY_VERSION}]"
  info << " [#{configuration.framework}]"
  info << " [Env: #{configuration.environment_name}]"
end
logger() click to toggle source

Look for the Rails logger currently defined

# File lib/hoptoad_notifier.rb, line 67
def logger
  self.configuration.logger
end
notify(exception, opts = {}) click to toggle source

Sends an exception manually using this method, even when you are not in a controller.

@param [Exception] exception The exception you want to notify Hoptoad about. @param [Hash] opts Data that will be sent to Hoptoad.

@option opts [String] :api_key The API key for this project. The API key is a unique identifier that Hoptoad uses for identification. @option opts [String] :error_message The error returned by the exception (or the message you want to log). @option opts [String] :backtrace A backtrace, usually obtained with caller. @option opts [String] :request The controller's request object. @option opts [String] :session The contents of the user's session. @option opts [String] :environment ENV merged with the contents of the request's environment.

# File lib/hoptoad_notifier.rb, line 96
def notify(exception, opts = {})
  send_notice(build_notice_for(exception, opts))
end
notify_or_ignore(exception, opts = {}) click to toggle source

Sends the notice unless it is one of the default ignored exceptions @see #notify

# File lib/hoptoad_notifier.rb, line 102
def notify_or_ignore(exception, opts = {})
  notice = build_notice_for(exception, opts)
  send_notice(notice) unless notice.ignore?
end
report_environment_info() click to toggle source

Prints out the environment info to the log for debugging help

# File lib/hoptoad_notifier.rb, line 45
def report_environment_info
  write_verbose_log("Environment Info: #{environment_info}")
end
report_ready() click to toggle source

Tell the log that the Notifier is good to go

# File lib/hoptoad_notifier.rb, line 40
def report_ready
  write_verbose_log("Notifier #{VERSION} ready to catch errors")
end
report_response_body(response) click to toggle source

Prints out the response body from Hoptoad for debugging help

# File lib/hoptoad_notifier.rb, line 50
def report_response_body(response)
  write_verbose_log("Response from Hoptoad: \n#{response}")
end
write_verbose_log(message) click to toggle source

Writes out the given message to the logger

# File lib/hoptoad_notifier.rb, line 62
def write_verbose_log(message)
  logger.info LOG_PREFIX + message if logger
end

Private Instance Methods

build_notice_for(exception, opts = {}) click to toggle source
# File lib/hoptoad_notifier.rb, line 132
def build_notice_for(exception, opts = {})
  exception = unwrap_exception(exception)
  if exception.respond_to?(:to_hash)
    opts = opts.merge(exception.to_hash)
  else
    opts = opts.merge(:exception => exception)
  end
  Notice.new(configuration.merge(opts))
end
send_notice(notice) click to toggle source
# File lib/hoptoad_notifier.rb, line 126
def send_notice(notice)
  if configuration.public?
    sender.send_to_hoptoad(notice.to_xml)
  end
end
unwrap_exception(exception) click to toggle source
# File lib/hoptoad_notifier.rb, line 142
def unwrap_exception(exception)
  if exception.respond_to?(:original_exception)
    exception.original_exception
  elsif exception.respond_to?(:continued_exception)
    exception.continued_exception
  else
    exception
  end
end