class Lanyon::Router

Router class for Lanyon applications.

Attributes

root[R]

Public Class Methods

new(root) click to toggle source

Creates a Router for the given root directory.

# File lib/lanyon/router.rb, line 14
def initialize(root)
  @root = File.expand_path(root)
end

Public Instance Methods

custom_404_body() click to toggle source

Returns the body of the custom 404 page or nil if none exists.

# File lib/lanyon/router.rb, line 48
def custom_404_body
  filename = File.join(root, "404.html")

  File.exist?(filename) ? File.binread(filename) : nil
end
endpoint(path) click to toggle source

Returns the full file system path of the file corresponding to the given URL path, or

  • :must_redirect if the request must be redirected to path/,

  • :not_found if no corresponding file exists.

The return value is found as follows:

  1. a path/ with a trailing slash is changed to path/index.html,

  2. then, the method checks for an exactly corresponding file,

  3. when path does not exist but path/index.html does, a redirect will be indicated,

  4. finally, when no exactly corresponding file or redirect can be found, path.html is tried.

# File lib/lanyon/router.rb, line 32
def endpoint(path)
  normalized = normalize_path_info(path)
  fullpath = File.join(@root, normalized)

  if FileTest.file?(fullpath)
    fullpath
  elsif needs_redirect_to_dir?(fullpath)
    :must_redirect
  elsif FileTest.file?(fullpath_html = "#{fullpath}.html")
    fullpath_html
  else
    :not_found
  end
end