Module: Sprockets::Mime

Includes:
HTTPUtils, Utils
Included in:
Configuration, Loader
Defined in:
lib/sprockets/mime.rb

Instance Method Summary collapse

Methods included from HTTPUtils

#find_best_mime_type_match, #find_best_q_match, #find_mime_type_matches, #find_q_matches, #match_mime_type?, #match_mime_type_keys, #parse_q_values

Methods included from Utils

#concat_javascript_sources, #dfs, #dfs_paths, #duplicable?, #hash_reassoc, #hash_reassoc1, #module_include, #string_end_with_semicolon?

Instance Method Details

#mime_extsObject

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.



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

def mime_exts
  config[:mime_exts]
end

#mime_type_charset_detecter(mime_type) ⇒ Object

Internal: Get detecter function for MIME type.

mime_type - String MIME type

Returns Proc detector or nil if none is available.



71
72
73
74
75
76
77
# 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_typesObject

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.



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

def mime_types
  config[:mime_types]
end

#read_file(filename, content_type = nil) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
# 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) ⇒ Object

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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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