Class: Landline::Handlers::Handler

Inherits:
Probe
  • Object
show all
Defined in:
lib/landline/probe/handler.rb

Overview

Probe that executes a callback on request

Direct Known Subclasses

GET

Instance Attribute Summary

Attributes inherited from Probe

#properties

Attributes inherited from Node

#remap, #root

Instance Method Summary collapse

Methods inherited from Node

#go, #reject

Constructor Details

#initialize(path, **args, &exec) ⇒ Handler

Returns a new instance of Handler.

Parameters:

  • path (Object)
  • args (Hash)

    hashed parameters to passthrough to Probe

  • exec (#call)


12
13
14
15
# File 'lib/landline/probe/handler.rb', line 12

def initialize(path, **args, &exec)
  super(path, **args)
  @callback = exec
end

Instance Method Details

#process(request) ⇒ Boolean

Method callback on successful request navigation. Runs block supplied with object initialization. Request’s #splat and #param are passed to block.

Callback’s returned should be one of viable responses:

  • Response object

  • An array that matches Rack return form

  • An array that matches old (Rack 2.x) return form

  • A string (returned as HTML with code 200)

  • false (bounces the request to next handler)

Parameters:

Returns:

  • (Boolean)

    true if further navigation is possible

Raises:

  • (UncaughtThrowError)

    may raise if die() is called.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/landline/probe/handler.rb', line 31

def process(request)
  origin, context = get_context(request)

  return reject(request) unless request.path.match?(/^\/?$/)

  response = catch(:break) do
    context.instance_exec(*request.splat,
                          **request.param,
                          &@callback)
  end
  return false unless response

  oresponse = origin.response
  if oresponse and [String, File, IO].include? response.class
    oresponse.body = response
    throw :finish, oresponse
  end
  throw :finish, response
end