Class: Duracloud::ResponseHandler
- Inherits:
-
Object
- Object
- Duracloud::ResponseHandler
- Defined in:
- lib/duracloud/response_handler.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #error_message ⇒ Object
- #exception ⇒ Object
- #handle_error ⇒ Object
-
#initialize(response) ⇒ ResponseHandler
constructor
A new instance of ResponseHandler.
- #log_response ⇒ Object
- #loggable_response_body? ⇒ Boolean
Constructor Details
#initialize(response) ⇒ ResponseHandler
Returns a new instance of ResponseHandler.
10 11 12 |
# File 'lib/duracloud/response_handler.rb', line 10 def initialize(response) @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
8 9 10 |
# File 'lib/duracloud/response_handler.rb', line 8 def response @response end |
Class Method Details
.call(response) ⇒ Object
4 5 6 |
# File 'lib/duracloud/response_handler.rb', line 4 def self.call(response) new(response).call end |
Instance Method Details
#call ⇒ Object
14 15 16 17 |
# File 'lib/duracloud/response_handler.rb', line 14 def call handle_error log_response end |
#error_message ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/duracloud/response_handler.rb', line 37 def if response.plain_text? && response.has_body? response.body else [ response.status, response.reason ].join(' ') end end |
#exception ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/duracloud/response_handler.rb', line 45 def exception case response.status when 400 BadRequestError when 404 NotFoundError when 409 ConflictError else if response.status >= 500 ServerError else Error end end end |
#handle_error ⇒ Object
31 32 33 34 35 |
# File 'lib/duracloud/response_handler.rb', line 31 def handle_error if response.error? raise exception, end end |
#log_response ⇒ Object
19 20 21 22 23 |
# File 'lib/duracloud/response_handler.rb', line 19 def log_response if loggable_response_body? Duracloud.logger.info(response.body) end end |
#loggable_response_body? ⇒ Boolean
25 26 27 28 29 |
# File 'lib/duracloud/response_handler.rb', line 25 def loggable_response_body? %w(POST PUT DELETE).include?(response.request_method) && response.plain_text? && response.has_body? end |