Sha256: 368879019ba91c60ebd19fec2d1e4c0e940188a0c548f8b2855ca1c3ac9a683e

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

module Smartsheet
  module API
    class FaradayResponse
      def self.from_response_env(faraday_response)
        if faraday_response[:body].kind_of?(Hash) && faraday_response[:body].key?(:errorCode)
          FaradayErrorResponse.new(faraday_response[:body], faraday_response)
        else
          FaradaySuccessResponse.new(faraday_response[:body], faraday_response)
        end
      end

      attr_reader :status_code, :reason_phrase, :headers

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

    class FaradayErrorResponse < FaradayResponse
      RETRYABLE_ERRORS = 4001..4004

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

      def initialize(result, faraday_response)
        super(faraday_response)
        @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

    class FaradaySuccessResponse < FaradayResponse
      attr_reader :result

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

      def should_retry?
        false
      end

      def success?
        true
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smartsheet-1.0.0.beta.2 lib/smartsheet/api/faraday_adapter/faraday_response.rb
smartsheet-1.0.0.beta.0 lib/smartsheet/api/faraday_adapter/faraday_response.rb