Class: Landline::Handlers::Serve

Inherits:
Probe
  • Object
show all
Defined in:
lib/landline/probe/serve_handler.rb

Overview

Probe that sends files from a location

Instance Attribute Summary collapse

Attributes inherited from Probe

#properties

Attributes inherited from Node

#remap, #root

Instance Method Summary collapse

Methods inherited from Node

#go, #reject

Constructor Details

#initialize(path, parent:) ⇒ Serve

Returns a new instance of Serve.

Parameters:



13
14
15
# File 'lib/landline/probe/serve_handler.rb', line 13

def initialize(path, parent:)
  super(path, parent: parent, filepath: true)
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



17
18
19
# File 'lib/landline/probe/serve_handler.rb', line 17

def response
  @response
end

Instance Method Details

#process(request) ⇒ Boolean, Array

Method callback on successful request navigation. Tries to serve files matched by handler

Parameters:

Returns:

  • (Boolean, Array)

    true if file was found



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/landline/probe/serve_handler.rb', line 23

def process(request)
  path = File.expand_path(request.filepath)
  return unless path.start_with? @properties["path"]

  filepath = path.delete_suffix("/")

  [200,
   {
     "content-type" => Landline::MIME.get_mime_type(filepath)
   },
   File.open(filepath)]
rescue StandardError
  false
end