Class: Kawaii::RouteHandler

Inherits:
Object
  • Object
show all
Includes:
MethodChain, RenderMethods
Defined in:
lib/kawaii/route_handler.rb

Overview

Creates context for execution of route handler block provided by the user with #params and other objects.

Examples:

Route handler block

get '/users/:id' do
  if params[:id] ...
end

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary (collapse)

Attributes included from MethodChain

#parent_scope

Instance Method Summary (collapse)

Methods included from RenderMethods

#render

Methods included from MethodChain

#method_missing, #respond_to?

Constructor Details

- (RouteHandler) initialize(scope, path_params, &block)

Creates a new RouteHandler wrapping a handler block.

Parameters:

  • path_params (Hash)

    named parameters from paths similar to /users/:id

  • block (Proc)

    the actual route handler



22
23
24
25
26
# File 'lib/kawaii/route_handler.rb', line 22

def initialize(scope, path_params, &block)
  self.parent_scope = scope
  @path_params = path_params
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kawaii::MethodChain

Instance Attribute Details

- (Object) params (readonly)

Params based on request visible in the route handler scope.



14
15
16
# File 'lib/kawaii/route_handler.rb', line 14

def params
  @params
end

- (Object) request (readonly)

Rack::Request object visible in the route handler scope



16
17
18
# File 'lib/kawaii/route_handler.rb', line 16

def request
  @request
end

Instance Method Details

- (Array) call(env)

Invokes the handler as a normal Rack application.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response array



31
32
33
34
35
# File 'lib/kawaii/route_handler.rb', line 31

def call(env)
  @request = Rack::Request.new(env)
  @params = @path_params.merge(@request.params.symbolize_keys)
  process_response(instance_exec(self, params, request, &@block))
end