Class: GraphdocRuby::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/graphdoc-ruby/static.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, headers: {}) ⇒ Static

Returns a new instance of Static.



8
9
10
11
# File 'lib/graphdoc-ruby/static.rb', line 8

def initialize(root, headers: {})
  @root = root.chomp('/')
  @file_server = ::Rack::File.new(@root, headers)
end

Instance Method Details

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
# File 'lib/graphdoc-ruby/static.rb', line 33

def serve(request)
  @file_server.call(request.env)
end