Class: Hyde::Handlers::Handler

Inherits:
Probe show all
Defined in:
lib/hyde/probe/handler.rb

Overview

Probe that executes a callback on request

Direct Known Subclasses

GET

Instance Attribute Summary collapse

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, parent:, &exec) ⇒ Handler

Returns a new instance of Handler.

Parameters:



12
13
14
15
16
17
# File 'lib/hyde/probe/handler.rb', line 12

def initialize(path, parent:, &exec)
  super(path, parent: parent)
  @callback = exec
  @context = Hyde::ProbeContext.new(self)
  @response = nil
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



20
21
22
# File 'lib/hyde/probe/handler.rb', line 20

def request
  @request
end

#responseObject

Returns the value of attribute response.



19
20
21
# File 'lib/hyde/probe/handler.rb', line 19

def response
  @response
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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hyde/probe/handler.rb', line 36

def process(request)
  @response = nil
  return reject(request) unless request.path.match?(/^\/?$/)
 
  @request = request
  response = catch(:break) do
    @context.instance_exec(*request.splat,
                           **request.param,
                           &@callback)
  end
  return false unless response
 
  if @response and [String, File, IO].include? response.class
    @response.body = response
    throw :finish, @response
  end
  throw :finish, response
end