Class: Wurk::API::App

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/api/app.rb

Overview

The machine-facing HTTP API. Plain Rack under lib/, with no reference to Rails or the engine, so one implementation serves all three mounts: engine-nested (<mount>/api/v1), separately mounted (mount Wurk::API => '/wurk-api'), and standalone (run Wurk::API).

Nothing here hardcodes '/wurk'. Every URL the API emits is built from SCRIPT_NAME (Request#url_for) — the same mount-agnostic rule the SPA shell follows in dashboard_controller.rb.

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ App

config is read on every request rather than captured, so a token registered after the first request still takes effect. Injectable because a test must be able to hand this app its own token table instead of mutating the process-wide one.



34
35
36
37
38
# File 'lib/wurk/api/app.rb', line 34

def initialize(config: nil)
  @config = config
  @router = Router.new
  draw(@router)
end

Instance Method Details

#call(env) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/wurk/api/app.rb', line 44

def call(env)
  request = Request.new(env)
  # Stamped once, before anything reads it, so every handler sees the
  # same config the auth gate did — including a test's injected one.
  request.config = config
  response = handle(request)
  # HEAD is routed as GET, so the handler built a body it must not send.
  request.head? ? [response[0], response[1], []] : response
end

#routes ⇒ Object

Every route this app answers, for test/unit/api_openapi_parity_test.rb to hold docs/api/openapi.yaml to.



42
# File 'lib/wurk/api/app.rb', line 42

def routes = @router.routes