Class: Rack::JsonSchema::SchemaProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/json_schema/schema_provider.rb

Constant Summary collapse

DEFAULT_PATH =
"/schema"

Instance Method Summary collapse

Constructor Details

#initialize(app, path: nil, schema: nil) ⇒ SchemaProvider

Behaves as a rack-middleware

Parameters:

  • app (Object)

    Rack application

  • path (String, nil) (defaults to: nil)

    URL path to return JSON Schema (default: /schema)

  • schema (Hash) (defaults to: nil)

    Schema object written in JSON schema format

Raises:

  • (JsonSchema::SchemaError)

11
12
13
14
15
# File 'lib/rack/json_schema/schema_provider.rb', line 11

def initialize(app, path: nil, schema: nil)
  @app = app
  @path = path
  @schema = schema
end

Instance Method Details

#call(env) ⇒ Array

Returns rendered document for document request

Parameters:

  • env (Hash)

    Rack env

Returns:

  • (Array)

    Rack response


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/json_schema/schema_provider.rb', line 20

def call(env)
  if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"] == path
    [
      200,
      { "Content-Type" => "application/json" },
      [rendered_schema],
    ]
  else
    @app.call(env)
  end
end