Class: Condenser::ZlibWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/condenser/writers/zlib_writer.rb

Overview

Generates a ‘.gz` file using the zlib algorithm built into Ruby’s standard library.

Constant Summary collapse

COMPRESSALBE_TYPES =

What mime types should we compress? This list comes from: www.fastly.com/blog/new-gzip-settings-and-deciding-what-compress

%w( text/html application/x-javascript text/css
application/javascript text/javascript text/plain text/xml
application/json application/vnd.ms-fontobject application/x-font-opentype
application/x-font-truetype application/x-font-ttf application/xml font/eot
font/opentype font/otf image/svg+xml image/vnd.microsoft.icon image/x-icon )
ADDED_MIME_TYPES =
['application/gzip']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_types: nil, added_mime_types: nil) ⇒ ZlibWriter

Returns a new instance of ZlibWriter.



20
21
22
23
# File 'lib/condenser/writers/zlib_writer.rb', line 20

def initialize(mime_types: nil, added_mime_types: nil)
  @mime_types = mime_types || COMPRESSALBE_TYPES
  @added_mime_types = added_mime_types || ADDED_MIME_TYPES
end

Instance Attribute Details

#added_mime_typesObject (readonly)

Returns the value of attribute added_mime_types.



18
19
20
# File 'lib/condenser/writers/zlib_writer.rb', line 18

def added_mime_types
  @added_mime_types
end

#mime_typesObject (readonly)

Returns the value of attribute mime_types.



18
19
20
# File 'lib/condenser/writers/zlib_writer.rb', line 18

def mime_types
  @mime_types
end

Instance Method Details

#call(output_directory, asset) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/condenser/writers/zlib_writer.rb', line 33

def call(output_directory, asset)
  filename = File.join(output_directory, "#{asset.path}.gz")
  FileUtils.mkdir_p(File.dirname(filename))
  Condenser::Utils.atomic_write(filename) do |file|
    gz = Zlib::GzipWriter.new(file, Zlib::BEST_COMPRESSION)
    gz.write(asset.source)
    gz.close
    # File.utime(mtime, mtime, file.path)
  end
  
  ["#{asset.filename}.gz"]
end

#exist?(asset) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/condenser/writers/zlib_writer.rb', line 29

def exist?(asset)
  ::File.exist?(path(asset))
end

#path(asset) ⇒ Object



25
26
27
# File 'lib/condenser/writers/zlib_writer.rb', line 25

def path(asset)
  "#{asset.path}.gz"
end