Class: OpenapiFirst::Router

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Router

Returns a new instance of Router.



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

def initialize(
  app,
  options
)
  @app = app
  @raise = options.fetch(:raise_error, false)
  @not_found = options.fetch(:not_found, :halt)
  spec = options.fetch(:spec)
  raise "You have to pass spec: when initializing #{self.class}" unless spec

  spec = OpenapiFirst.load(spec) unless spec.is_a?(Definition)

  @filepath = spec.filepath
  @router = build_router(spec.operations)
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  env[OPERATION] = nil
  response = call_router(env)
  if env[OPERATION].nil?
    raise_error(env) if @raise

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

  response
end