Sha256: 9b741d54470f107ba987ecf0031c0b343536cefa15bdd438056c8f6d08e7e2e9

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

module Smartsheet
  module API
    # Builds response objects from Faraday response environments
    class FaradayResponse
      def self.from_response_env(faraday_env)
        if faraday_env[:body].kind_of?(Hash) && faraday_env[:body].key?(:errorCode)
          FaradayErrorResponse.new(faraday_env[:body], faraday_env)
        elsif faraday_env.success?
          FaradaySuccessResponse.new(faraday_env[:body], faraday_env)
        else
          raise HttpResponseError.new(
              status_code: faraday_env[:status],
              reason_phrase: faraday_env[:reason_phrase],
              headers: faraday_env[:response_headers],
              message: "#{faraday_env[:status]} #{faraday_env[:reason_phrase]}"
          )
        end
      end

      attr_reader :status_code, :reason_phrase, :headers

      def initialize(faraday_env)
        @status_code = faraday_env[:status]
        @reason_phrase = faraday_env[:reason_phrase]
        @headers = faraday_env[:response_headers]
      end
    end

    # Provides a Smartsheet error response from a Faraday response environment
    class FaradayErrorResponse < FaradayResponse
      RETRYABLE_ERRORS = 4001..4004

      attr_reader :error_code, :message, :ref_id, :detail

      def initialize(result, faraday_env)
        super(faraday_env)
        @error_code = result[:errorCode]
        @message = result[:message]
        @ref_id = result[:refId]
        @detail = result[:detail]
      end

      def should_retry?
        RETRYABLE_ERRORS.include? error_code
      end

      def success?
        false
      end
    end

    # Provides a Smartsheet success response from a Faraday response environment
    class FaradaySuccessResponse < FaradayResponse
      attr_reader :result

      def initialize(result, faraday_env)
        super(faraday_env)
        @result = result
      end

      def should_retry?
        false
      end

      def success?
        true
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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