Class: Apicraft::Middlewares::Introspector

Inherits:
Object
  • Object
show all
Includes:
Concerns::MiddlewareUtil
Defined in:
lib/apicraft/middlewares/introspector.rb

Overview

Apicraft Middleware to handle API Introspection.

Instance Method Summary collapse

Methods included from Concerns::MiddlewareUtil

#config, #convertor

Constructor Details

#initialize(app) ⇒ Introspector

Returns a new instance of Introspector.



9
10
11
# File 'lib/apicraft/middlewares/introspector.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/apicraft/middlewares/introspector.rb', line 13

def call(env)
  return @app.call(env) unless config.introspection

  request = ActionDispatch::Request.new(env)
  return @app.call(env) unless introspect?(request)

  schema = Apicraft::Openapi::Contract.find_by_operation(
    request.method, request.path_info
  )&.operation(
    request.method, request.path_info
  )&.raw_schema
  return @app.call(env) if schema.blank?

  [
    200,
    { 'Content-Type': "application/json" },
    [schema.to_json]
  ]
end