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, options) ⇒ Router

Returns a new instance of Router.



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

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

Instance Method Details

#call(env) ⇒ Object



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

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