This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
forked from thoughtbot/hoptoad_notifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sender.rb
63 lines (50 loc) · 1.83 KB
/
sender.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
module HoptoadNotifier
# Sends out the notice to Hoptoad
class Sender
NOTICES_URI = '/notifier_api/v2/notices/'.freeze
def initialize(options = {})
[:proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol,
:host, :port, :secure, :http_open_timeout, :http_read_timeout].each do |option|
instance_variable_set("@#{option}", options[option])
end
end
# Sends the notice data off to Hoptoad for processing.
#
# @param [String] data The XML notice to be sent off
def send_to_hoptoad(data)
logger.debug { "Sending request to #{url.to_s}:\n#{data}" }
http =
Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).
new(url.host, url.port)
http.read_timeout = http_read_timeout
http.open_timeout = http_open_timeout
http.use_ssl = secure
response = begin
http.post(url.path, data, HEADERS)
rescue TimeoutError => e
log :error, "Timeout while contacting the Hoptoad server."
nil
end
case response
when Net::HTTPSuccess then
log :info, "Success: #{response.class}", response
else
log :error, "Failure: #{response.class}", response
end
end
private
attr_reader :proxy_host, :proxy_port, :proxy_user, :proxy_pass, :protocol,
:host, :port, :secure, :http_open_timeout, :http_read_timeout
def url
URI.parse("#{protocol}://#{host}:#{port}").merge(NOTICES_URI)
end
def log(level, message, response = nil)
logger.send level, LOG_PREFIX + message if logger
HoptoadNotifier.report_environment_info
HoptoadNotifier.report_response_body(response.body) if response && response.respond_to?(:body)
end
def logger
HoptoadNotifier.logger
end
end
end