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
46
# 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
               log :debug, "POSTing data:\n#{data.to_json}"
               https.post(url, data.to_json)
             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

#send(data, operator = "r") ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/errplane/api.rb', line 48

def send(data, operator="r")
  packet = {
    :d => Errplane.configuration.database_name,
    :a => Errplane.configuration.api_key.to_s,
    :o => operator,
    :w => [data]
  }

  log :debug, "Sending UDP Packet: #{packet.to_json}"

  begin
    udp_socket.send packet.to_json, 0, Errplane.configuration.api_udp_host, Errplane.configuration.api_udp_port
  rescue => e
    log :error, "Failed to send data via UDP. Check your network settings and available file descriptors. #{e.class}: #{e.message}"
  end
end