lib/rack/manifest.rb in rack-manifest-0.1.3 vs lib/rack/manifest.rb in rack-manifest-0.1.4

- old
+ new

@@ -1,9 +1,8 @@ require 'json' require 'erb' -require 'action_view/helpers/asset_url_helper' -require 'rack/request' +require "digest/md5" require 'rack/manifest/version' require 'rack/manifest/rails' if defined?(Rails::Railtie) require 'rack/manifest/sprockets' if defined?(Sprockets) && defined?(Rails) class Rack::Manifest @@ -21,33 +20,29 @@ @app = app end def call(env) if env['PATH_INFO'] == '/manifest.json' - headers = {} - if env.has_key?('HTTP_IF_MODIFIED_SINCE') - fetched_date = env['HTTP_IF_MODIFIED_SINCE'] - return [304, headers, []] if get_modified_time(FILE_PATH) == fetched_date - end manifest = load_yaml(FILE_PATH) json = JSON.generate(manifest) + etag = digest(json) + return [304, {}, []] if env['HTTP_IF_NONE_MATCH'] == etag [ 200, - headers.merge({ + { 'Content-Type' => 'application/json', - 'Last-Modified' => get_modified_time(FILE_PATH), + 'Etag' => etag, 'Content-Length' => json.length.to_s - }), + }, [json] ] else @app.call(env) end end private - def get_modified_time(path) - time = File.mtime(path) - time.strftime('%a, %d %b %Y %H:%M:%S GMT') + def digest(str) + Digest::MD5.hexdigest(str) end end