Sha256: 2b3961df4c525062f8d678ef9db1d4779dd48f74dee94edb8449d11aef69f52b

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

class JsTestServer::Server::Resources::File < JsTestServer::Server::Resources::Resource
  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

  map "*"

  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',
  }

  get "*" do
    do_get
  end

  def relative_path
    @relative_path ||= request.path_info
  end

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

  protected

  def do_get
    if ::File.exists?(absolute_path)
      if ::File.directory?(absolute_path)
        render_dir
      else
        render_file
      end
    else
      pass
    end
  end

  def render_dir
    JsTestServer::Server::Representations::Dir.new(:relative_path => relative_path, :absolute_path => absolute_path).to_s
  end

  def render_file
    self.class.render_file(absolute_path)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
js-test-server-0.2.1 lib/js_test_server/server/resources/file.rb
js-test-server-0.2.0 lib/js_test_server/server/resources/file.rb