Module: HoptoadNotifier

Defined in:
lib/merb_hoptoad_notifier/hoptoad_notifier.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 5

def api_key
  @api_key
end

.loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 5

def logger
  @logger
end

Class Method Details

.clean_hoptoad_environment(env) ⇒ Object

:nodoc:



124
125
126
127
128
129
130
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 124

def clean_hoptoad_environment(env) #:nodoc:
  env.each do |k, v|
    env[k] = "[FILTERED]" if HoptoadNotifier.environment_filters.any? do |filter|
      k.to_s.match(/#{filter}/)
    end
  end
end

.configureObject



7
8
9
10
11
12
13
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 7

def configure
  key = YAML.load_file(Merb.root / 'config' / 'hoptoad.yml')
  if key
    env = key[Merb.env.to_sym]
    env ? @api_key = env[:api_key] : raise(ArgumentError, "No hoptoad key for Merb environment #{Merb.env}")
  end
end

.default_notice_optionsObject

:nodoc:



107
108
109
110
111
112
113
114
115
116
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 107

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

.environment_filtersObject



19
20
21
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 19

def environment_filters
  @environment_filters ||= %w(AWS_ACCESS_KEY  AWS_SECRET_ACCESS_KEY AWS_ACCOUNT SSH_AUTH_SOCK)
end

.notify_hoptoad(request, session) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 51

def notify_hoptoad(request, session)
  return if request.nil?
  params = request.params

  request.exceptions.each do |exception|
    data = {
      :api_key       => HoptoadNotifier.api_key,
      :error_class   => Extlib::Inflection.camelize(exception.class.name),
      :error_message => "#{Extlib::Inflection.camelize(exception.class.name)}: #{exception.message}",
      :backtrace     => exception.backtrace,
      :environment   => ENV.to_hash
    }

    data[:request] = {
      :params => params
    }

    data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env))
    data[:environment][:RAILS_ENV] = Merb.env

    data[:session] = {
       :key         => session.instance_variable_get("@session_id"),
       :data        => session.to_hash
    }

    send_to_hoptoad :notice => default_notice_options.merge(data)
  end
  true
end

.send_to_hoptoad(data) ⇒ Object

:nodoc:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 81

def send_to_hoptoad(data) #:nodoc:
  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, stringify_keys(data).to_yaml, headers)
               rescue TimeoutError => e
                 logger.error "Timeout while contacting the Hoptoad server."
                 nil
               end
    case response
    when Net::HTTPSuccess then
      logger.info "Hoptoad Success: #{response.class}"
    else
      logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
    end
  end            
end

.stringify_keys(hash) ⇒ Object

:nodoc:



118
119
120
121
122
123
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 118

def stringify_keys(hash) #:nodoc:
  hash.inject({}) do |h, pair|
    h[pair.first.to_s] = pair.last.is_a?(Hash) ? stringify_keys(pair.last) : pair.last
    h
  end
end

.warn_hoptoad(message, request, session, options = {}) ⇒ Object



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
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 23

def warn_hoptoad(message, request, session, options={})
  return if request.nil?
  params = request.params

  data = {
    :api_key       => HoptoadNotifier.api_key,
    :error_class   => options[:error_class] || message,
    :error_message => message,
    :backtrace     => caller,
    :environment   => ENV.to_hash
  }

  data[:request] = {
    :params => params
  }

  data[:environment] = clean_hoptoad_environment(ENV.to_hash.merge(request.env))
  data[:environment][:RAILS_ENV] = Merb.env

  data[:session] = {
     :key         => session.instance_variable_get("@session_id"),
     :data        => session.to_hash
  }

  send_to_hoptoad :notice => default_notice_options.merge(data)
  true
end