Class: Errplane::Api

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/errplane/api.rb

Constant Summary collapse

POST_RETRIES =
5
READ_TIMEOUT =
3
OPEN_TIMEOUT =
3
HTTP_ERRORS =
[ EOFError,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EINVAL,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError,
Timeout::Error ].freeze

Constants included from Logger

Logger::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



5
6
7
# File 'lib/errplane/api.rb', line 5

def last_response
  @last_response
end

Instance Method Details

#post(data) ⇒ Object



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
# File 'lib/errplane/api.rb', line 20

def post(data)
  https = initialize_secure_connection
  retry_count = POST_RETRIES
  log :debug, "POSTing to #{url}"

  response = begin
               https.post(url, data)
             rescue *HTTP_ERRORS => e
               log :error, "HTTP error contacting API! #{e.class}: #{e.message}"
               retry_count -= 1
               unless retry_count.zero?
                 log :info, "Retrying failed POST..."
                 sleep 1
                 retry
               end
               log :info, "Unable to POST after #{POST_RETRIES} attempts. Aborting!"
             end

  if response.is_a?(Net::HTTPSuccess)
    log :info, "POST Succeeded: #{response.inspect}"
  else
    log :error, "POST Failed: #{response.inspect}"
  end

  @last_response = response
end