Class: Swagui::SwaggerDocHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/swagui/swagger_doc_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, url) ⇒ SwaggerDocHandler

Returns a new instance of SwaggerDocHandler.



3
4
5
6
7
8
9
10
# 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

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/swagui/swagger_doc_handler.rb', line 12

def handles?(env)
  @url_regex === env["PATH_INFO"]
end