Class: Otto::ResponseHandlers::AutoHandler

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

Overview

Auto-detection handler that chooses appropriate handler based on context

Class Method Summary collapse

Methods inherited from BaseHandler

ensure_status_set

Class Method Details

.detect_handler_type(result, response, context) ⇒ Object



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

def self.detect_handler_type(result, response, context)
  # Check if response type was already set by the handler
  content_type = response['Content-Type']

  if content_type&.include?('application/json')
    JSONHandler
  elsif (context[:logic_instance]&.respond_to?(:redirect_path) && context[:logic_instance].redirect_path) ||
        (result.is_a?(String) && result.match?(%r{^/}))
    # Logic instance has redirect path or result is a string path
    RedirectHandler
  elsif result.is_a?(Hash)
    JSONHandler
  elsif context[:logic_instance]&.respond_to?(:view)
    ViewHandler
  else
    DefaultHandler
  end
end

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



15
16
17
18
19
# File 'lib/otto/response_handlers/auto.rb', line 15

def self.handle(result, response, context = {})
  # Auto-detect based on result type and request context
  handler_class = detect_handler_type(result, response, context)
  handler_class.handle(result, response, context)
end