Sha256: 19935a9f4421eee7ded697bb41d6f75ba18e45b5ae963eca00f3441e21545956

Contents?: true

Size: 855 Bytes

Versions: 2

Compression:

Stored size: 855 Bytes

Contents

module Swagui
  class SwaggerDocHandler
    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

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

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
swagui-0.5.4 lib/swagui/swagger_doc_handler.rb
swagui-0.5.3 lib/swagui/swagger_doc_handler.rb