lib/tamber.rb in tamber-0.1.3 vs lib/tamber.rb in tamber-0.1.4

- old
+ new

@@ -40,11 +40,11 @@ @api_url = 'https://api.tamber.com/v1' @open_timeout = 30 @read_timeout = 80 class << self - attr_accessor :project_key, :engine_key, :api_base, :api_version, :verify_ssl_certs, :open_timeout, :read_timeout + attr_accessor :project_key, :engine_key, :api_version, :verify_ssl_certs, :open_timeout, :read_timeout end def self.api_url(api_base_url=nil, url='') (api_base_url || @api_url) + url @@ -57,11 +57,10 @@ def self.ca_bundle_path=(path) @ca_bundle_path = path end def self.request(method, url, params={}) - api_base_url = api_base_url || @api_base if project_key =~ /\s/ raise TamberError.new('Your project key is invalid, as it contains ' \ 'whitespace. (HINT: You can double-check your project key from the ' \ 'Tamber dashboard. See https://dashboard.tamber.com to get your engine\'s key, or ' \ @@ -86,12 +85,11 @@ "You should never do this in production. " \ "Execute 'Tamber.verify_ssl_certs = true' to enable verification.") end end - # params = Util.objects_to_ids(params) - url = api_url(api_base_url,url) + url = @api_url + url case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any? @@ -102,19 +100,21 @@ request_opts.update(:headers => request_headers(project_key, engine_key, method), :method => method, :open_timeout => open_timeout, :payload => payload, :url => url, :timeout => read_timeout) - response = execute_request_with_rescues(request_opts, api_base_url) + response = execute_request_with_rescues(request_opts) [parse(response)] end - def self.request_headers(project_key, engine_key, method) - encoded_key = Base64.encode64(project_key + ':' + engine_key) + def self.request_headers(pkey, ekey, method) + pkey ||= '' + ekey ||= '' + encoded_key = Base64.encode64(pkey + ':' + ekey) headers = { :user_agent => "Tamber/v1 RubyBindings/#{Tamber::VERSION}", :authorization => "Basic "+encoded_key, :content_type => 'application/x-www-form-urlencoded' } @@ -146,10 +146,14 @@ def self.general_api_error(rbody) TamberError.new("Invalid response object from API: "+rbody.inspect) end + def self.handle_restclient_error(e, request_opts) + parse(e) + end + def self.execute_request(opts) RestClient::Request.execute(opts) end def self.parse(response) @@ -161,38 +165,38 @@ raise TamberError.new("Error: "+response["error"]) end rescue JSON::ParserError raise general_api_error(response.body) end - Util.symbolize_names(response) end private - def self.execute_request_with_rescues(request_opts, api_base_url, retry_count = 0) - response = execute_request(request_opts) - # begin - # response = execute_request(request_opts) - # rescue SocketError => e - # response = handle_restclient_error(e, request_opts, retry_count, api_base_url) - # rescue NoMethodError => e - # # Work around RestClient bug - # if e.message =~ /\WRequestFailed\W/ - # e = APIConnectionError.new('Unexpected HTTP response code') - # response = handle_restclient_error(e, request_opts, retry_count, api_base_url) - # else - # raise - # end - # rescue RestClient::ExceptionWithResponse => e - # if e.response - # handle_api_error(e.response) - # else - # response = handle_restclient_error(e, request_opts, retry_count, api_base_url) - # end - # rescue RestClient::Exception, Errno::ECONNREFUSED => e - # response = handle_restclient_error(e, request_opts, retry_count, api_base_url) - # end + def self.execute_request_with_rescues(request_opts) + # response = execute_request(request_opts) + begin + response = execute_request(request_opts) + rescue SocketError => e + response = handle_restclient_error(e, request_opts) + rescue NoMethodError => e + # Work around RestClient bug + if e.message =~ /\WRequestFailed\W/ + raise TamberError.new('Error: Unexpected HTTP response code') + else + raise + end + rescue RestClient::ExceptionWithResponse => e + puts "ExceptionWithResponse: #{e}" + if e.response + puts "e.response" + parse(e.response) + else + response = handle_restclient_error(e, request_opts) + end + rescue RestClient::Exception, Errno::ECONNREFUSED => e + response = handle_restclient_error(e, request_opts) + end - # response + response end end