Class: Otto::ResponseHandlers::JSONHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/otto/response_handlers.rb

Overview

Handler for JSON responses

Class Method Summary collapse

Class Method Details

.handle(result, response, context = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/otto/response_handlers.rb', line 20

def self.handle(result, response, context = {})
  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