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

App

Constant Summary

Constants inherited from Resource

Resource::FIRST_PATH_SEGMENT_REGEXP

Instance Attribute Summary collapse

Attributes inherited from Resource

#children, #parent, #path

Instance Method Summary collapse

Methods inherited from Resource

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

Constructor Details

#initialize(directory: nil, **props, &block) ⇒ void

Initializes a ‘FileTree` resource.

Parameters:

  • directory (String) (defaults to: nil)

    static directory path



17
18
19
20
21
# File 'lib/impression/file_tree.rb', line 17

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

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



11
12
13
# File 'lib/impression/file_tree.rb', line 11

def directory
  @directory
end

Instance Method Details

#call(req) ⇒ void

This method returns an undefined value.

Responds to a request.

Parameters:



27
28
29
30
31
32
# File 'lib/impression/file_tree.rb', line 27

def call(req)
  return super if @directory.nil?

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

#render_from_path_info(req, path_info) ⇒ void

This method returns an undefined value.

Renders a response from the given path info.

Parameters:



39
40
41
42
43
44
45
46
47
48
# File 'lib/impression/file_tree.rb', line 39

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 #{path_info[:kind].inspect}"
  end
end