Module: PowerTrackV2::HTTP

Defined in:
lib/powertrack_v2/http.rb

Class Method Summary collapse

Class Method Details

.auth_digest(username, password) ⇒ Object



7
8
9
# File 'lib/powertrack_v2/http.rb', line 7

def self.auth_digest(username, password)
  ::Base64.strict_encode64("#{username}:#{password}")
end

.auth_header(auth_digest) ⇒ Object



11
12
13
# File 'lib/powertrack_v2/http.rb', line 11

def self.auth_header(auth_digest)
  {'Authorization' => "Basic #{auth_digest}"}
end

.get(url, headers = {}) ⇒ Object



23
24
25
# File 'lib/powertrack_v2/http.rb', line 23

def self.get(url, headers={})
  ::HTTPClient.get(url, nil, headers)
end

.headers(headers = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/powertrack_v2/http.rb', line 15

def self.headers(headers = {})
  username = ENV['POWERTRACK_USERNAME']
  password = ENV['POWERTRACK_PASSWORD']

  digest = auth_digest(username, password)
  headers.merge auth_header(digest)
end

.post(url, body, headers = {}) ⇒ Object



27
28
29
# File 'lib/powertrack_v2/http.rb', line 27

def self.post(url, body, headers={})
  ::HTTPClient.post(url, body, headers)
end

.stream(url, headers = {}, opts = { ignore_heartbeat: true }, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/powertrack_v2/http.rb', line 31

def self.stream(url, headers = {}, opts = { ignore_heartbeat: true }, &block)
  http = ::HTTPClient.new
  backoff = ::ExponentialBackoff.new(1, 120)

  while true
    begin
      connection = connect(url, headers)
      stream_from(connection, opts, backoff, &block)
    rescue StandardError => e
      sleep(backoff.next_interval)
    end
  end
end