Class: JsTestCore::Resources::File

Inherits:
ThinRest::Resource
  • Object
show all
Defined in:
lib/js_test_core/resources/file.rb

Direct Known Subclasses

Dir, Specs::SpecFileSuperclass

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
# File 'lib/js_test_core/resources/file.rb', line 33

def ==(other)
  return false unless other.class == self.class
  absolute_path == other.absolute_path && relative_path == other.relative_path
end

#getObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/js_test_core/resources/file.rb', line 15

def get
  extension = ::File.extname(absolute_path)
  content_type = MIME_TYPES[extension] || 'text/html'

  connection.terminate_after_sending do
    connection.send_head(
      200,
        'Content-Type' => content_type,
        'Last-Modified' => ::File.mtime(absolute_path).rfc822,
        'Content-Length' => ::File.size(absolute_path)
    )
    ::File.open(absolute_path) do |file|
      while !file.eof?
        connection.send_data(file.read(1024))
      end
    end
  end

  def ==(other)
    return false unless other.class == self.class
    absolute_path == other.absolute_path && relative_path == other.relative_path
  end
end