lib/smartsheet/api/faraday_adapter/faraday_response.rb in smartsheet-1.0.0.beta.2 vs lib/smartsheet/api/faraday_adapter/faraday_response.rb in smartsheet-1.0.0

- old
+ new

@@ -1,32 +1,41 @@ module Smartsheet module API + # Builds response objects from Faraday response environments 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) + 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 - FaradaySuccessResponse.new(faraday_response[:body], faraday_response) + 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_response) - @status_code = faraday_response[:status] - @reason_phrase = faraday_response[:reason_phrase] - @headers = faraday_response[:response_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_response) - super(faraday_response) + def initialize(result, faraday_env) + super(faraday_env) @error_code = result[:errorCode] @message = result[:message] @ref_id = result[:refId] @detail = result[:detail] end @@ -38,14 +47,15 @@ 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_response) - super(faraday_response) + def initialize(result, faraday_env) + super(faraday_env) @result = result end def should_retry? false