Module: Sprockets::Mime

Included in:
Sprockets, Processing
Defined in:
lib/sprockets/mime.rb

Instance Method Summary collapse

Instance Method Details

#encoding_for_mime_type(type) ⇒ Object

Returns the correct encoding for a given mime type, while falling back on the default external encoding, if it exists.



37
38
39
40
41
# File 'lib/sprockets/mime.rb', line 37

def encoding_for_mime_type(type)
  encoding = Encoding::BINARY if type =~ %r{^(image|audio|video)/}
  encoding ||= default_external_encoding if respond_to?(:default_external_encoding)
  encoding
end

#extension_for_mime_type(type) ⇒ Object



19
20
21
# File 'lib/sprockets/mime.rb', line 19

def extension_for_mime_type(type)
  mime_types.key(type)
end

#mime_types(ext = nil) ⇒ Object

Returns a ‘Hash` of registered mime types registered on the environment and those part of `Rack::Mime`.

If an ‘ext` is given, it will lookup the mime type for that extension.



9
10
11
12
13
14
15
16
# File 'lib/sprockets/mime.rb', line 9

def mime_types(ext = nil)
  if ext.nil?
    Rack::Mime::MIME_TYPES.merge(@mime_types)
  else
    ext = Sprockets::Utils.normalize_extension(ext)
    @mime_types[ext] || Rack::Mime::MIME_TYPES[ext]
  end
end

#register_mime_type(mime_type, ext) ⇒ Object

Register a new mime type.



29
30
31
32
# File 'lib/sprockets/mime.rb', line 29

def register_mime_type(mime_type, ext)
  ext = Sprockets::Utils.normalize_extension(ext)
  @mime_types[ext] = mime_type
end