Class: Impression::FileTree

Inherits:
Resource show all
Defined in:
lib/impression/file_tree.rb

Overview

FileTree implements a resource that maps to a static file hierarchy.

Direct Known Subclasses

Jamstack

Constant Summary

Constants inherited from Resource

Resource::FIRST_PATH_SEGMENT_REGEXP

Instance Attribute Summary

Attributes inherited from Resource

#children, #parent, #path

Instance Method Summary collapse

Methods inherited from Resource

#absolute_path, #add_child, #each, #html_response, #json_response, #render_tree_to_static_files, #route, #text_response, #to_proc

Constructor Details

#initialize(directory:, **props) ⇒ void

Initializes a FileTree resource.

Parameters:

  • directory (String)

    static directory path



15
16
17
18
19
# File 'lib/impression/file_tree.rb', line 15

def initialize(directory:, **props)
  super(**props)
  @directory = directory
  @path_info_cache = {}
end

Instance Method Details

#call(req) ⇒ void

This method returns an undefined value.

Responds to a request.

Parameters:



25
26
27
28
# File 'lib/impression/file_tree.rb', line 25

def call(req)
  path_info = get_path_info(req.resource_relative_path)
  render_from_path_info(req, path_info)
end

#render_from_path_info(req, path_info) ⇒ Object

Renders a response from the given response kind and path.

Parameters:

  • req (Qeweney::Request)

    request

  • kind (Symbol)

    path kind (:not_found or :file)

  • path (String, nil)

    file path



35
36
37
38
39
40
41
42
43
44
# File 'lib/impression/file_tree.rb', line 35

def render_from_path_info(req, path_info)
  case path_info[:kind]
  when :not_found
    req.respond(nil, ':status' => Qeweney::Status::NOT_FOUND)
  when :file
    render_file(req, path_info)
  else
    raise "Invalid path info kind #{kind.inspect}"
  end
end