class ExceptionNotifier::IdobataNotifier

Attributes

url[R]

Public Class Methods

new(options) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 9
def initialize(options)
  @options = extract_global_options(options, :url, :skip_library_backtrace, :proxy)

  raise(ArgumentError, 'Endpoint must be specified') unless @url
end

Public Instance Methods

call(exception, options={}) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 15
def call(exception, options={})
  enviroments =
    if options[:env]
      request_option(ActionDispatch::Request.new(options[:env]))
    else
      # [TODO] - Add option specification route for non-web notification
      {'Timestamp' => Time.zone.now}
    end

  source = build_message(exception, enviroments)

  http_client.post_form URI.parse(url),  source: source, format: :html
end

Private Instance Methods

app_root() click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 107
def app_root
  Dir.pwd
end
build_message(exception, enviroments) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 64
    def build_message(exception, enviroments)
      return <<-HTML
<span class='label label-danger'>#{exception.class.to_s}</span>
<b>#{CGI.escapeHTML(exception.message.inspect)}</b>

<h4>Backtrace:</h4>
<pre>#{format_backtrace(exception.backtrace).join("\n")}</pre>

<h4>Environments:</h4>
<table>
  <tbody>
    #{table_rows_from(@options.merge(enviroments))}
  </tbody>
</table>
      HTML
    end
bundle_root() click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 103
def bundle_root
  Bundler.bundle_path.to_s if defined?(Bundler)
end
extract_global_options(options, *global_option_names) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 48
def extract_global_options(options, *global_option_names)
  options.dup.tap do |opts|
    global_option_names.each {|name| instance_variable_set "@#{name}", opts.delete(name) }
  end
end
extract_proxy_settings(proxy) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 35
def extract_proxy_settings(proxy)
  case proxy
  when String
    extract_proxy_settings(URI(proxy))
  when URI
    [proxy.host, proxy.port]
  when Hash
    proxy.values_at(:host, :port)
  else
    proxy
  end
end
format_backtrace(trace) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 87
def format_backtrace(trace)
  trace.map { |line|
    if from_bundler?(line)
      @skip_library_backtrace ? nil : line.sub(bundle_root, '[bundle]...')
    else
      line.sub(app_root, '[app]...')
    end
  }.compact
end
from_bundler?(line) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 97
def from_bundler?(line)
  if bundle_root
    line.match(bundle_root)
  end
end
http_client() click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 31
def http_client
  @proxy ? Net::HTTP.Proxy(*extract_proxy_settings(@proxy)) : Net::HTTP
end
request_option(request) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 54
def request_option(request)
  {
    'URL'         => request.original_url,
    'HTTP Method' => request.method,
    'IP Address'  => request.remote_ip,
    'Paramters'   => request.filtered_parameters,
    'Timestamp'   => Time.zone.now
  }
end
table_rows_from(hash) click to toggle source
# File lib/exception_notifier/idobata_notifier.rb, line 81
def table_rows_from(hash)
  hash.each_with_object('') { |(key, value), rows|
    rows << "<tr><th>#{key}</th><td>#{value}</td></tr>"
  }
end