class Rack::Bunto::FileHandler

Attributes

files[R]
root[R]

Public Class Methods

new(root, files = nil) click to toggle source

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

# File lib/rack/bunto/filehandler.rb, line 9
def initialize(root, files = nil)
  @root = ::File.expand_path(root)
  @files = files || get_file_list
end

Public Instance Methods

empty?() click to toggle source
# File lib/rack/bunto/filehandler.rb, line 14
def empty?
  @files.empty?
end
get_filename(path) click to toggle source

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

# 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
update() click to toggle source
# File lib/rack/bunto/filehandler.rb, line 18
def update
  @files = get_file_list
end

Private Instance Methods

get_file_list() click to toggle source

Retrieves and returns a list of all files in the root directory (excluding directory names).

# File lib/rack/bunto/filehandler.rb, line 48
def get_file_list
  files = ::Dir[@root + "/**/*"]
  files.delete_if {|file| ::FileTest.directory?(file) }

  files
end