Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit aa856e0d66b4d3ec8251b8d3c7da1260618cfafd 0 parents
@dhh dhh authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+AssetServer
+===========
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the asset_server plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the asset_server plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'AssetServer'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  init.rb
@@ -0,0 +1 @@
+# Include hook code here
83 lib/asset_server.rb
@@ -0,0 +1,83 @@
+require 'digest/md5'
+require 'time'
+
+# match "/stylesheets/all.js", :to => Assets::Server.new("#{Rails.root}/app/assets/stylesheets/**/*")
+# match "/javascript", :to => Assets::Server.new("#{Rails.root}/app/assets/javascript")
+# match "/images", :to => Assets::Server.new("#{Rails.root}/app/assets/images")
+
+
+module Assets
+ class BundleServer
+ YEAR_IN_SECONDS = 31540000
+
+ def initialize(*paths)
+ @sources = paths.map { |path| Dir[path] }.flatten
+ end
+
+ def last_modified_source
+ @sources.collect { |file| File.mtime(file) }.sort.last
+ end
+
+ def call(env)
+ rebundle if source_changed?
+
+ if not_modified?(env) || etag_match?(env)
+ not_modified_response(env)
+ else
+ ok_response(env)
+ end
+ end
+
+ private
+ def source_changed?
+ true
+ end
+
+ def rebundle
+ @source = concate_source
+ @etag = compute_quoted_md5
+ @previous_last_modified = last_modified_source
+ end
+
+
+ def not_modified?(env)
+ env["HTTP_IF_MODIFIED_SINCE"] == @previous_last_modified.httpdate
+ end
+
+ def etag_match?(env)
+ env["HTTP_IF_NONE_MATCH"] == @etag
+ end
+
+ def not_modified_response(env)
+ [ 304, headers(env), [] ]
+ end
+
+ def ok_response(env)
+ [ 200, headers(env), [@source] ]
+ end
+
+
+ def headers(env)
+ Hash.new.tap do |headers|
+ headers["Content-Type"] = "text/javascript"
+ headers["Content-Length"] = @source.size.to_s
+
+ headers["Cache-Control"] = "public, must-revalidate"
+ headers["Last-Modified"] = @previous_last_modified.httpdate
+ headers["ETag"] = @etag
+
+ if env["QUERY_STRING"] == self.md5
+ headers["Cache-Control"] << ", max-age=#{YEAR_IN_SECONDS}"
+ end
+ end
+ end
+
+ def concate_source
+ @sources.collect { |file| File.read(file) }.join("\n\n")
+ end
+
+ def compute_quoted_md5
+ %("#{Digest::MD5.hexdigest(@source)}")
+ end
+ end
+end
4 tasks/asset_server_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :asset_server do
+# # Task goes here
+# end
83 test/asset_server_test.rb
@@ -0,0 +1,83 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
+
+require 'rack'
+require 'asset_server'
+
+require "test/unit"
+
+class AssetServerTest < Test::Unit::TestCase
+ FIXTURE_PATH = "#{File.dirname(__FILE__)}/fixtures"
+
+ App = Rack::Builder.new {
+ map "/javascripts/all.js" do
+ run Assets::BundleServer.new("#{FIXTURE_PATH}/javascripts/*.js")
+ end
+
+ map "/javascripts/all_foo.js" do
+ run Assets::BundleServer.new("#{FIXTURE_PATH}/javascripts/*.js", "#{File.dirname(__FILE__)}/fixtures/plugins/foo/javascripts/*.js")
+ end
+
+ map "/javascripts/all_plugins.js" do
+ run Assets::BundleServer.new("#{FIXTURE_PATH}/javascripts/*.js", "#{File.dirname(__FILE__)}/fixtures/plugins/**/javascripts/*.js")
+ end
+ }
+
+ def test_serves_javascript_assets_from_directory
+ response = Rack::MockRequest.new(App).get("/javascripts/all.js")
+ assert_equal "var bar;\n\n\nvar foo;", response.body
+ end
+
+ def test_serves_javascript_assets_from_all_and_plugin
+ response = Rack::MockRequest.new(App).get("/javascripts/all_foo.js")
+ assert_equal "var bar;\n\n\nvar foo;\n\nvar foo_plugin;", response.body
+ end
+
+ def test_serves_javascript_assets_from_all_and_all_plugins
+ response = Rack::MockRequest.new(App).get("/javascripts/all_plugins.js")
+ assert_equal "var bar;\n\n\nvar foo;\n\nvar bar_plugin;\n\nvar foo_plugin;", response.body
+ end
+
+ def test_serves_source_with_etag_headers
+ response = Rack::MockRequest.new(App).get("/javascripts/all.js")
+ assert_equal "\"b6d428092bf6479893474f9dd032faeb\"", response.headers["ETag"]
+ end
+
+ def test_updated_file_updates_the_last_modified
+ response = Rack::MockRequest.new(App).get("/javascripts/all.js")
+ time_before_touching = response.headers["Last-Modified"]
+
+ response = Rack::MockRequest.new(App).get("/javascripts/all.js")
+ time_after_touching = response.headers["Last-Modified"]
+
+ assert_equal time_before_touching, time_after_touching
+
+ touch_fixture("javascripts/bar.js")
+
+ response = Rack::MockRequest.new(App).get("/javascripts/all.js")
+ time_after_touching = response.headers["Last-Modified"]
+
+ assert_not_equal time_before_touching, time_after_touching
+ end
+
+ def test_not_modified_response_when_headers_match
+ touch_fixture("plugins/bar/javascripts/bar.js")
+
+ response = Rack::MockRequest.new(App).get(
+ "/javascripts/all_plugins.js",
+ "HTTP_IF_MODIFIED_SINCE" => File.mtime("#{FIXTURE_PATH}/plugins/bar/javascripts/bar.js").httpdate
+ )
+
+ assert_equal 304, response.status
+ end
+
+ def test_query_string_md5_sets_expiration_to_the_future
+ flunk
+ end
+
+ private
+ def touch_fixture(relative_path)
+ `touch #{FIXTURE_PATH}/#{relative_path}`
+ end
+end
1  test/fixtures/javascripts/bar.js
@@ -0,0 +1 @@
+var bar;
1  test/fixtures/javascripts/foo.js
@@ -0,0 +1 @@
+var foo;
1  test/fixtures/plugins/bar/javascripts/bar.js
@@ -0,0 +1 @@
+var bar_plugin;
1  test/fixtures/plugins/foo/javascripts/foo.js
@@ -0,0 +1 @@
+var foo_plugin;
3  test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.