class SoberSwag::Server
A basic, rack-only server to serve up swagger definitions. By default it is configured to work with rails, but you can pass stuff to initialize.
Constants
- EFFECT_HTML
- RAILS_CONTROLLER_PROC
Public Class Methods
new( controller_proc: RAILS_CONTROLLER_PROC, cache: false, redoc_version: 'next' )
click to toggle source
Start up.
@param controller_proc [Proc] a proc that, when called, gives a list of {SoberSwag::Controller}s to document @param cache [Bool | Proc] if we should cache our definitions (default false) @param redoc_version [String] what version of the redoc library to use to display UI (default 'next', the latest version).
# File lib/sober_swag/server.rb, line 26 def initialize( controller_proc: RAILS_CONTROLLER_PROC, cache: false, redoc_version: 'next' ) @controller_proc = controller_proc @cache = cache @html = EFFECT_HTML.gsub(/REDOC_VERSION/, redoc_version) end
Public Instance Methods
call(env)
click to toggle source
Standard Rack call method.
# File lib/sober_swag/server.rb, line 65 def call(env) req = Rack::Request.new(env) if req.path_info&.match?(/json/si) || req.get_header('Accept')&.match?(/json/si) [200, { 'Content-Type' => 'application/json' }, [generate_json_string]] else [200, { 'Content-Type' => 'text/html' }, [@html.gsub(/SCRIPT_NAME/, "#{env['SCRIPT_NAME']}.json")]] end end
Private Instance Methods
cache?()
click to toggle source
# File lib/sober_swag/server.rb, line 84 def cache? @cache.respond_to?(:call) ? @cache.call : @cache end
generate_json_string()
click to toggle source
# File lib/sober_swag/server.rb, line 76 def generate_json_string if cache? @json_string ||= JSON.dump(generate_swagger) else JSON.dump(generate_swagger) end end
generate_swagger()
click to toggle source
# File lib/sober_swag/server.rb, line 88 def generate_swagger routes = sober_controllers.flat_map(&:defined_routes).reduce(SoberSwag::Compiler.new) { |c, r| c.add_route(r) } { openapi: '3.0.0', info: { version: '1', title: 'SoberSwag Swagger' } }.merge(routes.to_swagger) end
sober_controllers()
click to toggle source
# File lib/sober_swag/server.rb, line 99 def sober_controllers @controller_proc.call end