Class: AssetPacker::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_packer/processor.rb,
lib/asset_packer/processor/chain.rb,
lib/asset_packer/processor/local.rb

Direct Known Subclasses

Local

Defined Under Namespace

Classes: Chain, Local

Instance Attribute Summary collapse

Instance Method Summary collapse

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).expand_path
    @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_dirObject (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

#destinationObject (readonly)

Returns the value of attribute destination.



3
4
5
# File 'lib/asset_packer/processor.rb', line 3

def destination
  @destination
end

#full_source_uriObject (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_uriObject (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