Class: Otto::ResponseHandlers::JSONHandler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- Otto::ResponseHandlers::JSONHandler
- Defined in:
- lib/otto/response_handlers/json.rb
Overview
Handler for JSON responses
Class Method Summary collapse
Methods inherited from BaseHandler
Class Method Details
.handle(result, response, context = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/otto/response_handlers/json.rb', line 11 def self.handle(result, response, context = {}) # If a redirect has already been set, don't override with JSON # This allows controllers to conditionally redirect based on Accept header return if response.status&.between?(300, 399) && response['Location'] response['Content-Type'] = 'application/json' # Determine the data to serialize data = if context[:logic_instance]&.respond_to?(:response_data) context[:logic_instance].response_data elsif result.is_a?(Hash) result elsif result.nil? { success: true } else { success: true, data: result } end response.body = [JSON.generate(data)] ensure_status_set(response, context[:status_code] || 200) end |