lib/intercom/request.rb in intercom-2.5.4 vs lib/intercom/request.rb in intercom-3.0.0b1

- old
+ new

@@ -1,22 +1,25 @@ require 'cgi' require 'net/https' module Intercom class Request - attr_accessor :path, :net_http_method + attr_accessor :path, :net_http_method, :rate_limit_details def initialize(path, net_http_method) self.path = path self.net_http_method = net_http_method end def set_common_headers(method, base_uri) - method.basic_auth(CGI.unescape(base_uri.user), CGI.unescape(base_uri.password)) method.add_field('AcceptEncoding', 'gzip, deflate') end + def set_basic_auth(method, username, secret) + method.basic_auth(CGI.unescape(username), CGI.unescape(secret)) + end + def self.get(path, params) new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers)) end def self.post(path, form_data) @@ -52,13 +55,14 @@ net.read_timeout = 90 net.open_timeout = 30 net end - def execute(target_base_url=nil) + def execute(target_base_url=nil, username:, secret: nil) base_uri = URI.parse(target_base_url) set_common_headers(net_http_method, base_uri) + set_basic_auth(net_http_method, username, secret) begin client(base_uri).start do |http| begin response = http.request(net_http_method) set_rate_limit_details(response) @@ -72,11 +76,11 @@ end rescue Timeout::Error raise Intercom::ServiceConnectionError.new('Failed to connect to service [connection attempt timed out]') end end - + def decode_body(response) decode(response['content-encoding'], response.body) end def parse_body(decoded_body, response) @@ -94,10 +98,10 @@ 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 + @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