123456789_123456789_123456789_123456789_123456789_

Module: Sprockets::Mime

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, HTTPUtils, Utils
Defined in: lib/sprockets/mime.rb

Constant Summary

Utils - Included

MODULE_INCLUDE_MUTEX, WHITESPACE_ORDINALS

Instance Method Summary

HTTPUtils - Included

#find_best_mime_type_match

Internal: Find the best qvalue match from an Array of available mime type options.

#find_best_q_match

Internal: Find the best qvalue match from an Array of available options.

#find_mime_type_matches

Internal: Find the all qvalue match from an Array of available mime type options.

#find_q_matches

Internal: Find all qvalue matches from an Array of available options.

#match_mime_type?

Public: Test mime type against mime range.

#match_mime_type_keys

Public: Return values from Hash where the key matches the mime type.

#parse_q_values

Internal: Parse Accept header quality values.

Utils - Included

#concat_javascript_sources

Internal: Accumulate asset source to buffer and append a trailing semicolon if necessary.

#dfs

Internal: Post-order Depth-First search algorithm.

#dfs_paths

Internal: Post-order Depth-First search algorithm that gathers all paths along the way.

#duplicable?

Internal: Check if object can safely be .dup’d.

#hash_reassoc

Internal: Duplicate and store key/value on new frozen hash.

#hash_reassoc1

Internal: Duplicate and store key/value on new frozen hash.

#module_include

Internal: Inject into target module for the duration of the block.

#string_end_with_semicolon?

Internal: Check if string has a trailing semicolon.

Instance Method Details

#mime_exts

Internal: Mapping of MIME extension Strings to MIME type Strings.

Used for internal fast lookup purposes.

Examples:

mime_exts['.js'] #=> 'application/javascript'

key - MIME extension String value - MIME Type String

Returns Hash.

[ GitHub ]

  
# File 'lib/sprockets/mime.rb', line 34

def mime_exts
  config[:mime_exts]
end

#mime_type_charset_detecter(mime_type)

Internal: Get detecter function for MIME type.

mime_type - String MIME type

Returns Proc detector or nil if none is available.

[ GitHub ]

  
# File 'lib/sprockets/mime.rb', line 71

def mime_type_charset_detecter(mime_type)
  if type = config[:mime_types][mime_type]
    if detect = type[:charset]
      return detect
    end
  end
end

#mime_types

Public: Mapping of MIME type Strings to properties Hash.

key - MIME Type String value - Hash

extensions - Array of extnames
charset    - Default Encoding or function to detect encoding

Returns Hash.

[ GitHub ]

  
# File 'lib/sprockets/mime.rb', line 18

def mime_types
  config[:mime_types]
end

#read_file(filename, content_type = nil)

Public: Read file on disk with MIME type specific encoding.

filename - String path content_type - String MIME type

Returns String file contents transcoded to UTF-8 or in its external encoding.

[ GitHub ]

  
# File 'lib/sprockets/mime.rb', line 86

def read_file(filename, content_type = nil)
  data = File.binread(filename)

  if detect = mime_type_charset_detecter(content_type)
    detect.call(data).encode(Encoding::UTF_8, universal_newline: true)
  else
    data
  end
end

#register_mime_type(mime_type, extensions: [], charset: nil)

Public: Register a new mime type.

mime_type - String MIME Type extensions - Array of String extnames charset - Proc/Method that detects the charset of a file.

See EncodingUtils.

Returns nothing.

[ GitHub ]

  
# File 'lib/sprockets/mime.rb', line 46

def register_mime_type(mime_type, extensions: [], charset: nil)
  extnames = Array(extensions)

  charset ||= :default if mime_type.start_with?('text/')
  charset = EncodingUtils::CHARSET_DETECT[charset] if charset.is_a?(Symbol)

  self.config = hash_reassoc(config, :mime_exts) do |mime_exts|
    extnames.each do |extname|
      mime_exts[extname] = mime_type
    end
    mime_exts
  end

  self.config = hash_reassoc(config, :mime_types) do |mime_types|
    type = { extensions: extnames }
    type[:charset] = charset if charset
    mime_types.merge(mime_type => type)
  end
end