Class: Rack::Bunto::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bunto/filehandler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, files = nil) ⇒ FileHandler

Initializes a FileHandler for a given root directory (for testing only: use a given array of filenames, files).



9
10
11
12
# File 'lib/rack/bunto/filehandler.rb', line 9

def initialize(root, files = nil)
  @root = ::File.expand_path(root)
  @files = files || get_file_list
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'lib/rack/bunto/filehandler.rb', line 5

def files
  @files
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/rack/bunto/filehandler.rb', line 5

def root
  @root
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/rack/bunto/filehandler.rb', line 14

def empty?
  @files.empty?
end

#get_filename(path) ⇒ Object

Returns the full file system path of the file corresponding to the given URL path, or nil if no corresponding file exists.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/bunto/filehandler.rb', line 24

def get_filename(path)
  fullpath = ::File.join(@root, path)

  if fullpath.end_with?("/")
    normalized = fullpath + "index.html"
  elsif !@files.include?(fullpath)
    normalized = fullpath + "/index.html"
  else
    normalized = fullpath
  end

  if @files.include?(normalized)
    filename = normalized
  else
    filename = nil
  end

  filename
end

#updateObject



18
19
20
# File 'lib/rack/bunto/filehandler.rb', line 18

def update
  @files = get_file_list
end