lib/intercom/request.rb in intercom-0.0.11 vs lib/intercom/request.rb in intercom-0.0.12

- old
+ new

@@ -1,75 +1,73 @@ require 'cgi' require 'net/https' module Intercom class Request - attr_accessor :uri, :net_http_method + attr_accessor :path, :net_http_method - def initialize(uri, net_http_method) - set_common_headers(net_http_method, uri) - - self.uri = uri + def initialize(path, net_http_method) + self.path = path self.net_http_method = net_http_method end - def set_common_headers(method, uri) - method.basic_auth(CGI.unescape(uri.user), CGI.unescape(uri.password)) + def set_common_headers(method, base_uri) + method.basic_auth(CGI.unescape(base_uri.user), CGI.unescape(base_uri.password)) method.add_field('Accept', 'application/json') method.add_field('AcceptEncoding', 'gzip, deflate') end - def self.get(url, params) - uri = URI.parse(url) - new(uri, Net::HTTP::Get.new(append_query_string_to_url(uri.path, params), default_headers)) + def self.get(path, params) + new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers)) end - def self.post(url, form_data) - uri = URI.parse(url) - new(uri, method_with_body(Net::HTTP::Post, uri, form_data)) + def self.post(path, form_data) + new(path, method_with_body(Net::HTTP::Post, path, form_data)) end - def self.delete(url, params) - uri = URI.parse(url) - new(uri, Net::HTTP::Delete.new(append_query_string_to_url(uri.path, params), default_headers)) + def self.delete(path, params) + new(path, Net::HTTP::Delete.new(append_query_string_to_url(path, params), default_headers)) end - def self.put(url, form_data) - uri = URI.parse(url) - new(uri, method_with_body(Net::HTTP::Put, uri, form_data)) + def self.put(path, form_data) + new(path, method_with_body(Net::HTTP::Put, path, form_data)) end - def self.method_with_body(http_method, uri, params) - request = http_method.send(:new, uri.request_uri, default_headers) + def self.method_with_body(http_method, path, params) + request = http_method.send(:new, path, default_headers) request.body = params.to_json request["Content-Type"] = "application/json" request end def self.default_headers {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/json'} end - def client + def client(uri) net = Net::HTTP.new(uri.host, uri.port) if uri.is_a?(URI::HTTPS) - net.use_ssl = uri.is_a?(URI::HTTPS) + net.use_ssl = true net.verify_mode = OpenSSL::SSL::VERIFY_PEER net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem') end net.read_timeout = 30 - net.open_timeout = 10 + net.open_timeout = 3 net end - def execute - client.start do |http| + def execute(target_base_url=nil) + base_uri = URI.parse(target_base_url) + set_common_headers(net_http_method, base_uri) + client(base_uri).start do |http| response = http.request(net_http_method) raise_errors_on_failure(response) decoded = decode(response['content-encoding'], response.body) JSON.parse(decoded) end + rescue Timeout::Error + raise Intercom::ServiceUnavailableError end def decode(content_encoding, body) return body if (!body) || body.empty? || content_encoding != 'gzip' Zlib::GzipReader.new(StringIO.new(body)).read @@ -78,9 +76,11 @@ def raise_errors_on_failure(res) if res.code.to_i.eql?(404) raise Intercom::ResourceNotFound elsif res.code.to_i.eql?(401) raise Intercom::AuthenticationError + elsif res.code.to_i.eql?(503) + raise Intercom::ServiceUnavailableError elsif res.code.to_i.eql?(500) raise Intercom::ServerError end end \ No newline at end of file