Sha256: f7d7383d8b9a0fd997e24b46120f071ad686886eb0e61fe975e8e6dd12ced84d

Contents?: true

Size: 1.34 KB

Versions: 6

Compression:

Stored size: 1.34 KB

Contents

require 'plissken'

require 'smartsheet/error'

module Smartsheet
  module API
    # Passes requests to the decorated client, and parses responses into expected result types
    class ResponseNetClientDecorator
      def initialize(client, json_output: false, logger: MuteRequestLogger.new)
        @json_output = json_output
        @client = client
        @logger = logger
      end

      def make_request(request)
        response = begin
          client.make_request(request)
        rescue Smartsheet::HttpResponseError => e
          logger.log_http_error_response(request, e)
          raise e
        end

        parse(request, response)
      end

      private

      attr_reader :json_output, :client, :logger

      def parse(request, response)
         if response.success?
           logger.log_successful_response(response)
           parse_success(response)
         else
           logger.log_api_error_response(request, response)
           parse_failure(response)
         end
      end

      def parse_success(response)
        if json_output
          response.result.to_json
        elsif response.result.respond_to? :to_snake_keys
          response.result.to_snake_keys
        else
          response.result
        end
      end

      def parse_failure(response)
        raise Smartsheet::ApiError.new(response)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
smartsheet-2.101.1 lib/smartsheet/api/response_net_client_decorator.rb
smartsheet-2.101.0 lib/smartsheet/api/response_net_client_decorator.rb
smartsheet-2.86.1 lib/smartsheet/api/response_net_client_decorator.rb
smartsheet-2.86.0 lib/smartsheet/api/response_net_client_decorator.rb
smartsheet-2.77.2 lib/smartsheet/api/response_net_client_decorator.rb
smartsheet-1.0.0 lib/smartsheet/api/response_net_client_decorator.rb