Class: Snooze::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/snooze/post.rb

Class Method Summary collapse

Class Method Details

.exception!(clock_id, backtrace = "") ⇒ Object

Pass an exception backtrace to Snooze



49
50
51
# File 'lib/snooze/post.rb', line 49

def self.exception!(clock_id, backtrace="")
  self.execute!('exception', "{ \"id\":\"#{clock_id}\", \"backtrace\":\"#{backtrace}\" }")
end

.execute!(action, post_body, retries = 4) ⇒ Object

Execute an HTTP post



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

def self.execute!(action, post_body, retries = 4)
  uri = self.generate_uri_from_action(action)
  response = nil

  begin
    connection = self.setup_connection(uri)

    response = connection.post uri.path do |request|
      request.headers['Content-Type'] = 'application/json'
      request.options[:timeout] = 5
      request.options[:open_timeout] = 2
      request.body = post_body
    end

    raise Faraday::Error::ConnectionFailed.new('') if response.status == 500
  rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::ClientError, Faraday::Error => e
    raise Snooze::ConnectionError if retries <= 0
    sleep_for_a_while(5 - retries)
    response = self.execute!(action, post_body, retries-1)
  end

  response
end

.generate_uri_from_action(action) ⇒ Object

Setup the api host URI for the given action



4
5
6
# File 'lib/snooze/post.rb', line 4

def self.generate_uri_from_action(action)
  URI.parse("#{Snooze::API_HOST}/#{Snooze::API_PATH}/#{action}")
end

.ping!(clock_id) ⇒ Object

Ping Snooze



39
40
41
# File 'lib/snooze/post.rb', line 39

def self.ping!(clock_id)
  self.execute!('ping', "{ \"id\":\"#{clock_id}\" }" )
end

.setup_connection(uri) ⇒ Object

Setup the connection



9
10
11
# File 'lib/snooze/post.rb', line 9

def self.setup_connection(uri)
  ::Faraday.new(:url => uri)
end

.snooze!(clock_id) ⇒ Object

Snooze an alarm



44
45
46
# File 'lib/snooze/post.rb', line 44

def self.snooze!(clock_id)
  self.execute!('snooze', "{ \"id\":\"#{clock_id}\" }")
end