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.



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

def api_key
  @api_key
end

.loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

.secureObject Also known as: secure?

Returns the value of attribute secure.



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

def secure
  @secure
end

Class Method Details

.clean_hoptoad_environment(env) ⇒ Object

:nodoc:



173
174
175
176
177
178
179
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 173

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

.clean_non_serializable_data(notice) ⇒ Object

:nodoc:



152
153
154
155
156
157
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 152

def clean_non_serializable_data(notice) #:nodoc:
  notice.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

.configureObject



10
11
12
13
14
15
16
17
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 10

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
  self.secure = Merb::Plugins.config[:merb_hoptoad_notifier][:secure]
end

.default_notice_optionsObject

:nodoc:



141
142
143
144
145
146
147
148
149
150
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 141

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

.environment_filtersObject



33
34
35
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 33

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

.notify_hoptoad(request, session) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 64

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

  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
    }

    id = send_to_hoptoad :notice => default_notice_options.merge(data)
    ids << id if id
  end
  ids.empty? ? false : ids
end

.notify_hoptoad_exception(exception) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 96

def notify_hoptoad_exception(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[:environment][:RAILS_ENV] = Merb.env

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

.send_to_hoptoad(data) ⇒ Object

:nodoc:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 111

def send_to_hoptoad(data) #:nodoc:
  url = self.url

  connection = Net::HTTP.new(url.host, url.port)
  connection.read_timeout = 5 # seconds
  connection.open_timeout = 2 # seconds
  connection.use_ssl = !!HoptoadNotifier.secure
  connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  connection.start do |http|
    headers = {
      'Content-type' => 'application/x-yaml',
      'Accept' => 'text/xml, application/xml'
    }

    response = begin
                 http.post(url.path, clean_non_serializable_data(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}"
      return response.body[/<id type=.integer.>(\d+)<\/id>/, 1].to_i
    else
      logger.error "Hoptoad Failure: #{response.class}\n#{response.body if response.respond_to? :body}"
    end
  end
end

.serializable?(value) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


159
160
161
162
163
164
165
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 159

def serializable?(value) #:nodoc:
  value.is_a?(Fixnum) ||
  value.is_a?(Array)  ||
  value.is_a?(String) ||
  value.is_a?(Hash)   ||
  value.is_a?(Bignum)
end

.stringify_keys(hash) ⇒ Object

:nodoc:



167
168
169
170
171
172
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 167

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

.urlObject



19
20
21
22
23
24
25
26
27
# File 'lib/merb_hoptoad_notifier/hoptoad_notifier.rb', line 19

def url
  @url ||= begin
    if secure?
      URI.parse("https://hoptoadapp.com:443/notices/")
    else
      URI.parse("http://hoptoadapp.com:80/notices/")
    end
  end
end

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



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

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)
end