Sha256: cc70bb5b4c5fcd6ef7ec8c5b32d0a6ea50869a42fd1841c5cd23811a8eb72c81

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module JsTestCore
  module Resources
    class File < Resource
      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',
        }

      property :absolute_path, :relative_path

      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
      end
      
      def ==(other)
        return false unless other.class == self.class
        absolute_path == other.absolute_path && relative_path == other.relative_path
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pivotal-screw-unit-0.4.0 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.4.1 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.4.2 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.4.3 vendor/js-test-core/lib/js_test_core/resources/file.rb