Class: WhoopsLogger::Sender

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

Constant Summary collapse

NOTICES_URI =
'/events/'.freeze
HTTP_ERRORS =
[Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Errno::ECONNREFUSED].freeze
HEADERS =
{
  'Content-type'             => 'application/json',
  'Accept'                   => 'application/json'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sender

Returns a new instance of Sender.



18
19
20
21
22
23
# File 'lib/whoops_logger/sender.rb', line 18

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

Instance Method Details

#prepare_data(data) ⇒ Object



62
63
64
# File 'lib/whoops_logger/sender.rb', line 62

def prepare_data(data)
  {:event => data}.to_json
end

#send_message(data) ⇒ Object

Sends the notice data off to Whoops for processing.

Parameters:

  • data (Hash)

    The notice to be sent off



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
# File 'lib/whoops_logger/sender.rb', line 28

def send_message(data)
  # TODO: format
  # TODO: validation
  data = prepare_data(data)
  logger.debug { "Sending request to #{url.to_s}:\n#{data}" } if logger

  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 *HTTP_ERRORS => e
               log :error, "Timeout while contacting the Whoops server."
               nil
             end

  case response
  when Net::HTTPSuccess then
    log :info, "Success: #{response.class}", response
  else
    log :error, "Failure: #{response.class}", response
  end

  if response && response.respond_to?(:body)
    error_id = response.body.match(%r{<error-id[^>]*>(.*?)</error-id>})
    error_id[1] if error_id
  end
end