Module: Rage::OpenAPI

Defined in:
lib/rage/openapi/openapi.rb

Defined Under Namespace

Modules: Nodes, Parsers Classes: Builder, Collector, Converter, Parser

Class Method Summary collapse

Class Method Details

.application(namespace: nil) ⇒ Object

Create a new OpenAPI application.

Examples:

map "/publicapi" do
  run Rage.openapi.application
end
map "/publicapi/v1" do
  run Rage.openapi.application(namespace: "Api::V1")
end

map "/publicapi/v2" do
  run Rage.openapi.application(namespace: "Api::V2")
end

Parameters:

  • namespace (String, Module) (defaults to: nil)

    limit the parser to a specific namespace



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rage/openapi/openapi.rb', line 31

def self.application(namespace: nil)
  html_app = ->(env) do
    __data_cache[[:page, namespace]] ||= begin
      scheme, host, path = env["rack.url_scheme"], env["HTTP_HOST"], env["SCRIPT_NAME"]
      spec_url = "#{scheme}://#{host}#{path}/json"
      page = ERB.new(File.read("#{__dir__}/index.html.erb")).result(binding)

      [200, { "Content-Type" => "text/html; charset=UTF-8" }, [page]]
    end
  end

  json_app = ->(env) do
    spec = (__data_cache[[:spec, namespace]] ||= build(namespace:).to_json)
    [200, { "Content-Type" => "application/json" }, [spec]]
  end

  app = ->(env) do
    if env["PATH_INFO"] == ""
      html_app.call(env)
    elsif env["PATH_INFO"] == "/json"
      json_app.call(env)
    else
      [404, {}, ["Not Found"]]
    end
  end

  if Rage.config.middleware.include?(Rage::Reloader)
    Rage.with_middlewares(app, [Rage::Reloader])
  elsif defined?(ActionDispatch::Reloader) && Rage.config.middleware.include?(ActionDispatch::Reloader)
    Rage.with_middlewares(app, [ActionDispatch::Reloader])
  else
    app
  end
end

.build(namespace: nil) ⇒ Hash

Build an OpenAPI specification for the application.

Parameters:

  • namespace (String, Module) (defaults to: nil)

    limit the parser to a specific namespace

Returns:

  • (Hash)


69
70
71
# File 'lib/rage/openapi/openapi.rb', line 69

def self.build(namespace: nil)
  Builder.new(namespace:).run
end