class

Pika::API

Inherits Reference < Object

Class methods

openapi
Source
openapi_doc
Source
openapi_routes
Source
present(obj, using entity_class, **opts) : String

present — serialize an object or collection through an Entity class. Call from inside a handler block: present user, using: UserEntity

Source
router
Source
run(host : String = "0.0.0.0", port : Int32 = 3000, reuse_port : Bool = false)
Source

Macros

after
Source
before

before / after hooks — run around every route handler in this class

before do raise Pika::UnauthorizedError.new unless env.request.headers["X-Token"]? end

Source
docs(at = "/docs")

docs — mount Scalar UI and OpenAPI JSON endpoint

docs at: "/docs" # GET /docs → Scalar HTML # GET /docs/openapi.json → OpenAPI 3.1

Source
error_formatter(fmt)

error_formatter — select the active error serialization format

error_formatter :rfc7807 # default — application/problem+json error_formatter :grape # {"error": "message"} error_formatter :jsonapi # {"errors": [...]}

Source
helpers

helpers — define class-level helper methods accessible inside handlers

helpers do def self.current_user(env) env.request.headers["X-User-Id"]? end end

Source
info

info — set API metadata used in the OpenAPI document

info title: "My API", version: "2.0.0", description: "..."

Source
mount(api_class)

mount — compose another Pika::API's routes into this one

mount AdminAPI # copies /admin/... routes verbatim

The current version + namespace prefix is applied to all mounted paths.

Source
namespace(name, &block)

namespace — groups routes under a common path segment

namespace :admin do resource :users do ... end end

Source
resource(name, &block)

resource macro

The core DSL macro. Parses its block body as an AST to accumulate desc / params / verb triples, then generates:

  1. A typed DeclaredParams struct per route.
  2. A validate! class method with full constraint enforcement.
  3. A Pika::Router#add registration (path computed at class-init time).
  4. A Pika::OpenAPI::RouteSpec entry.

Supports nested route_param blocks one level deep:

resource :users do get do ... end # GET /users post do ... end # POST /users

route_param :id do
  get do ... end         # GET /users/:id
  patch do ... end       # PATCH /users/:id
  delete do ... end      # DELETE /users/:id
end

end

Params blocks support mutually_exclusive / at_least_one_of / exactly_one_of:

params do optional email : String? optional phone : String? mutually_exclusive :email, :phone end

Source
version(v, **options)

version — sets a path prefix for the entire API

version "v1", using: :path

Source