Class: Toadhopper

Inherits:
Object
  • Object
show all
Defined in:
lib/toadhopper.rb

Overview

Posts errors to the Airbrake API

Defined Under Namespace

Classes: BacktraceLine, Response

Constant Summary collapse

VERSION =
'2.1'
FILTER_REPLACEMENT =
"[FILTERED]"
DEFAULT_DOMAIN =
'airbrake.io'
DEFAULT_NOTIFY_HOST =
'http://'+DEFAULT_DOMAIN
CA_FILE =

CA_FILE: Path to an updated certificate authority file, which was built from source If you provide a custom Net :transport and get erroneous SSL peer verification failures, try setting the transport’s ca_file to Toadhopper::CA_FILE

File.expand_path File.join('..', 'resources', 'ca-bundle.crt'),
File.dirname(__FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, params = {}) ⇒ Toadhopper

Initialize and configure a Toadhopper

Parameters:

  • Your (String)

    api key

  • params (Hash) (defaults to: {})
    optional

    :notify_host - [String] The default host to use :error_url - [String] Absolute URL to use for error reporting :deploy_url - [String] Absolute URL to use for deploy tracking :transport - [Net::HTTP|Net::HTTP::Proxy] A customized Net::* object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/toadhopper.rb', line 33

def initialize(api_key, params = {})
  @filters    = []
  @api_key    = api_key

  notify_host = URI.parse(params[:notify_host] || DEFAULT_NOTIFY_HOST)
  @transport  = params.delete :transport
  if @transport and not params[:notify_host]
    notify_host.scheme  = 'https' if @transport.use_ssl?
    notify_host.host    = @transport.address
    notify_host.port    = @transport.port
  end

  @error_url  = URI.parse(params.delete(:error_url)  || "#{notify_host}/notifier_api/v2/notices")
  @deploy_url = URI.parse(params.delete(:deploy_url) || "#{notify_host}/deploys.txt")

  validate!
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



22
23
24
# File 'lib/toadhopper.rb', line 22

def api_key
  @api_key
end

#deploy_urlObject (readonly)

Returns the value of attribute deploy_url.



22
23
24
# File 'lib/toadhopper.rb', line 22

def deploy_url
  @deploy_url
end

#error_urlObject (readonly)

Returns the value of attribute error_url.



22
23
24
# File 'lib/toadhopper.rb', line 22

def error_url
  @error_url
end

Instance Method Details

#connection(uri) ⇒ Object

Provider of the net transport used

MIT Licensing Note: Portions of logic below for connecting via SSL were copied from the airbrake project under the MIT License.



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/toadhopper.rb', line 144

def connection(uri)
  return @transport if @transport

  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = 5 # seconds
  http.open_timeout = 2 # seconds
  if uri.scheme.eql? 'https'
    http.use_ssl      = true
    http.ca_file      = CA_FILE
    http.verify_mode  = OpenSSL::SSL::VERIFY_PEER
  end
  http
end

#deploy!(options = {}) ⇒ Response

Posts a deployment notification

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • framework_env (String)

    The framework environment your app is running under, defaults to development

  • scm_repository (String)

    The repository URL

  • scm_revision (String)

    The current repository revision

  • username (String)

    Your name, defaults to ‘whoami`

Returns:



124
125
126
127
128
129
130
131
132
# File 'lib/toadhopper.rb', line 124

def deploy!(options={})
  params = {}
  params['api_key'] = @api_key
  params['deploy[rails_env]'] = options[:framework_env] || 'development'
  params['deploy[local_username]'] = options[:username] || %x(whoami).strip
  params['deploy[scm_repository]'] = options[:scm_repository]
  params['deploy[scm_revision]'] = options[:scm_revision]
  response(@deploy_url, params)
end

#filters=(*filters) ⇒ Object

Sets patterns to [FILTER] out sensitive data such as /password/, /email/ and /credit_card_number/



80
81
82
# File 'lib/toadhopper.rb', line 80

def filters=(*filters)
  @filters = filters.flatten
end

#post!(error, options = {}, http_headers = {}) ⇒ Response

Posts an exception to Airbrake.

Examples:

Toadhopper('apikey').post! error,
                           {:action => 'show', :controller => 'Users'},
                           {'X-Airbrake-Client-Name' => 'My Awesome Notifier'}

Parameters:

  • error (Exception)

    the error to post

  • options (Hash) (defaults to: {})
  • http_headers (Hash) (defaults to: {})

    extra HTTP headers to be sent in the post to the API

Options Hash (options):

  • url (String)

    The url for the request, required to post but not useful in a console environment

  • component (String)

    Normally this is your Controller name in an MVC framework

  • action (String)

    Normally the action for your request in an MVC framework

  • params (Hash)

    A hash of the request’s parameters

  • notifier_name (String)

    Say you’re a different notifier than Toadhopper

  • notifier_version (String)

    Specify the version of your custom notifier

  • notifier_url (String)

    Specify the project URL of your custom notifier

  • session (Hash)

    A hash of the user session in a web request

  • framework_env (String)

    The framework environment your app is running under

  • backtrace (Array)

    Normally not needed, parsed automatically from the provided exception parameter

  • environment (Hash)

    You MUST scrub your environment if you plan to use this, please do not use it though. :)

  • project_root (String)

    The root directory of your app

Returns:



110
111
112
113
# File 'lib/toadhopper.rb', line 110

def post!(error, options={}, http_headers={})
  options[:notifier_name] ||= 'Toadhopper'
  post_document(document_for(error, options), {'X-Airbrake-Client-Name' => options[:notifier_name]})
end

#secure?Boolean

Returns:

  • (Boolean)


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

def secure?
  connection(@deploy_url).use_ssl? and connection(@error_url).use_ssl?
end

#validate!Object



51
52
53
54
# File 'lib/toadhopper.rb', line 51

def validate!
  validate_url! :error_url
  validate_url! :deploy_url
end

#validate_url!(sym) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toadhopper.rb', line 56

def validate_url!(sym)
  url = instance_variable_get '@'+sym.to_s
  unless url.absolute?
    raise ToadhopperException, "#{sym} #{url.inspect} must begin with http:// or https://"
  end

  if @transport
    if @transport.use_ssl? != url.scheme.eql?('https')
      raise ToadhopperException,
        ":transport use_ssl? setting of #{@transport.use_ssl?.inspect} does not match" +
        " #{sym} scheme #{url.scheme.inspect}"
    elsif @transport.address != url.host
      raise ToadhopperException,
        ":transport hostname #{@transport.address.inspect} does not match" +
        " #{sym} hostname #{url.host.inspect}"
    elsif @transport.port != url.port
      raise ToadhopperException,
        ":transport port #{@transport.port.inspect} does not match" +
        " #{sym} port #{url.port.inspect}"
    end
  end
end