class GraphdocRuby::Static

Public Class Methods

new(root, headers: {}) click to toggle source
# File lib/graphdoc-ruby/static.rb, line 8
def initialize(root, headers: {})
  @root = root.chomp('/')
  @file_server = ::Rack::File.new(@root, headers)
end

Public Instance Methods

match?(path) click to toggle source
# File lib/graphdoc-ruby/static.rb, line 13
def match?(path)
  path = ::Rack::Utils.unescape_path(path)
  return false unless ::Rack::Utils.valid_path? path
  path = ::Rack::Utils.clean_path_info(path)

  candidate_paths = [path, "#{path}.html", "#{path}/index.html"]

  match = candidate_paths.detect do |candidate_path|
    absolute_path = File.join(@root, candidate_path.dup.force_encoding(Encoding::UTF_8))

    begin
      File.file?(absolute_path) && File.readable?(absolute_path)
    rescue SystemCallError
      false
    end
  end

  ::Rack::Utils.escape_path(match) if match
end
serve(request) click to toggle source
# File lib/graphdoc-ruby/static.rb, line 33
def serve(request)
  @file_server.call(request.env)
end