Class: Miron::Middleware::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/miron/middleware/static.rb

Constant Summary collapse

ALLOWED_VERBS =
['GET' 'HEAD' 'OPTIONS']
ALLOW_HEADER =
ALLOWED_VERBS.join(', ')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Static

Returns a new instance of Static


13
14
15
16
# File 'lib/miron/middleware/static.rb', line 13

def initialize(args)
  @filesystem_root = (Pathname.pwd + args[0]).to_s || Dir.pwd.to_s
  @url_path = args[1] if args[1]
end

Instance Attribute Details

#filesystem_rootObject (readonly)

Returns the value of attribute filesystem_root


7
8
9
# File 'lib/miron/middleware/static.rb', line 7

def filesystem_root
  @filesystem_root
end

#requestObject (readonly)

Returns the value of attribute request


8
9
10
# File 'lib/miron/middleware/static.rb', line 8

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response


8
9
10
# File 'lib/miron/middleware/static.rb', line 8

def response
  @response
end

#url_pathObject (readonly)

Returns the value of attribute url_path


7
8
9
# File 'lib/miron/middleware/static.rb', line 7

def url_path
  @url_path
end

Instance Method Details

#call(request, response) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/miron/middleware/static.rb', line 18

def call(request, response)
  @request = request
  @response = response

  # Check to make sure it's a valid HTTP method
  if check_request_method == true
    @request_file_path = Pathname.new(@filesystem_root + request.hash['PATH'])
    # Make sure file is available
    if check_for_file == true
      # Calculate important things about the file
      file = File.read(@request_file_path)
      file_size = file.size.to_s
      file_type = MimeMagic.by_extension(File.extname(@request_file_path)) || 'text/plain'
      file_last_modified = File.mtime(@request_file_path.to_s).httpdate

      # Return a response!
      @response.http_status = 200
      @response.headers = { 'Content-Length' => file_size, 'Content-Type' => file_type, 'Last-Modified' => "#{file_last_modified}" }
      @response.body = file
      @response
    else
      return_404
    end
  else
    return_405
  end
end

#check_for_fileObject


46
47
48
# File 'lib/miron/middleware/static.rb', line 46

def check_for_file
  File.exist?(@request_file_path)
end

#check_request_methodObject


50
51
52
# File 'lib/miron/middleware/static.rb', line 50

def check_request_method
  ALLOWED_VERBS.first.include?(@request.hash['HTTP_METHOD'])
end

#return_404Object


54
55
56
57
58
59
# File 'lib/miron/middleware/static.rb', line 54

def return_404
  @response.http_status = 404
  @response.headers = {}
  @response.body = 'Not found'
  @response
end

#return_405Object


61
62
63
64
65
66
# File 'lib/miron/middleware/static.rb', line 61

def return_405
  @response.http_status = 405
  @response.headers = { 'Allow' => ALLOW_HEADER }
  @response.body = 'Method Not Allowed'
  @response
end