Class: Lanyon::Router
- Inherits:
-
Object
- Object
- Lanyon::Router
- Includes:
- Backports::Router::UnescapePath
- Defined in:
- lib/lanyon/router.rb
Overview
Router class for Lanyon applications.
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#custom_404_body ⇒ Object
Returns the body of the custom 404 page or
nil
if none exists. -
#endpoint(path) ⇒ Object
Returns the full file system path of the file corresponding to the given URL
path
, or. -
#initialize(root) ⇒ Router
constructor
Creates a Router for the given root directory.
Constructor Details
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
11 12 13 |
# File 'lib/lanyon/router.rb', line 11 def root @root end |
Instance Method Details
#custom_404_body ⇒ Object
Returns the body of the custom 404 page or nil
if none exists.
48 49 50 51 52 |
# 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) ⇒ Object
Returns the full file system path of the file corresponding to the given URL path
, or
-
:must_redirect
if the request must be redirected topath/
, -
:not_found
if no corresponding file exists.
The return value is found as follows:
-
a
path/
with a trailing slash is changed topath/index.html
, -
then, the method checks for an exactly corresponding file,
-
when
path
does not exist butpath/index.html
does, a redirect will be indicated, -
finally, when no exactly corresponding file or redirect can be found,
path.html
is tried.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |