Class: JsTestServer::Server::Resources::File

Inherits:
Resource
  • Object
show all
Defined in:
lib/js_test_server/server/resources/file.rb

Direct Known Subclasses

FrameworkFile, SpecFile, WebRoot

Constant Summary collapse

MIME_TYPES =
{
  '.html' => 'text/html',
  '.htm' => 'text/html',
  '.js' => 'text/javascript',
  '.css' => 'text/css',
  '.png' => 'image/png',
  '.jpg' => 'image/jpeg',
  '.jpeg' => 'image/jpeg',
  '.gif' => 'image/gif',
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.render_file(absolute_path) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/js_test_server/server/resources/file.rb', line 2

def self.render_file(absolute_path)
  extension = ::File.extname(absolute_path)
  content_type = MIME_TYPES[extension] || 'text/html'
  headers = {
    'Content-Type' => content_type,
    'Last-Modified' => ::File.mtime(absolute_path).rfc822
  }
  [200, headers, ::File.read(absolute_path)]
end

Instance Method Details

#absolute_pathObject



33
34
35
# File 'lib/js_test_server/server/resources/file.rb', line 33

def absolute_path
  @absolute_path ||= ::File.expand_path("#{root_path}#{relative_path}")
end

#relative_pathObject



29
30
31
# File 'lib/js_test_server/server/resources/file.rb', line 29

def relative_path
  @relative_path ||= request.path_info
end