Module: Spikard::HandlerWrapper

Included in:
Spikard
Defined in:
lib/spikard/handler_wrapper.rb

Overview

Handler wrapper utilities.

UploadFile conversion now happens in the Rust binding, so these wrappers simply forward the already-converted body/params.

Examples:

Basic usage with body only

app.post('/upload', &wrap_body_handler do |body|
  {
    filename: body[:file].filename,
    content: body[:file].read
  }
end)

With all parameters

app.post('/upload', &wrap_handler do |params, query, body|
  {
    id: params[:id],
    search: query[:q],
    file: body[:file].filename
  }
end)

Class Method Summary collapse

Class Method Details

.wrap_body_handler {|body| ... } ⇒ Proc

Wrap a handler that receives only the request body

Automatically converts file metadata in the body to UploadFile instances.

Examples:

app.post('/upload', &wrap_body_handler do |body|
  { filename: body[:file].filename }
end)

Yields:

  • (body)

    Handler block that receives converted body

Yield Parameters:

  • body (Hash)

    Request body with file metadata converted to UploadFile

Yield Returns:

Returns:

  • (Proc)

    Wrapped handler proc

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
# File 'lib/spikard/handler_wrapper.rb', line 43

def wrap_body_handler(&handler)
  raise ArgumentError, 'block required for wrap_body_handler' unless handler

  # Return a proc that matches the signature expected by Spikard::App
  # The actual handler receives path params, query params, and body from Rust
  lambda do |_params, _query, body|
    handler.call(body)
  end
end

.wrap_handler {|params, query, body| ... } ⇒ Proc

Wrap a handler that receives path params, query params, and body

Automatically converts file metadata in the body to UploadFile instances.

Examples:

app.post('/users/{id}/upload', &wrap_handler do |params, query, body|
  {
    user_id: params[:id],
    description: query[:desc],
    file: body[:file].filename
  }
end)

Yields:

  • (params, query, body)

    Handler block that receives all request data

Yield Parameters:

  • params (Hash)

    Path parameters

  • query (Hash)

    Query parameters

  • body (Hash)

    Request body with file metadata converted to UploadFile

Yield Returns:

Returns:

  • (Proc)

    Wrapped handler proc

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
# File 'lib/spikard/handler_wrapper.rb', line 72

def wrap_handler(&handler)
  raise ArgumentError, 'block required for wrap_handler' unless handler

  lambda do |params, query, body|
    handler.call(params, query, body)
  end
end

.wrap_handler_with_context {|context| ... } ⇒ Proc

Wrap a handler that receives a context hash with all request data

Automatically converts file metadata in the body to UploadFile instances. Useful when you want all request data in a single hash.

Examples:

app.post('/upload', &wrap_handler_with_context do |ctx|
  {
    file: ctx[:body][:file].filename,
    query_params: ctx[:query]
  }
end)

Yields:

  • (context)

    Handler block that receives context hash

Yield Parameters:

  • context (Hash)

    Request context with:

    • :params [Hash] Path parameters

    • :query [Hash] Query parameters

    • :body [Hash] Request body with file metadata converted to UploadFile

Yield Returns:

Returns:

  • (Proc)

    Wrapped handler proc

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spikard/handler_wrapper.rb', line 100

def wrap_handler_with_context(&handler)
  raise ArgumentError, 'block required for wrap_handler_with_context' unless handler

  lambda do |params, query, body|
    context = {
      params: params,
      query: query,
      body: body
    }
    handler.call(context)
  end
end