Module: Sprockets::Mime

### Home Classes Methods

### In Files

- lib/sprockets/mime.rb

### Methods

- #mime_exts
- #mime_type_charset_detecter
- #mime_types
- #read_file
- #register_mime_type

### Included Modules

- Sprockets::HTTPUtils
- Sprockets::Utils

### Class/Module Index

Quicksearch

- Sprockets
- Sprockets::ArgumentError
- Sprockets::Asset
- Sprockets::Autoload
- Sprockets::Base
- Sprockets::Bower
- Sprockets::Bundle
- Sprockets::Cache
- Sprockets::Cache::FileStore
- Sprockets::Cache::GetWrapper
- Sprockets::Cache::HashWrapper
- Sprockets::Cache::MemoryStore
- Sprockets::Cache::NullStore
- Sprockets::Cache::ReadWriteWrapper
- Sprockets::Cache::Wrapper
- Sprockets::CachedEnvironment
- Sprockets::ClosureCompressor
- Sprockets::CoffeeScriptProcessor
- Sprockets::CoffeeScriptTemplate
- Sprockets::Compressing
- Sprockets::Configuration
- Sprockets::ContentTypeMismatch
- Sprockets::Context
- Sprockets::ConversionError
- Sprockets::Dependencies
- Sprockets::DigestUtils
- Sprockets::DirectiveProcessor
- Sprockets::ERBProcessor
- Sprockets::ERBTemplate
- Sprockets::EcoProcessor
- Sprockets::EcoTemplate
- Sprockets::EjsProcessor
- Sprockets::EjsTemplate
- Sprockets::EncodingUtils
- Sprockets::Engines
- Sprockets::Environment
- Sprockets::Error
- Sprockets::FileNotFound
- Sprockets::FileOutsidePaths
- Sprockets::FileReader
- Sprockets::HTTPUtils
- Sprockets::Index
- Sprockets::JstProcessor
- Sprockets::LegacyProcProcessor
- Sprockets::LegacyTiltProcessor
- Sprockets::Loader
- Sprockets::Manifest
- Sprockets::ManifestUtils
- Sprockets::Mime
- Sprockets::NotFound
- Sprockets::NotImplementedError
- Sprockets::PathDependencyUtils
- Sprockets::PathDigestUtils
- Sprockets::PathUtils
- Sprockets::Paths
- Sprockets::Processing
- Sprockets::ProcessorUtils
- Sprockets::Resolve
- Sprockets::SassCompressor
- Sprockets::SassProcessor
- Sprockets::SassProcessor::CacheStore
- Sprockets::SassProcessor::Functions
- Sprockets::SassTemplate
- Sprockets::ScssProcessor
- Sprockets::ScssTemplate
- Sprockets::Server
- Sprockets::Transformers
- Sprockets::URITar
- Sprockets::URIUtils
- Sprockets::UglifierCompressor
- Sprockets::UnloadedAsset
- Sprockets::Utils
- Sprockets::YUICompressor
- Rake
- Rake::SprocketsTask

No matching classes.

# Sprockets::Mime

### Public Instance Methods

mime_exts() click to toggle source

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.

    # File lib/sprockets/mime.rb, line 33
    def mime_exts
      config[:mime_exts]
    end

mime_type_charset_detecter(mime_type) click to toggle source

Internal: Get detecter function for MIME type.

mime_type - String MIME type

Returns Proc detector or nil if none is available.

    # File lib/sprockets/mime.rb, line 81
    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() click to toggle source

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.

    # File lib/sprockets/mime.rb, line 17
    def mime_types
      config[:mime_types]
    end

read_file(filename, content_type = nil) click to toggle source

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.

    # File lib/sprockets/mime.rb, line 96
    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, options = {}) click to toggle source

Public: Register a new mime type.

mime_type - String MIME Type options - Hash

    extensions: Array of String extnames
    charset: Proc/Method that detects the charset of a file.
             See EncodingUtils.

Returns nothing.

    # File lib/sprockets/mime.rb, line 46
    def register_mime_type(mime_type, options = {})
      # Legacy extension argument, will be removed from 4.x
      if options.is_a?(String)
        options = { extensions: [options] }
      end

      extnames = Array(options[:extensions]).map { |extname|
        Sprockets::Utils.normalize_extension(extname)
      }

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

      self.computed_config = {}

      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

[Validate]

Generated with the Darkfish Rdoc Generator 2.