Sha256: aba70c3f18025930745cbf8440395fbb48fd024e39e7dadfa8e66e681993ecfc
Contents?: true
Size: 1.81 KB
Versions: 3
Compression:
Stored size: 1.81 KB
Contents
module Wakatime class Session def initialize(opts = {}, backoff = 0.1) @backoff = backoff # try not to excessively hammer API. @api_key = opts[:api_key] end def get(url, raw = false) uri = URI.parse(url) request = Net::HTTP::Get.new(uri.request_uri) resp = request(uri, request, raw) end def request(uri, request, raw = false, _retries = 0) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true # http.set_debug_output($stdout) request.add_field('Content-Type', 'application/json') if @api_key request.add_field('Authorization', "Basic #{Base64.strict_encode64(@api_key)}") # end response = http.request(request) if response.is_a? Net::HTTPNotFound fail Wakatime::ObjectNotFound end sleep(@backoff) # try not to excessively hammer API. handle_errors(response, raw) end def handle_errors(response, raw) status = response.code.to_i body = response.body begin parsed_body = JSON.parse(body) rescue msg = body.nil? || body.empty? ? 'no data returned' : body parsed_body = { 'errors' => msg } end # status is used to determine whether # we need to refresh the access token. parsed_body['status'] = status case status / 100 when 3 # 302 Found. We should return the url parsed_body['location'] = response['Location'] if status == 302 when 4 fail(Wakatime::AuthError.new(parsed_body, status, body), parsed_body['errors']) if status == 401 fail(Wakatime::RequestError.new(parsed_body, status, body), parsed_body['errors']) when 5 fail(Wakatime::ServerError.new(parsed_body, status, body), parsed_body['errors']) end raw ? body : parsed_body end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
wakatime-0.2.0 | lib/wakatime/session.rb |
wakatime-0.1.0 | lib/wakatime/session.rb |
wakatime-0.0.2 | lib/wakatime/session.rb |