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::Data::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.

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

load(options = {columnar: false}) click to toggle source

Loads the default MIME::Type registry.

# File lib/mime/types/loader.rb, line 91
def load(options = {columnar: false})
  new.load(options)
end
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 125
def load_from_json(filename)
  require "json"
  JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
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 the JSON format.

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

# File lib/mime/types/loader.rb, line 104
def load_from_yaml(filename)
  begin
    require "psych"
  rescue LoadError
    nil
  end

  require "yaml"

  if old_yaml?
    YAML.safe_load(read_file(filename), [MIME::Type])
  else
    YAML.safe_load(read_file(filename), permitted_classes: [MIME::Type])
  end
end
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 Columnar registry format loaders.

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

Private Class Methods

old_yaml?() click to toggle source
# File lib/mime/types/loader.rb, line 136
def old_yaml?
  @old_yaml ||=
    begin
      require "rubygems/version"
      Gem::Version.new(YAML::VERSION) < Gem::Version.new("3.1")
    end
end
read_file(filename) click to toggle source
# File lib/mime/types/loader.rb, line 132
def read_file(filename)
  File.open(filename, "r:UTF-8:-", &:read)
end

Public Instance Methods

load(options = {columnar: false}) click to toggle source

Loads a MIME::Types registry. Loads from JSON files by default (#load_json).

This will load from columnar files (#load_columnar) if columnar: true is provided in options and there are columnar files in path.

# File lib/mime/types/loader.rb, line 81
def load(options = {columnar: false})
  if options[:columnar] && !Dir[columnar_path].empty?
    load_columnar
  else
    load_json
  end
end
load_columnar() click to toggle source

Loads a MIME::Types registry from columnar files recursively found in path.

# File lib/mime/types/loader.rb, line 68
def load_columnar
  require "mime/types/columnar" unless defined?(MIME::Types::Columnar)
  container.extend(MIME::Types::Columnar)
  container.load_base_data(path)

  container
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.

# 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)
    container.add(*types, :silent)
  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 the JSON format.

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

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

Private Instance Methods

columnar_path() click to toggle source
# File lib/mime/types/loader.rb, line 155
def columnar_path
  File.join(path, "*.column")
end
json_path() click to toggle source
# File lib/mime/types/loader.rb, line 151
def json_path
  File.join(path, "*.json")
end
yaml_path() click to toggle source
# File lib/mime/types/loader.rb, line 147
def yaml_path
  File.join(path, "*.y{,a}ml")
end