Class: Otto::RouteHandlers::LogicClassHandler

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

Overview

Handler for Logic classes (new in Otto Framework Enhancement) Supports the OneTime Secret Logic class pattern

Instance Attribute Summary

Attributes inherited from BaseHandler

#otto_instance, #route_definition

Instance Method Summary collapse

Methods inherited from BaseHandler

#initialize

Constructor Details

This class inherits a constructor from Otto::RouteHandlers::BaseHandler

Instance Method Details

#call(env, extra_params = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/otto/route_handlers.rb', line 144

def call(env, extra_params = {})
  req = Rack::Request.new(env)
  res = Rack::Response.new

  begin
    # Get authentication context if available
    auth_result = env['otto.auth_result']

    # Initialize Logic class with standard parameters
    # Logic classes expect: session, user, params, locale
    logic_params = req.params.merge(extra_params)
    locale = env['otto.locale'] || 'en'

    logic = if target_class.instance_method(:initialize).arity == 4
              # Standard Logic class constructor
              target_class.new(
                auth_result&.session,
                auth_result&.user,
                logic_params,
                locale
              )
            else
              # Fallback for custom constructors
              target_class.new(req, res)
            end

    # Execute standard Logic class lifecycle
    if logic.respond_to?(:raise_concerns)
      logic.raise_concerns
    end

    result = if logic.respond_to?(:process)
               logic.process
             else
               logic.call || logic
             end

    # Handle response with Logic instance context
    handle_response(result, res, {
      logic_instance: logic,
      request: req,
      status_code: logic.respond_to?(:status_code) ? logic.status_code : nil
    })

  rescue => e
    # Error handling - return 500 with proper headers like main Otto error handler
    error_id = SecureRandom.hex(8)
    Otto.logger.error "[#{error_id}] #{e.class}: #{e.message}"
    Otto.logger.debug "[#{error_id}] Backtrace: #{e.backtrace.join("\n")}" if Otto.debug

    res.status = 500
    res.headers['content-type'] = 'text/plain'

    if Otto.env?(:dev, :development)
      res.write "Server error (ID: #{error_id}). Check logs for details."
    else
      res.write "An error occurred. Please try again later."
    end

    # Add security headers if available
    if otto_instance&.respond_to?(:security_config) && otto_instance.security_config
      otto_instance.security_config.security_headers.each do |header, value|
        res.headers[header] = value
      end
    end
  end

  res.finish
end