summaryrefslogtreecommitdiff
path: root/lib/mime
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mime')
-rw-r--r--lib/mime/type.rb158
-rw-r--r--lib/mime/type/columnar.rb4
-rw-r--r--lib/mime/types.rb33
-rw-r--r--lib/mime/types/_columnar.rb36
-rw-r--r--lib/mime/types/cache.rb16
-rw-r--r--lib/mime/types/columnar.rb2
-rw-r--r--lib/mime/types/container.rb26
-rw-r--r--lib/mime/types/deprecations.rb26
-rw-r--r--lib/mime/types/full.rb4
-rw-r--r--lib/mime/types/loader.rb27
-rw-r--r--lib/mime/types/logger.rb2
-rw-r--r--lib/mime/types/registry.rb14
12 files changed, 178 insertions, 170 deletions
diff --git a/lib/mime/type.rb b/lib/mime/type.rb
index 9035a73..cdf2471 100644
--- a/lib/mime/type.rb
+++ b/lib/mime/type.rb
@@ -92,21 +92,21 @@ class MIME::Type
end
# The released version of the mime-types library.
- VERSION = '3.3.1'
+ VERSION = "3.3.1"
include Comparable
# :stopdoc:
# TODO verify mime-type character restrictions; I am pretty sure that this is
# too wide open.
- MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)}.freeze
- I18N_RE = /[^[:alnum:]]/.freeze
- BINARY_ENCODINGS = %w(base64 8bit).freeze
- ASCII_ENCODINGS = %w(7bit quoted-printable).freeze
+ MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)}.freeze
+ I18N_RE = /[^[:alnum:]]/.freeze
+ BINARY_ENCODINGS = %w[base64 8bit].freeze
+ ASCII_ENCODINGS = %w[7bit quoted-printable].freeze
# :startdoc:
private_constant :MEDIA_TYPE_RE, :I18N_RE, :BINARY_ENCODINGS,
- :ASCII_ENCODINGS
+ :ASCII_ENCODINGS
# Builds a MIME::Type object from the +content_type+, a MIME Content Type
# value (e.g., 'text/plain' or 'application/x-eruby'). The constructed object
@@ -121,7 +121,7 @@ class MIME::Type
# * Otherwise, the content_type will be used as a string.
#
# Yields the newly constructed +self+ object.
- def initialize(content_type) # :yields self:
+ def initialize(content_type) # :yields: self
@friendly = {}
@obsolete = @registered = false
@preferred_extension = @docs = @use_instead = nil
@@ -148,11 +148,12 @@ class MIME::Type
# Indicates that a MIME type is like another type. This differs from
# <tt>==</tt> because <tt>x-</tt> prefixes are removed for this comparison.
def like?(other)
- other = if other.respond_to?(:simplified)
- MIME::Type.simplified(other.simplified, remove_x_prefix: true)
- else
- MIME::Type.simplified(other.to_s, remove_x_prefix: true)
- end
+ other =
+ if other.respond_to?(:simplified)
+ MIME::Type.simplified(other.simplified, remove_x_prefix: true)
+ else
+ MIME::Type.simplified(other.to_s, remove_x_prefix: true)
+ end
MIME::Type.simplified(simplified, remove_x_prefix: true) == other
end
@@ -166,8 +167,8 @@ class MIME::Type
elsif other.respond_to?(:simplified)
simplified <=> other.simplified
else
- filtered = 'silent' if other == :silent
- filtered ||= 'true' if other == true
+ filtered = "silent" if other == :silent
+ filtered ||= "true" if other == true
filtered ||= other.to_s
simplified <=> MIME::Type.simplified(filtered)
@@ -193,23 +194,24 @@ class MIME::Type
def priority_compare(other)
pc = simplified <=> other.simplified
if pc.zero? || !(extensions & other.extensions).empty?
- pc = if (reg = registered?) != other.registered?
- reg ? -1 : 1 # registered < unregistered
- elsif (comp = complete?) != other.complete?
- comp ? -1 : 1 # complete < incomplete
- elsif (obs = obsolete?) != other.obsolete?
- obs ? 1 : -1 # current < obsolete
- elsif obs and ((ui = use_instead) != (oui = other.use_instead))
- if ui.nil?
- 1
- elsif oui.nil?
- -1
- else
- ui <=> oui
- end
- else
- 0
- end
+ pc =
+ if (reg = registered?) != other.registered?
+ reg ? -1 : 1 # registered < unregistered
+ elsif (comp = complete?) != other.complete?
+ comp ? -1 : 1 # complete < incomplete
+ elsif (obs = obsolete?) != other.obsolete?
+ obs ? 1 : -1 # current < obsolete
+ elsif obs && ((ui = use_instead) != (oui = other.use_instead))
+ if ui.nil?
+ 1
+ elsif oui.nil?
+ -1
+ else
+ ui <=> oui
+ end
+ else
+ 0
+ end
end
pc
@@ -218,7 +220,7 @@ class MIME::Type
# Returns +true+ if the +other+ object is a MIME::Type and the content types
# match.
def eql?(other)
- other.kind_of?(MIME::Type) and self == other
+ other.is_a?(MIME::Type) && (self == other)
end
# Returns the whole MIME content-type string.
@@ -324,9 +326,9 @@ class MIME::Type
##
def encoding=(enc) # :nodoc:
- if enc.nil? or enc == :default
+ if enc.nil? || (enc == :default)
@encoding = default_encoding
- elsif BINARY_ENCODINGS.include?(enc) or ASCII_ENCODINGS.include?(enc)
+ elsif BINARY_ENCODINGS.include?(enc) || ASCII_ENCODINGS.include?(enc)
@encoding = enc
else
fail InvalidEncoding, enc
@@ -335,7 +337,7 @@ class MIME::Type
# Returns the default encoding for the MIME::Type based on the media type.
def default_encoding
- @media_type == 'text' ? 'quoted-printable' : 'base64'
+ @media_type == "text" ? "quoted-printable" : "base64"
end
##
@@ -355,7 +357,7 @@ class MIME::Type
# Returns +true+ if the media type is obsolete.
attr_accessor :obsolete
- alias obsolete? obsolete
+ alias_method :obsolete?, :obsolete
# The documentation for this MIME::Type.
attr_accessor :docs
@@ -365,7 +367,7 @@ class MIME::Type
# call-seq:
# text_plain.friendly # => "Text File"
# text_plain.friendly('en') # => "Text File"
- def friendly(lang = 'en')
+ def friendly(lang = "en")
@friendly ||= {}
case lang
@@ -377,7 +379,7 @@ class MIME::Type
@friendly.update(lang)
else
fail ArgumentError,
- "Expected a language or translation set, not #{lang.inspect}"
+ "Expected a language or translation set, not #{lang.inspect}"
end
end
@@ -408,14 +410,14 @@ class MIME::Type
# The decoded cross-reference URL list for this MIME::Type.
def xref_urls
xrefs.flat_map { |type, values|
- name = :"xref_url_for_#{type.tr('-', '_')}"
- respond_to?(name, true) and xref_map(values, name) or values.to_a
+ name = :"xref_url_for_#{type.tr("-", "_")}"
+ respond_to?(name, true) && xref_map(values, name) || values.to_a
}
end
# Indicates whether the MIME type has been registered with IANA.
attr_accessor :registered
- alias registered? registered
+ alias_method :registered?, :registered
# MIME types can be specified to be sent across a network in particular
# formats. This method returns +true+ when the MIME::Type encoding is set
@@ -433,7 +435,7 @@ class MIME::Type
# Indicateswhether the MIME type is declared as a signature type.
attr_accessor :signature
- alias signature? signature
+ alias_method :signature?, :signature
# Returns +true+ if the MIME::Type specifies an extension list,
# indicating that it is a complete MIME::Type.
@@ -456,7 +458,7 @@ class MIME::Type
# Converts the MIME::Type to a JSON string.
def to_json(*args)
- require 'json'
+ require "json"
to_h.to_json(*args)
end
@@ -472,26 +474,26 @@ class MIME::Type
#
# This method should be considered a private implementation detail.
def encode_with(coder)
- coder['content-type'] = @content_type
- coder['docs'] = @docs unless @docs.nil? or @docs.empty?
- coder['friendly'] = @friendly unless @friendly.nil? or @friendly.empty?
- coder['encoding'] = @encoding
- coder['extensions'] = @extensions.to_a unless @extensions.empty?
- coder['preferred-extension'] = @preferred_extension if @preferred_extension
+ coder["content-type"] = @content_type
+ coder["docs"] = @docs unless @docs.nil? || @docs.empty?
+ coder["friendly"] = @friendly unless @friendly.nil? || @friendly.empty?
+ coder["encoding"] = @encoding
+ coder["extensions"] = @extensions.to_a unless @extensions.empty?
+ coder["preferred-extension"] = @preferred_extension if @preferred_extension
if obsolete?
- coder['obsolete'] = obsolete?
- coder['use-instead'] = use_instead if use_instead
+ coder["obsolete"] = obsolete?
+ coder["use-instead"] = use_instead if use_instead
end
unless xrefs.empty?
{}.tap do |hash|
xrefs.each do |k, v|
hash[k] = v.to_a.sort
end
- coder['xrefs'] = hash
+ coder["xrefs"] = hash
end
end
- coder['registered'] = registered?
- coder['signature'] = signature? if signature?
+ coder["registered"] = registered?
+ coder["signature"] = signature? if signature?
coder
end
@@ -500,18 +502,18 @@ class MIME::Type
#
# This method should be considered a private implementation detail.
def init_with(coder)
- self.content_type = coder['content-type']
- self.docs = coder['docs'] || ''
- self.encoding = coder['encoding']
- self.extensions = coder['extensions'] || []
- self.preferred_extension = coder['preferred-extension']
- self.obsolete = coder['obsolete'] || false
- self.registered = coder['registered'] || false
- self.signature = coder['signature']
- self.xrefs = coder['xrefs'] || {}
- self.use_instead = coder['use-instead']
+ self.content_type = coder["content-type"]
+ self.docs = coder["docs"] || ""
+ self.encoding = coder["encoding"]
+ self.extensions = coder["extensions"] || []
+ self.preferred_extension = coder["preferred-extension"]
+ self.obsolete = coder["obsolete"] || false
+ self.registered = coder["registered"] || false
+ self.signature = coder["signature"]
+ self.xrefs = coder["xrefs"] || {}
+ self.use_instead = coder["use-instead"]
- friendly(coder['friendly'] || {})
+ friendly(coder["friendly"] || {})
end
def inspect # :nodoc:
@@ -535,8 +537,8 @@ class MIME::Type
# Converts a provided +content_type+ into a translation key suitable for
# use with the I18n library.
def i18n_key(content_type)
- simplify_matchdata(match(content_type), joiner: '.') { |e|
- e.gsub!(I18N_RE, '-')
+ simplify_matchdata(match(content_type), joiner: ".") { |e|
+ e.gsub!(I18N_RE, "-")
}
end
@@ -553,12 +555,12 @@ class MIME::Type
private
- def simplify_matchdata(matchdata, remove_x = false, joiner: '/')
+ def simplify_matchdata(matchdata, remove_x = false, joiner: "/")
return nil unless matchdata
matchdata.captures.map { |e|
e.downcase!
- e.sub!(/^x-/, '') if remove_x
+ e.sub!(/^x-/, "") if remove_x
yield e if block_given?
e
}.join(joiner)
@@ -571,11 +573,11 @@ class MIME::Type
match = MEDIA_TYPE_RE.match(type_string)
fail InvalidContentType, type_string if match.nil?
- @content_type = intern_string(type_string)
+ @content_type = intern_string(type_string)
@raw_media_type, @raw_sub_type = match.captures
- @simplified = intern_string(MIME::Type.simplified(match))
- @i18n_key = intern_string(MIME::Type.i18n_key(match))
- @media_type, @sub_type = MEDIA_TYPE_RE.match(@simplified).captures
+ @simplified = intern_string(MIME::Type.simplified(match))
+ @i18n_key = intern_string(MIME::Type.i18n_key(match))
+ @media_type, @sub_type = MEDIA_TYPE_RE.match(@simplified).captures
@raw_media_type = intern_string(@raw_media_type)
@raw_sub_type = intern_string(@raw_sub_type)
@@ -600,22 +602,22 @@ class MIME::Type
end
def xref_url_for_rfc(value)
- 'http://www.iana.org/go/%s' % value
+ "http://www.iana.org/go/%s" % value
end
def xref_url_for_draft(value)
- 'http://www.iana.org/go/%s' % value.sub(/\ARFC/, 'draft')
+ "http://www.iana.org/go/%s" % value.sub(/\ARFC/, "draft")
end
def xref_url_for_rfc_errata(value)
- 'http://www.rfc-editor.org/errata_search.php?eid=%s' % value
+ "http://www.rfc-editor.org/errata_search.php?eid=%s" % value
end
def xref_url_for_person(value)
- 'http://www.iana.org/assignments/media-types/media-types.xhtml#%s' % value
+ "http://www.iana.org/assignments/media-types/media-types.xhtml#%s" % value
end
def xref_url_for_template(value)
- 'http://www.iana.org/assignments/media-types/%s' % value
+ "http://www.iana.org/assignments/media-types/%s" % value
end
end
diff --git a/lib/mime/type/columnar.rb b/lib/mime/type/columnar.rb
index c29690e..93a3350 100644
--- a/lib/mime/type/columnar.rb
+++ b/lib/mime/type/columnar.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'mime/type'
+require "mime/type"
# A version of MIME::Type that works hand-in-hand with a MIME::Types::Columnar
# container to load data by columns.
@@ -36,7 +36,7 @@ class MIME::Type::Columnar < MIME::Type
column :docs, :docs=
column :preferred_extension, :preferred_extension=
column :obsolete, :obsolete=, :obsolete?, :registered, :registered=,
- :registered?, :signature, :signature=, :signature?, file: 'flags'
+ :registered?, :signature, :signature=, :signature?, file: "flags"
column :xrefs, :xrefs=, :xref_urls
column :use_instead, :use_instead=
diff --git a/lib/mime/types.rb b/lib/mime/types.rb
index 26ff937..2cf7fc4 100644
--- a/lib/mime/types.rb
+++ b/lib/mime/types.rb
@@ -7,7 +7,7 @@ module MIME
end
end
-require 'mime/type'
+require "mime/type"
# MIME::Types is a registry of MIME types. It is both a class (created with
# MIME::Types.new) and a default registry (loaded automatically or through
@@ -72,8 +72,8 @@ class MIME::Types
# Creates a new MIME::Types registry.
def initialize
- @type_variants = Container.new
- @extension_index = Container.new
+ @type_variants = Container.new
+ @extension_index = Container.new
end
# Returns the number of known type variants.
@@ -122,14 +122,15 @@ class MIME::Types
# 5. Obsolete definitions use-instead clauses are compared.
# 6. Sort on name.
def [](type_id, complete: false, registered: false)
- matches = case type_id
+ matches =
+ case type_id
when MIME::Type
@type_variants[type_id.simplified]
when Regexp
match(type_id)
else
@type_variants[MIME::Type.simplified(type_id)]
- end
+ end
prune_matches(matches, complete, registered).sort { |a, b|
a.priority_compare(b)
@@ -155,7 +156,7 @@ class MIME::Types
a.priority_compare(b)
}
end
- alias of type_for
+ alias_method :of, :type_for
# Add one or more MIME::Type objects to the set of known types. If the
# type is already known, a warning will be displayed.
@@ -163,7 +164,7 @@ class MIME::Types
# The last parameter may be the value <tt>:silent</tt> or +true+ which
# will suppress duplicate MIME type warnings.
def add(*types)
- quiet = ((types.last == :silent) or (types.last == true))
+ quiet = ((types.last == :silent) || (types.last == true))
types.each do |mime_type|
case mime_type
@@ -184,9 +185,9 @@ class MIME::Types
# already known, a warning will be displayed. The +quiet+ parameter may be a
# truthy value to suppress that warning.
def add_type(type, quiet = false)
- if !quiet and @type_variants[type.simplified].include?(type)
- MIME::Types.logger.warn <<-WARNING
-Type #{type} is already registered as a variant of #{type.simplified}.
+ if !quiet && @type_variants[type.simplified].include?(type)
+ MIME::Types.logger.warn <<~WARNING
+ Type #{type} is already registered as a variant of #{type.simplified}.
WARNING
end
@@ -223,9 +224,9 @@ Type #{type} is already registered as a variant of #{type.simplified}.
end
end
-require 'mime/types/cache'
-require 'mime/types/container'
-require 'mime/types/loader'
-require 'mime/types/logger'
-require 'mime/types/_columnar'
-require 'mime/types/registry'
+require "mime/types/cache"
+require "mime/types/container"
+require "mime/types/loader"
+require "mime/types/logger"
+require "mime/types/_columnar"
+require "mime/types/registry"
diff --git a/lib/mime/types/_columnar.rb b/lib/mime/types/_columnar.rb
index f68bf61..777c316 100644
--- a/lib/mime/types/_columnar.rb
+++ b/lib/mime/types/_columnar.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'mime/type/columnar'
+require "mime/type/columnar"
# MIME::Types::Columnar is used to extend a MIME::Types container to load data
# by columns instead of from JSON or YAML. Column loads of MIME types loaded
@@ -22,7 +22,7 @@ module MIME::Types::Columnar
def load_base_data(path) #:nodoc:
@__root__ = path
- each_file_line('content_type', false) do |line|
+ each_file_line("content_type", false) do |line|
line = line.split
content_type = line.shift
extensions = line
@@ -45,11 +45,11 @@ module MIME::Types::Columnar
i = -1
column = File.join(@__root__, "mime.#{name}.column")
- IO.readlines(column, encoding: 'UTF-8').each do |line|
+ IO.readlines(column, encoding: "UTF-8").each do |line|
line.chomp!
if lookup
- type = @__mime_data__[i += 1] or next
+ (type = @__mime_data__[i += 1]) || next
yield type, line
else
yield line
@@ -61,26 +61,26 @@ module MIME::Types::Columnar
end
def load_encoding
- each_file_line('encoding') do |type, line|
+ each_file_line("encoding") do |type, line|
pool ||= {}
type.instance_variable_set(:@encoding, (pool[line] ||= line))
end
end
def load_docs
- each_file_line('docs') do |type, line|
+ each_file_line("docs") do |type, line|
type.instance_variable_set(:@docs, opt(line))
end
end
def load_preferred_extension
- each_file_line('pext') do |type, line|
+ each_file_line("pext") do |type, line|
type.instance_variable_set(:@preferred_extension, opt(line))
end
end
def load_flags
- each_file_line('flags') do |type, line|
+ each_file_line("flags") do |type, line|
line = line.split
type.instance_variable_set(:@obsolete, flag(line.shift))
type.instance_variable_set(:@registered, flag(line.shift))
@@ -89,29 +89,29 @@ module MIME::Types::Columnar
end
def load_xrefs
- each_file_line('xrefs') { |type, line|
+ each_file_line("xrefs") { |type, line|
type.instance_variable_set(:@xrefs, dict(line, array: true))
}
end
def load_friendly
- each_file_line('friendly') { |type, line|
+ each_file_line("friendly") { |type, line|
type.instance_variable_set(:@friendly, dict(line))
}
end
def load_use_instead
- each_file_line('use_instead') do |type, line|
+ each_file_line("use_instead") do |type, line|
type.instance_variable_set(:@use_instead, opt(line))
end
end
def dict(line, array: false)
- if line == '-'
+ if line == "-"
{}
else
- line.split('|').each_with_object({}) { |l, h|
- k, v = l.split('^')
+ line.split("|").each_with_object({}) { |l, h|
+ k, v = l.split("^")
v = nil if v.empty?
h[k] = array ? Array(v) : v
}
@@ -119,18 +119,18 @@ module MIME::Types::Columnar
end
def arr(line)
- if line == '-'
+ if line == "-"
[]
else
- line.split('|').flatten.compact.uniq
+ line.split("|").flatten.compact.uniq
end
end
def opt(line)
- line unless line == '-'
+ line unless line == "-"
end
def flag(line)
- line == '1'
+ line == "1"
end
end
diff --git a/lib/mime/types/cache.rb b/lib/mime/types/cache.rb
index e3d69c8..31113a6 100644
--- a/lib/mime/types/cache.rb
+++ b/lib/mime/types/cache.rb
@@ -15,21 +15,21 @@ class << MIME::Types::Cache
# file does not exist, if the file cannot be loaded, or if the data in
# the cache version is different than this version.
def load(cache_file = nil)
- cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
- return nil unless cache_file and File.exist?(cache_file)
+ cache_file ||= ENV["RUBY_MIME_TYPES_CACHE"]
+ return nil unless cache_file && File.exist?(cache_file)
cache = Marshal.load(File.binread(cache_file))
if cache.version == MIME::Types::Data::VERSION
Marshal.load(cache.data)
else
- MIME::Types.logger.warn <<-WARNING.chomp
-Could not load MIME::Types cache: invalid version
+ MIME::Types.logger.warn <<~WARNING.chomp
+ Could not load MIME::Types cache: invalid version
WARNING
nil
end
rescue => e
- MIME::Types.logger.warn <<-WARNING.chomp
-Could not load MIME::Types cache: #{e}
+ MIME::Types.logger.warn <<~WARNING.chomp
+ Could not load MIME::Types cache: #{e}
WARNING
nil
end
@@ -44,12 +44,12 @@ Could not load MIME::Types cache: #{e}
# +RUBY_MIME_TYPES_CACHE+. If there is no cache file specified either
# directly or through the environment, this method will return +nil+
def save(types = nil, cache_file = nil)
- cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
+ cache_file ||= ENV["RUBY_MIME_TYPES_CACHE"]
return nil unless cache_file
types ||= MIME::Types.send(:__types__)
- File.open(cache_file, 'wb') do |f|
+ File.open(cache_file, "wb") do |f|
f.write(
Marshal.dump(new(MIME::Types::Data::VERSION, Marshal.dump(types)))
)
diff --git a/lib/mime/types/columnar.rb b/lib/mime/types/columnar.rb
index 5c7dd73..dcb87cf 100644
--- a/lib/mime/types/columnar.rb
+++ b/lib/mime/types/columnar.rb
@@ -1,3 +1,3 @@
# frozen_string_literal: true
-require 'mime/types'
+require "mime/types"
diff --git a/lib/mime/types/container.rb b/lib/mime/types/container.rb
index 0f08622..094682b 100644
--- a/lib/mime/types/container.rb
+++ b/lib/mime/types/container.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
-require 'set'
-require 'forwardable'
+require "set"
+require "forwardable"
# MIME::Types requires a serializable keyed container that returns an empty Set
# on a key miss. Hash#default_value cannot be used because, while it traverses
@@ -37,7 +37,7 @@ class MIME::Types::Container #:nodoc:
def merge!(other)
tap {
- other = other.kind_of?(MIME::Types::Container) ? other.container : other
+ other = other.is_a?(MIME::Types::Container) ? other.container : other
container.merge!(other)
normalize
}
@@ -48,15 +48,15 @@ class MIME::Types::Container #:nodoc:
end
def_delegators :@container,
- :==,
- :count,
- :each,
- :each_value,
- :empty?,
- :flat_map,
- :keys,
- :select,
- :values
+ :==,
+ :count,
+ :each,
+ :each_value,
+ :empty?,
+ :flat_map,
+ :keys,
+ :select,
+ :values
def add(key, value)
(container[key] ||= Set.new).add(value)
@@ -85,7 +85,7 @@ class MIME::Types::Container #:nodoc:
def normalize
container.each do |k, v|
- next if v.kind_of?(Set)
+ next if v.is_a?(Set)
container[k] = Set[*v]
end
diff --git a/lib/mime/types/deprecations.rb b/lib/mime/types/deprecations.rb
index 9de2946..8bd6455 100644
--- a/lib/mime/types/deprecations.rb
+++ b/lib/mime/types/deprecations.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-require 'mime/types/logger'
+require "mime/types/logger"
# The namespace for MIME applications, tools, and libraries.
module MIME
@@ -8,25 +8,29 @@ module MIME
class Types
# Used to mark a method as deprecated in the mime-types interface.
def self.deprecated(klass, sym, message = nil, &block) # :nodoc:
- level = case klass
+ level =
+ case klass
when Class, Module
- '.'
+ "."
else
klass = klass.class
- '#'
- end
- message = case message
+ "#"
+ end
+ message =
+ case message
when :private, :protected
"and will be #{message}"
when nil
- 'and will be removed'
+ "and will be removed"
else
message
- end
- MIME::Types.logger.warn <<-WARNING.chomp
-#{caller[1]}: #{klass}#{level}#{sym} is deprecated #{message}.
+ end
+ MIME::Types.logger.warn <<~WARNING.chomp
+ #{caller(2..2).first}: #{klass}#{level}#{sym} is deprecated #{message}.
WARNING
- block.call if block
+
+ return unless block
+ block.call
end
end
end
diff --git a/lib/mime/types/full.rb b/lib/mime/types/full.rb
index 920ad2a..a69e6bd 100644
--- a/lib/mime/types/full.rb
+++ b/lib/mime/types/full.rb
@@ -9,11 +9,11 @@ module MIME
private
def load_mode
- { columnar: false }
+ {columnar: false}
end
end
end
end
end
-require 'mime/types'
+require "mime/types"
diff --git a/lib/mime/types/loader.rb b/lib/mime/types/loader.rb
index a38ba48..b9694ac 100644
--- a/lib/mime/types/loader.rb
+++ b/lib/mime/types/loader.rb
@@ -4,10 +4,11 @@
##
module MIME; end
+
##
class MIME::Types; end
-require 'mime/types/data'
+require "mime/types/data"
# This class is responsible for initializing the MIME::Types registry from
# the data files supplied with the mime-types library.
@@ -30,7 +31,7 @@ class MIME::Types::Loader
# Creates a Loader object that can be used to load MIME::Types registries
# into memory, using YAML, JSON, or Columnar registry format loaders.
def initialize(path = nil, container = nil)
- path = path || ENV['RUBY_MIME_TYPES_DATA'] || MIME::Types::Data::PATH
+ path = path || ENV["RUBY_MIME_TYPES_DATA"] || MIME::Types::Data::PATH
@container = container || MIME::Types.new
@path = File.expand_path(path)
end
@@ -68,7 +69,7 @@ class MIME::Types::Loader
# Loads a MIME::Types registry from columnar files recursively found in
# +path+.
def load_columnar
- require 'mime/types/columnar' unless defined?(MIME::Types::Columnar)
+ require "mime/types/columnar" unless defined?(MIME::Types::Columnar)
container.extend(MIME::Types::Columnar)
container.load_base_data(path)
@@ -80,7 +81,7 @@ class MIME::Types::Loader
#
# This will load from columnar files (#load_columnar) if <tt>columnar:
# true</tt> is provided in +options+ and there are columnar files in +path+.
- def load(options = { columnar: false })
+ def load(options = {columnar: false})
if options[:columnar] && !Dir[columnar_path].empty?
load_columnar
else
@@ -90,7 +91,7 @@ class MIME::Types::Loader
class << self
# Loads the default MIME::Type registry.
- def load(options = { columnar: false })
+ def load(options = {columnar: false})
new.load(options)
end
@@ -105,12 +106,12 @@ class MIME::Types::Loader
# NOTE: The purpose of this format is purely for maintenance reasons.
def load_from_yaml(filename)
begin
- require 'psych'
+ require "psych"
rescue LoadError
nil
end
- require 'yaml'
- YAML.load(read_file(filename))
+ require "yaml"
+ YAML.safe_load(read_file(filename), permitted_classes: [MIME::Type])
end
# Loads MIME::Types from a single JSON file.
@@ -119,28 +120,28 @@ class MIME::Types::Loader
# The JSON format is the registry format for the MIME types registry
# shipped with the mime-types library.
def load_from_json(filename)
- require 'json'
+ require "json"
JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
end
private
def read_file(filename)
- File.open(filename, 'r:UTF-8:-', &:read)
+ File.open(filename, "r:UTF-8:-", &:read)
end
end
private
def yaml_path
- File.join(path, '*.y{,a}ml')
+ File.join(path, "*.y{,a}ml")
end
def json_path
- File.join(path, '*.json')
+ File.join(path, "*.json")
end
def columnar_path
- File.join(path, '*.column')
+ File.join(path, "*.column")
end
end
diff --git a/lib/mime/types/logger.rb b/lib/mime/types/logger.rb
index 927a4e5..a065fc3 100644
--- a/lib/mime/types/logger.rb
+++ b/lib/mime/types/logger.rb
@@ -2,7 +2,7 @@
# -*- ruby encoding: utf-8 -*-
-require 'logger'
+require "logger"
##
module MIME
diff --git a/lib/mime/types/registry.rb b/lib/mime/types/registry.rb
index 5c8e5c7..8d92d65 100644
--- a/lib/mime/types/registry.rb
+++ b/lib/mime/types/registry.rb
@@ -33,7 +33,7 @@ class << MIME::Types
def type_for(filename)
__types__.type_for(filename)
end
- alias of type_for
+ alias_method :of, :type_for
# MIME::Types#add against the default MIME::Types registry.
def add(*types)
@@ -43,22 +43,22 @@ class << MIME::Types
private
def lazy_load?
- return unless ENV.key?('RUBY_MIME_TYPES_LAZY_LOAD')
+ return unless ENV.key?("RUBY_MIME_TYPES_LAZY_LOAD")
- MIME::Types.logger.warn <<-WARNING.chomp
-Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
+ MIME::Types.logger.warn <<~WARNING.chomp
+ Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
WARNING
- (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
+ (lazy = ENV["RUBY_MIME_TYPES_LAZY_LOAD"]) && (lazy != "false")
end
def __types__
- (defined?(@__types__) and @__types__) or load_default_mime_types
+ (defined?(@__types__) && @__types__) || load_default_mime_types
end
unless private_method_defined?(:load_mode)
def load_mode
- { columnar: true }
+ {columnar: true}
end
end