Module: Spikard::LifecycleHooks

Included in:
App
Defined in:
lib/spikard/app.rb

Overview

Lifecycle hooks support for Spikard applications

Instance Method Summary collapse

Instance Method Details

#on_error(&hook) ⇒ Proc

Register an onError lifecycle hook

Runs when an error occurs. Can customize error responses.

Examples:

app.on_error do |response|
  response.headers['Content-Type'] = 'application/json'
  response
end

Parameters:

  • hook (Proc)

    A proc that receives an error response and returns a (possibly modified) response

Returns:

  • (Proc)

    The hook proc (for chaining)



100
101
102
103
# File 'lib/spikard/app.rb', line 100

def on_error(&hook)
  native_hooks.add_on_error(hook)
  hook
end

#on_request(&hook) ⇒ Proc

Register an onRequest lifecycle hook

Runs before routing. Can inspect/modify the request or short-circuit with a response.

Examples:

app.on_request do |request|
  puts "Request: #{request.method} #{request.path}"
  request
end

Parameters:

  • hook (Proc)

    A proc that receives a request and returns either:

    • The (possibly modified) request to continue processing

    • A Response object to short-circuit the request pipeline

Returns:

  • (Proc)

    The hook proc (for chaining)



22
23
24
25
# File 'lib/spikard/app.rb', line 22

def on_request(&hook)
  native_hooks.add_on_request(hook)
  hook
end

#on_response(&hook) ⇒ Proc

Register an onResponse lifecycle hook

Runs after the handler executes. Can modify the response.

Examples:

app.on_response do |response|
  response.headers['X-Frame-Options'] = 'DENY'
  response
end

Parameters:

  • hook (Proc)

    A proc that receives a response and returns the (possibly modified) response

Returns:

  • (Proc)

    The hook proc (for chaining)



83
84
85
86
# File 'lib/spikard/app.rb', line 83

def on_response(&hook)
  native_hooks.add_on_response(hook)
  hook
end

#pre_handler(&hook) ⇒ Proc

Register a preHandler lifecycle hook

Runs after validation but before the handler. Ideal for authentication/authorization.

Examples:

app.pre_handler do |request|
  if invalid_token?(request.headers['Authorization'])
    Spikard::Response.new(content: { error: "Unauthorized" }, status_code: 401)
  else
    request
  end
end

Parameters:

  • hook (Proc)

    A proc that receives a request and returns either:

    • The (possibly modified) request to continue processing

    • A Response object to short-circuit the request pipeline

Returns:

  • (Proc)

    The hook proc (for chaining)



66
67
68
69
# File 'lib/spikard/app.rb', line 66

def pre_handler(&hook)
  native_hooks.add_pre_handler(hook)
  hook
end

#pre_validation(&hook) ⇒ Proc

Register a preValidation lifecycle hook

Runs after routing but before validation. Useful for rate limiting.

Examples:

app.pre_validation do |request|
  if too_many_requests?
    Spikard::Response.new(content: { error: "Rate limit exceeded" }, status_code: 429)
  else
    request
  end
end

Parameters:

  • hook (Proc)

    A proc that receives a request and returns either:

    • The (possibly modified) request to continue processing

    • A Response object to short-circuit the request pipeline

Returns:

  • (Proc)

    The hook proc (for chaining)



44
45
46
47
# File 'lib/spikard/app.rb', line 44

def pre_validation(&hook)
  native_hooks.add_pre_validation(hook)
  hook
end