bdotdub (owner)

Revisions

gist: 131064 Download_button fork
public
Description:
Hoptoad notifier for Sinatra
Public Clone URL: git://gist.github.com/131064.git
error_handler.rb
1
2
3
4
5
6
error do
  exception = request.env['sinatra.error']
  Hoptoad::Notifier.send_error exception, params, request.env if %w(staging production).include?(ENV['RACK_ENV'])
 
  erb :five_hundred
end
hoptoad_notifier.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Hoptoad
  class Notifier
    def self.send_error(exception, params, env)
      data = {
        'api_key' => ENV['HOPTOAD_API_KEY'],
        'error_class' => exception.class.name,
        'error_message' => "#{exception.class.name}: #{exception.message}",
        'backtrace' => exception.backtrace,
        'environment' => env,
        'request' => {
          'params' => params,
          'path' => env['REQUEST_PATH']
        },
        'session' => {
          'key' => env['rack.session'],
          'data' => env['rack.session']
        }
      }
 
 
      data = { 'notice' => clean_non_serializable_data(data) }
    
      url = URI.parse( 'http://hoptoadapp.com:80/notices/' )
      Net::HTTP.start( url.host, url.port ) do |http|
        headers = {
          'Content-type' => 'application/x-yaml',
          'Accept' => 'text/xml, application/xml'
        }
        http.read_timeout = 5 # seconds
        http.open_timeout = 2 # seconds
        # http.use_ssl = HoptoadNotifier.secure
        response = begin
          http.post( url.path, data.to_yaml, headers )
        rescue TimeoutError => e
          puts "Timeout while contacting the Hoptoad server."
          nil
        end
        
        case response
        when Net::HTTPSuccess
          puts "Hoptoad notification sent."
        else
          puts "Hoptoad notification failure: #{response.class}"
          if response.respond_to? :body
            puts response.body
          end
        end
      end
    end
 
    def self.serializable?(value) #:nodoc:
      value.is_a?(Fixnum) ||
      value.is_a?(Array) ||
      value.is_a?(String) ||
      value.is_a?(Hash) ||
      value.is_a?(Bignum)
    end
    
    def self.clean_non_serializable_data(data) #:nodoc:
      data.select{|k,v| serializable?(v) }.inject({}) do |h, pair|
        h[pair.first] = pair.last.is_a?(Hash) ? clean_non_serializable_data(pair.last) : pair.last
        h
      end
    end
  end
end