Class: StaticSignature::Middleware
- Inherits:
-
Object
- Object
- StaticSignature::Middleware
- Defined in:
- lib/static_signature.rb
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #asset_signature(path) ⇒ Object
- #bust_tag(doc, tag_name, attribute) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
- #signature_cache ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
7 8 9 10 11 |
# File 'lib/static_signature.rb', line 7 def initialize(app, = {}) @app = app @static_dir = .fetch(:static_dir) @mutex = Mutex.new end |
Instance Method Details
#_call(env) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/static_signature.rb', line 17 def _call(env) status, headers, response = @app.call(env) if headers["Content-Type"] !~ /text\/html/ [status, headers, response] else body = "" response.each do |part| body << part end doc = Nokogiri::HTML(body) bust_tag(doc, "link", "href") bust_tag(doc, "script", "src") body = doc.to_html headers["Content-Length"] = body.bytesize.to_s [status, headers, [body]] end end |
#asset_signature(path) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/static_signature.rb', line 44 def asset_signature(path) full_path = File.join(@static_dir, path) @mutex.synchronize { signature_cache[path] ||= Digest::MD5.hexdigest(File.read(full_path)) } end |
#bust_tag(doc, tag_name, attribute) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/static_signature.rb', line 36 def bust_tag(doc, tag_name, attribute) doc.search(tag_name).each do |node| if node[attribute] && ! node[attribute].include?("//") node[attribute] += "?#{asset_signature(node[attribute])}" end end end |
#call(env) ⇒ Object
13 14 15 |
# File 'lib/static_signature.rb', line 13 def call(env) dup._call(env) end |
#signature_cache ⇒ Object
52 53 54 |
# File 'lib/static_signature.rb', line 52 def signature_cache Thread.current[:_signature_cache] ||= {} end |