Class: Rack::Spec::Docs

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/spec/docs.rb

Constant Summary collapse

DEFAULT_PATH =
"/docs"

Instance Method Summary collapse

Constructor Details

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

Behaves as a rack-middleware

Parameters:

  • app (Object)

    Rack application

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

    URL path to return document (default: /docs)

  • schema (Hash) (defaults to: nil)

    Schema object written in JSON schema format



10
11
12
13
14
# File 'lib/rack/spec/docs.rb', line 10

def initialize(app, path: nil, schema: nil)
  @app = app
  @path = path
  @document = Jdoc::Generator.call(schema)
end

Instance Method Details

#call(env) ⇒ Object

Returns rendered document for document request

Parameters:

  • env (Hash)

    Rack env



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/spec/docs.rb', line 18

def call(env)
  if env["REQUEST_METHOD"] == "GET" && env["PATH_INFO"] == path
    [
      200,
      { "Content-Type" => "text/plain; charset=utf-8" },
      [@document],
    ]
  else
    @app.call(env)
  end
end