Class: Middleman::Extensions::AssetHash::AssetHashManager

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-more/extensions/asset_hash.rb

Overview

Central class for managing asset_hash extension

Instance Method Summary collapse

Constructor Details

#initialize(app, exts, ignore) ⇒ AssetHashManager

Returns a new instance of AssetHashManager.



29
30
31
32
33
# File 'lib/middleman-more/extensions/asset_hash.rb', line 29

def initialize(app, exts, ignore)
  @app = app
  @exts = exts
  @ignore = ignore
end

Instance Method Details

#manipulate_resource_list(resources) ⇒ void

This method returns an undefined value.

Update the main sitemap resource list



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/middleman-more/extensions/asset_hash.rb', line 37

def manipulate_resource_list(resources)
  resources.each do |resource|
    next unless @exts.include? resource.ext
    next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }

    # Render through the Rack interface so middleware and mounted apps get a shot
    rack_client = ::Rack::Test::Session.new(@app.class)
    response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => true })
    raise "#{resource.path} should be in the sitemap!" unless response.status == 200

    digest = Digest::SHA1.hexdigest(response.body)[0..7]

    resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
  end
end