Class: Otto::RouteHandlers::BaseHandler

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

Overview

Base class for all route handlers Provides common functionality and interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route_definition, otto_instance = nil) ⇒ BaseHandler

Returns a new instance of BaseHandler.



15
16
17
18
# File 'lib/otto/route_handlers/base.rb', line 15

def initialize(route_definition, otto_instance = nil)
  @route_definition = route_definition
  @otto_instance    = otto_instance
end

Instance Attribute Details

#otto_instanceObject (readonly)

Returns the value of attribute otto_instance.



13
14
15
# File 'lib/otto/route_handlers/base.rb', line 13

def otto_instance
  @otto_instance
end

#route_definitionObject (readonly)

Returns the value of attribute route_definition.



13
14
15
# File 'lib/otto/route_handlers/base.rb', line 13

def route_definition
  @route_definition
end

Instance Method Details

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

Execute the route handler

Parameters:

  • env (Hash)

    Rack environment

  • extra_params (Hash) (defaults to: {})

    Additional parameters

Returns:

  • (Array)

    Rack response array



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/otto/route_handlers/base.rb', line 24

def call(env, extra_params = {})
  @start_time = Otto::Utils.now_in_μs
  req = otto_instance ? otto_instance.request_class.new(env) : Otto::Request.new(env)
  res = otto_instance ? otto_instance.response_class.new : Otto::Response.new

  begin
    setup_request_response(req, res, env, extra_params)
    result, context = invoke_target(req, res)

    handle_response(result, res, context) if route_definition.response_type != 'default'
  rescue StandardError => e
    handle_execution_error(e, env, req, res, @start_time)
  end

  finalize_response(res)
end