Class: Errplane::Api

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

Constant Summary collapse

HTTPS_HOST =
"w.apiv3.errplane.com"
UDP_HOST =
"udp.apiv3.errplane.com"
UDP_PORT =
8126
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



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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/errplane/api.rb', line 52

def send(data, operator="r")
  udp = UDPSocket.new

  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}"

  udp.send packet.to_json, 0, UDP_HOST, UDP_PORT
end