class Swagui::SwaggerDocHandler

Public Class Methods

new(path, url) click to toggle source
# File lib/swagui/swagger_doc_handler.rb, line 3
def initialize(path, url)
  @url_regex = Regexp.new("^#{url}")
  app_doc_dir = Swagui.file_full_path(path || url)

  raise "swagger api doc directory #{app_doc_dir} does not exist" unless File.directory?(app_doc_dir)

  @app_file_server = YAMLDocHandler.new(Rack::File.new(app_doc_dir))
end

Public Instance Methods

call(env) click to toggle source
# File lib/swagui/swagger_doc_handler.rb, line 16
def call(env)
  path = env["PATH_INFO"].gsub(@url_regex, '') # root path renders index.html

  first_valid_file_response = ['', '.json', '.yml'].map do |ext|
    @app_file_server.call(env.merge('PATH_INFO' => "#{path}#{ext}", 'REQUEST_METHOD' => 'GET'))
  end.find {|res| (res[0] == 200 || res[0] == 304) }

  first_valid_file_response || [404, {"Content-Type"=>"application/json"}, '']
end
handles?(env) click to toggle source
# File lib/swagui/swagger_doc_handler.rb, line 12
def handles?(env)
  @url_regex === env["PATH_INFO"]
end