Class: ErrornotNotifier::Sender

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

Overview

Sends out the notice to Errornot

Constant Summary collapse

NOTICES_URI =
'/errors'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sender

Returns a new instance of Sender.



7
8
9
10
11
12
# File 'lib/errornot_notifier/sender.rb', line 7

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

#process_payload(p = nil, parent_key = nil) ⇒ Object

Methode extract from RestClient maybe need some test



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/errornot_notifier/sender.rb', line 16

def process_payload(p=nil, parent_key=nil)
  unless p.is_a?(Hash)
    p
  else
    p.keys.map do |k|
      key = parent_key ? "#{parent_key}[#{k}]" : k
      if p[k].is_a? Hash
        process_payload(p[k], key)
      elsif p[k].is_a? Array
        p[k].map do |v|
          value = URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
          "#{key}[]=#{value}"
        end
      else
        value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
        "#{key}=#{value}"
      end
    end.join("&")
  end
end

#send_to_errornot(data) ⇒ Object

Sends the notice data off to Errornot for processing.

Parameters:

  • data (String)

    The XML notice to be sent off



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
68
# File 'lib/errornot_notifier/sender.rb', line 41

def send_to_errornot(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
               # TODO see how use http.post or convert all to restclient
               #RestClient.post(url.to_s, data)
               data = process_payload(data)
               http.post(url.path, data, HEADERS)
             rescue TimeoutError => e
               log :error, "Timeout while contacting the Errornot server."
               nil
             end

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