Sha256: 5177bc4291e25d719c5e5a015dc02a55db0ad730be09e3c8699318c35823af0c

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 KB

Contents

module JsTestCore
  module Resources
    class File < Resource
      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("#{public_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
        Representations::Dir.new(:relative_path => relative_path, :absolute_path => absolute_path).to_s
      end

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

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
pivotal-screw-unit-0.5.0 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.5.1 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.5.2 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-0.5.3 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.3 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.4 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.5 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.6 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.7 vendor/js-test-core/lib/js_test_core/resources/file.rb
pivotal-screw-unit-server-0.5.8 vendor/js-test-core/lib/js_test_core/resources/file.rb
screw-unit-0.5.1 vendor/js-test-core/lib/js_test_core/resources/file.rb
screw-unit-0.5.2 vendor/js-test-core/lib/js_test_core/resources/file.rb
screw-unit-server-0.5.3 vendor/js-test-core/lib/js_test_core/resources/file.rb