class MIME::Types::Loader

This class is responsible for initializing the MIME::Types registry from the data files supplied with the mime-types library.

The Loader will use one of the following paths:

  1. The path provided in its constructor argument;

  2. The value of ENV; or

  3. The value of MIME::Types::Loader::PATH.

When load is called, the path will be searched recursively for all YAML (.yml or .yaml) files. By convention, there is one file for each media type (application.yml, audio.yml, etc.), but this is not required.

Constants

PATH

The path that will be used for loading the MIME::Types data. The default location is __FILE__/../../../../data, which is where the data lives in the gem installation of the mime-types library.

The MIME::Types::Loader will load all YAML files contained in this path. By convention, there is one file for each media type (e.g., application.yml, audio.yml, etc.).

System repackagers note: this is the constant that you would change if you repackage mime-types for your system. It is recommended that the path be something like /usr/share/ruby/mime-types/.

Attributes

container[R]

The MIME::Types container instance that will be loaded. If not provided at initialization, a new MIME::Types instance will be constructed.

path[R]

The path that will be read for the MIME::Types files.

Public Class Methods

new(path = nil, container = nil) click to toggle source

Creates a Loader object that can be used to load MIME::Types registries into memory, using YAML, JSON, or v1 registry format loaders.

# File lib/mime/types/loader.rb, line 26
def initialize(path = nil, container = nil)
  path       = path || ENV['RUBY_MIME_TYPES_DATA'] ||
    MIME::Types::Loader::PATH
  @path      = File.expand_path(File.join(path, '**'))
  @container = container || MIME::Types.new
end

Public Instance Methods

load()
Alias for: load_json
load_from_json(filename) click to toggle source

Loads MIME::Types from a single JSON file.

It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.

# File lib/mime/types/loader.rb, line 207
def load_from_json(filename)
  require 'json'
  JSON.load(read_file(filename)).map { |type| MIME::Type.new(type) }
end
load_from_v1(filename) click to toggle source

Build the type list from a file in the format:

[*][!][os:]mt/st[<ws>@ext][<ws>:enc][<ws>'url-list][<ws>=docs]

*

An unofficial MIME type. This should be used if and only if the MIME type is not properly specified (that is, not under either x-type or vnd.name.type).

!

An obsolete MIME type. May be used with an unofficial MIME type.

os:

Platform-specific MIME type definition.

mt

The media type.

st

The media subtype.

<ws>@ext

The list of comma-separated extensions.

<ws>:enc

The encoding.

<ws>‘url-list

The list of comma-separated URLs.

<ws>=docs

The documentation string.

That is, everything except the media type and the subtype is optional. The more information that’s available, though, the richer the values that can be provided.

# File lib/mime/types/loader.rb, line 125
    def load_from_v1(filename)
      data = read_file(filename).split($/)
      mime = MIME::Types.new
      data.each_with_index { |line, index|
        item = line.chomp.strip
        next if item.empty?

        begin
          m = MIME::Types::Loader::V1_FORMAT.match(item).captures
        rescue Exception
          warn <<-EOS
#{filename}:#{index}: Parsing error in v1 MIME type definition.
=> #{line}
          EOS
          raise
        end

        unregistered, obsolete, platform, mediatype, subtype, extensions,
          encoding, urls, docs, comment = *m

        if mediatype.nil?
          if comment.nil?
            warn <<-EOS
#{filename}:#{index}: Parsing error in v1 MIME type definition (no media type).
=> #{line}
            EOS
            raise
          end

          next
        end

        extensions &&= extensions.split(/,/)
        urls &&= urls.split(/,/)

        if docs.nil?
          use_instead = nil
        else
          use_instead = docs.scan(%r{use-instead:(\S+)}).flatten
          docs = docs.gsub(%r{use-instead:\S+}, "").squeeze(" \t")
        end

        mime_type = MIME::Type.new("#{mediatype}/#{subtype}") do |t|
          t.extensions  = extensions
          t.encoding    = encoding
          t.system      = platform
          t.obsolete    = obsolete
          t.registered  = false if unregistered
          t.use_instead = use_instead
          t.docs        = docs
          t.references  = urls
        end

        mime.add_type(mime_type, true)
      }
      mime
    end
load_from_yaml(filename) click to toggle source

Loads MIME::Types from a single YAML file.

It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type.

Note that the YAML format is about 2½ times slower than either the v1 format or the JSON format.

NOTE: The purpose of this format is purely for maintenance reasons.

# File lib/mime/types/loader.rb, line 192
def load_from_yaml(filename)
  begin
    require 'psych'
  rescue LoadError
    nil
  end
  require 'yaml'
  YAML.load(read_file(filename))
end
load_json() click to toggle source

Loads a MIME::Types registry from JSON files (*.json) recursively found in path.

It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.

This method is aliased to load.

# File lib/mime/types/loader.rb, line 58
def load_json
  Dir[json_path].sort.each do |f|
    types = self.class.load_from_json(f).map { |type|
      MIME::Type.new(type)
    }
    container.add(*types, :silent)
  end
  container
end
Also aliased as: load
load_v1() click to toggle source

Loads a MIME::Types registry from files found in path that are in the v1 data format. The file search for this will exclude both JSON (*.json) and YAML (*.yml or *.yaml) files.

This method has been deprecated.

# File lib/mime/types/loader.rb, line 74
def load_v1
  MIME.deprecated(self.class, __method__)
  Dir[v1_path].sort.each do |f|
    next if f =~ /\.ya?ml$|\.json$/
    container.add(self.class.load_from_v1(f), true)
  end
  container
end
load_yaml() click to toggle source

Loads a MIME::Types registry from YAML files (*.yml or *.yaml) recursively found in path.

It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type.

Note that the YAML format is about 2½ times slower than either the v1 format or the JSON format.

NOTE: The purpose of this format is purely for maintenance reasons.

# File lib/mime/types/loader.rb, line 43
def load_yaml
  Dir[yaml_path].sort.each do |f|
    container.add(*self.class.load_from_yaml(f), :silent)
  end
  container
end