lib/intercom/request.rb in intercom-2.2.2 vs lib/intercom/request.rb in intercom-2.2.3

- old
+ new

@@ -59,10 +59,11 @@ set_common_headers(net_http_method, base_uri) begin client(base_uri).start do |http| begin response = http.request(net_http_method) + set_rate_limit_details(response) decoded = decode(response['content-encoding'], response.body) unless decoded.strip.empty? parsed_body = JSON.parse(decoded) raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list' end @@ -73,9 +74,17 @@ end end rescue Timeout::Error raise Intercom::ServiceConnectionError.new('Failed to connect to service [connection attempt timed out]') end + end + + def set_rate_limit_details(response) + rate_limit_details = {} + rate_limit_details[:limit] = response['X-RateLimit-Limit'].to_i if response['X-RateLimit-Limit'] + rate_limit_details[:remaining] = response['X-RateLimit-Remaining'].to_i if response['X-RateLimit-Remaining'] + rate_limit_details[:reset_at] = Time.at(response['X-RateLimit-Reset'].to_i) if response['X-RateLimit-Reset'] + Intercom.rate_limit_details = rate_limit_details end def decode(content_encoding, body) return body if (!body) || body.empty? || content_encoding != 'gzip' Zlib::GzipReader.new(StringIO.new(body)).read