Class: Lanyon::Router

Inherits:
Object
  • Object
show all
Includes:
Backports::Router::UnescapePath
Defined in:
lib/lanyon/router.rb

Overview

Router class for Lanyon applications.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Router

Creates a Router for the given root directory.



14
15
16
# File 'lib/lanyon/router.rb', line 14

def initialize(root)
  @root = File.expand_path(root)
end

Instance Attribute Details

#rootObject (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_bodyObject

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 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.



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