module PowerTrackV2::HTTP
Public Class Methods
auth_digest(username, password)
click to toggle source
# File lib/powertrack_v2/http.rb, line 7 def self.auth_digest(username, password) ::Base64.strict_encode64("#{username}:#{password}") end
auth_header(auth_digest)
click to toggle source
# File lib/powertrack_v2/http.rb, line 11 def self.auth_header(auth_digest) {'Authorization' => "Basic #{auth_digest}"} end
get(url, headers={})
click to toggle source
# File lib/powertrack_v2/http.rb, line 23 def self.get(url, headers={}) ::HTTPClient.get(url, nil, headers) end
headers(headers = {})
click to toggle source
# 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={})
click to toggle source
# 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)
click to toggle source
# 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
Private Class Methods
connect(url, headers)
click to toggle source
# File lib/powertrack_v2/http.rb, line 47 def self.connect(url, headers) http = ::HTTPClient.new http.get_async(url, nil, headers) end
stream_from(connection, opts, backoff, &block)
click to toggle source
# File lib/powertrack_v2/http.rb, line 52 def self.stream_from(connection, opts, backoff, &block) ignore_heartbeat = opts[:ignore_heartbeat] content = connection.pop.content backoff.clear until connection.finished? next_response = content.gets block.call(next_response) unless ignore_heartbeat && next_response == "\r\n" end end