Class: AssetPacker::Processor
- Inherits:
-
Object
- Object
- AssetPacker::Processor
- Defined in:
- lib/asset_packer/processor.rb,
lib/asset_packer/processor/chain.rb,
lib/asset_packer/processor/local.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#asset_dir ⇒ Object
readonly
Returns the value of attribute asset_dir.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#full_source_uri ⇒ Object
readonly
Returns the value of attribute full_source_uri.
-
#source_uri ⇒ Object
readonly
Returns the value of attribute source_uri.
Instance Method Summary collapse
- #cache(uri) ⇒ Object
-
#initialize(source_uri, asset_dir, destination) ⇒ Processor
constructor
source_uri: location of the original document, used to retrieve relative URI’s asset_dir: location where assets will be stored destination: file that will be generated, used to create relative URI’s from.
- #retrieve_asset(uri) ⇒ Object
Constructor Details
#initialize(source_uri, asset_dir, destination) ⇒ Processor
source_uri: location of the original document, used to retrieve relative URI’s asset_dir: location where assets will be stored destination: file that will be generated, used to create relative URI’s from
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/asset_packer/processor.rb', line 8 def initialize(source_uri, asset_dir, destination) @source_uri = source_uri @full_source_uri = URI(source_uri) if @full_source_uri.relative? source_file = Pathname(source_uri). @full_source_uri = URI("file://#{source_file}") @cache_dir = source_file.dirname.join('.asset_cache') FileUtils.mkdir_p @cache_dir unless @cache_dir.exist? end @asset_dir = Pathname(asset_dir) @destination = Pathname(destination) end |
Instance Attribute Details
#asset_dir ⇒ Object (readonly)
Returns the value of attribute asset_dir.
3 4 5 |
# File 'lib/asset_packer/processor.rb', line 3 def asset_dir @asset_dir end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
3 4 5 |
# File 'lib/asset_packer/processor.rb', line 3 def destination @destination end |
#full_source_uri ⇒ Object (readonly)
Returns the value of attribute full_source_uri.
3 4 5 |
# File 'lib/asset_packer/processor.rb', line 3 def full_source_uri @full_source_uri end |
#source_uri ⇒ Object (readonly)
Returns the value of attribute source_uri.
3 4 5 |
# File 'lib/asset_packer/processor.rb', line 3 def source_uri @source_uri end |
Instance Method Details
#cache(uri) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/asset_packer/processor.rb', line 33 def cache(uri) return yield if @cache_dir.nil? || uri.scheme == 'file' hash = Digest::SHA256.hexdigest(uri.to_s) cache_path = @cache_dir.join(hash) cache_path.write(yield) unless cache_path.exist? cache_path.read || '' end |
#retrieve_asset(uri) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/asset_packer/processor.rb', line 21 def retrieve_asset(uri) uri = URI.join(full_source_uri, uri) cache(uri) do case when %w[http https].include?(uri.scheme) Net::HTTP.get(uri) when uri.scheme.eql?('file') File.read(uri.path) end end end |