Class: OpenapiFirst::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/router.rb

Constant Summary collapse

ORIGINAL_PATH =
'openapi_first.path_info'

Instance Method Summary collapse

Constructor Details

#initialize(app, spec:, raise_error: false, not_found: :halt, parent_app: nil) ⇒ Router

Returns a new instance of Router.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openapi_first/router.rb', line 8

def initialize(
  app,
  spec:,
  raise_error: false,
  not_found: :halt,
  parent_app: nil
)
  @app = app
  @parent_app = parent_app
  @raise = raise_error
  @not_found = not_found
  @filepath = spec.filepath
  @router = build_router(spec.operations)
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openapi_first/router.rb', line 23

def call(env)
  env[OPERATION] = nil
  response = call_router(env)
  if env[OPERATION].nil?
    return @parent_app.call(env) if @parent_app # This should only happen if used via OpenapiFirst.middleware

    raise_error(env) if @raise

    return @app.call(env) if @not_found == :continue
  end
  response
end