Class: Apia::OpenApi::Rack
- Inherits:
-
Object
- Object
- Apia::OpenApi::Rack
- Defined in:
- lib/apia/open_api/rack.rb
Instance Method Summary collapse
- #api_class ⇒ Object
- #base_url ⇒ Object
- #call(env) ⇒ Object
- #development? ⇒ Boolean
- #external_docs ⇒ Object
- #info ⇒ Object
-
#initialize(app, api_class:, schema_path:, **options) ⇒ Rack
constructor
A new instance of Rack.
- #security_schemes ⇒ Object
Constructor Details
#initialize(app, api_class:, schema_path:, **options) ⇒ Rack
Returns a new instance of Rack.
7 8 9 10 11 12 |
# File 'lib/apia/open_api/rack.rb', line 7 def initialize(app, api_class:, schema_path:, **) @app = app @api_class = api_class @schema_path = "/#{schema_path.sub(/\A\/+/, '').sub(/\/+\z/, '')}" @options = end |
Instance Method Details
#api_class ⇒ Object
21 22 23 24 25 26 |
# File 'lib/apia/open_api/rack.rb', line 21 def api_class return Object.const_get(@api_class) if @api_class.is_a?(String) && development? return @cached_api ||= Object.const_get(@api_class) if @api_class.is_a?(String) @api_class end |
#base_url ⇒ Object
28 29 30 |
# File 'lib/apia/open_api/rack.rb', line 28 def base_url @options[:base_url] || "https://api.example.com/api/v1" end |
#call(env) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/apia/open_api/rack.rb', line 44 def call(env) if @options[:hosts]&.none? { |host| host == env["HTTP_HOST"] } return @app.call(env) end unless env["PATH_INFO"] == @schema_path return @app.call(env) end specification = Specification.new(api_class, base_url, @options[:name], { info: info, external_docs: external_docs, security_schemes: security_schemes }) body = specification.json [200, { "content-type" => "application/json", "content-length" => body.bytesize.to_s }, [body]] end |
#development? ⇒ Boolean
14 15 16 17 18 19 |
# File 'lib/apia/open_api/rack.rb', line 14 def development? env_is_dev = ENV["RACK_ENV"] == "development" return true if env_is_dev && @options[:development].nil? @options[:development] == true end |
#external_docs ⇒ Object
36 37 38 |
# File 'lib/apia/open_api/rack.rb', line 36 def external_docs @options[:external_docs] || {} end |
#info ⇒ Object
40 41 42 |
# File 'lib/apia/open_api/rack.rb', line 40 def info @options[:info] || {} end |
#security_schemes ⇒ Object
32 33 34 |
# File 'lib/apia/open_api/rack.rb', line 32 def security_schemes @options[:security_schemes] || {} end |