Class: OpenapiRuby::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/rack_app.rb

Overview

Serves the generated schema documents and the Swagger UI as a plain Rack app, for hosts with no engine to mount:

# Hanami — config/routes.rb
mount OpenapiRuby::RackApp, at: "/api-docs"

Routes mirror the Rails engine's (config/routes.rb) so both hosts expose the same paths. Schema URLs are derived from SCRIPT_NAME, so the app works at any mount point.

Deliberately not namespaced under OpenapiRuby::Rack: that constant would shadow the top-level ::Rack for every file in this gem.

Constant Summary collapse

SCHEMA_PATH =
%r{\A/schemas/(?<name>.+)\z}
SCHEMA_EXTENSION =
/\.(json|ya?ml)\z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(env) ⇒ Object



22
23
24
# File 'lib/openapi_ruby/rack_app.rb', line 22

def self.call(env)
  (@app ||= new).call(env)
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openapi_ruby/rack_app.rb', line 26

def call(env)
  request = ::Rack::Request.new(env)
  return method_not_allowed unless request.get? || request.head?

  status, headers, body = case request.path_info
  when "", "/" then ui(request)
  when "/schemas" then schema_index
  when "/oauth2-redirect.html" then oauth2_redirect
  when SCHEMA_PATH then schema(::Regexp.last_match(:name), request)
  else not_found
  end

  # Drop the body ourselves: Rails runs Rack::Head above the engine, a bare
  # Hanami mount has nothing between the router and here.
  [status, headers, request.head? ? [] : body]
end