lib/runcible/base.rb in runcible-2.8.0 vs lib/runcible/base.rb in runcible-2.8.1

- old
+ new

@@ -3,13 +3,16 @@ require 'json' require 'thread' module Runcible class Base + attr_accessor :logs + def initialize(config = {}) @mutex = Mutex.new @config = config + @logs = [] end def lazy_config=(a_block) @mutex.synchronize { @lazy_config = a_block } end @@ -26,15 +29,15 @@ self.class.path(*args) end # rubocop:disable Metrics/AbcSize: def call(method, path, options = {}) + self.logs = [] clone_config = self.config.clone #on occation path will already have prefix (sync cancel) path = clone_config[:api_path] + path unless path.start_with?(clone_config[:api_path]) - RestClient.log = [] headers = clone_config[:headers].clone get_params = options[:params] if options[:params] path = combine_get_params(path, get_params) if get_params @@ -63,25 +66,33 @@ args = [method] args << generate_payload(options) if [:post, :put].include?(method) args << headers + self.logs << ([method.upcase, URI.join(client.url, path)] + args[1..-1]).join(': ') response = get_response(client, path, *args) - process_response(response) - + processed = process_response(response) + self.logs << "Response: #{response.code}: #{response.body}" + log_info + processed rescue RestClient::ResourceNotFound => e + self.logs << exception_to_log(e) log_info raise e rescue => e + self.logs << exception_to_log(e) log_exception raise e end + def exception_to_log(exception, body = exception.try(:response).try(:body)) + "#{exception.message}: #{body}" + end + def get_response(client, path, *args) client[path].send(*args) do |response, _request, _result, &_block| resp = response.return! - log_debug return resp end end def combine_get_params(path, params) @@ -133,12 +144,12 @@ body = body.map do |i| i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i end end response = Runcible::Response.new(body, response) - rescue JSON::ParserError - log_exception + rescue JSON::ParserError => e + self.logs << "Unable to parse JSON: #{e.message}" end return response end @@ -186,31 +197,18 @@ headers['Authorization'] = http_request['Authorization'] return headers end def log_debug - if self.config[:logging][:debug] - log_message = generate_log_message - self.config[:logging][:logger].debug(log_message) - end + self.config[:logging][:logger].debug(self.logs.join("\n")) if self.config[:logging][:debug] end def log_exception - if self.config[:logging][:exception] - log_message = generate_log_message - self.config[:logging][:logger].error(log_message) - end + self.config[:logging][:logger].error(self.logs.join("\n")) if self.config[:logging][:exception] end def log_info - if self.config[:logging][:info] - log_message = generate_log_message - self.config[:logging][:logger].info(log_message) - end - end - - def generate_log_message - RestClient.log.join('\n') + self.config[:logging][:logger].info(self.logs.join("\n")) if self.config[:logging][:info] end def logger self.config[:logging][:logger] end