summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAustin Ziegler <austin@zieglers.ca>2015-05-24 01:15:47 -0400
committerAustin Ziegler <austin@zieglers.ca>2015-05-24 01:15:47 -0400
commit441579b4ce6693b715462705eeedfedfe8f44a77 (patch)
treea758fe1d72d25de7646e0ce699b5ddd304e09083
parent93f5571f132cd448e90e4d6331f22b3e04020a06 (diff)
downloadmime-types-2.6-work.tar.gz
Rubocopify.2.6-work
-rw-r--r--.gitignore1
-rw-r--r--Rakefile62
-rw-r--r--lib/mime.rb9
-rw-r--r--lib/mime/type.rb92
-rw-r--r--lib/mime/types.rb17
-rw-r--r--lib/mime/types/cache.rb8
-rw-r--r--lib/mime/types/columnar.rb15
-rw-r--r--lib/mime/types/loader.rb8
-rw-r--r--support/apache_mime_types.rb3
-rw-r--r--support/benchmarks/load.rb16
-rw-r--r--support/benchmarks/load_allocations.rb12
-rw-r--r--support/benchmarks/object_counts.rb4
-rw-r--r--support/convert.rb17
-rw-r--r--support/iana_registry.rb20
-rw-r--r--test/minitest_helper.rb9
-rw-r--r--test/test_mime_type.rb62
-rw-r--r--test/test_mime_types.rb22
-rw-r--r--test/test_mime_types_class.rb28
-rw-r--r--test/test_mime_types_loader.rb6
19 files changed, 215 insertions, 196 deletions
diff --git a/.gitignore b/.gitignore
index 051f986..597a659 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,3 +12,4 @@ html
pkg
publish
test/cache.tst
+tmp/
diff --git a/Rakefile b/Rakefile
index 5bc10d2..cfcff12 100644
--- a/Rakefile
+++ b/Rakefile
@@ -15,24 +15,24 @@ spec = Hoe.spec 'mime-types' do
developer('Austin Ziegler', 'halostatue@gmail.com')
self.need_tar = true
- self.require_ruby_version '>= 1.9.2'
+ require_ruby_version '>= 1.9.2'
self.history_file = 'History.rdoc'
self.readme_file = 'README.rdoc'
- self.extra_rdoc_files = FileList["*.rdoc"].to_a
- self.licenses = ["MIT", "Artistic 2.0", "GPL-2"]
-
- self.extra_dev_deps << ['hoe-doofus', '~> 1.0']
- self.extra_dev_deps << ['hoe-gemspec2', '~> 1.1']
- self.extra_dev_deps << ['hoe-git', '~> 1.6']
- self.extra_dev_deps << ['hoe-rubygems', '~> 1.0']
- self.extra_dev_deps << ['hoe-travis', '~> 1.2']
- self.extra_dev_deps << ['minitest', '~> 5.3']
- self.extra_dev_deps << ['minitest-autotest', '~>1.0']
- self.extra_dev_deps << ['minitest-focus', '~>1.0']
- self.extra_dev_deps << ['rake', '~> 10.0']
- self.extra_dev_deps << ['simplecov', '~> 0.7']
- self.extra_dev_deps << ['coveralls', '~> 0.8']
+ self.extra_rdoc_files = FileList['*.rdoc'].to_a
+ self.licenses = ['MIT', 'Artistic 2.0', 'GPL-2']
+
+ extra_dev_deps << ['hoe-doofus', '~> 1.0']
+ extra_dev_deps << ['hoe-gemspec2', '~> 1.1']
+ extra_dev_deps << ['hoe-git', '~> 1.6']
+ extra_dev_deps << ['hoe-rubygems', '~> 1.0']
+ extra_dev_deps << ['hoe-travis', '~> 1.2']
+ extra_dev_deps << ['minitest', '~> 5.3']
+ extra_dev_deps << ['minitest-autotest', '~>1.0']
+ extra_dev_deps << ['minitest-focus', '~>1.0']
+ extra_dev_deps << ['rake', '~> 10.0']
+ extra_dev_deps << ['simplecov', '~> 0.7']
+ extra_dev_deps << ['coveralls', '~> 0.8']
end
task :support do
@@ -45,7 +45,7 @@ task 'support:nokogiri' => :support do
begin
gem 'nokogiri'
rescue Gem::LoadError
- fail "Nokogiri is not installed. Please install it."
+ raise 'Nokogiri is not installed. Please install it.'
end
end
@@ -53,8 +53,10 @@ namespace :benchmark do
desc 'Benchmark Load Times'
task :load, [ :repeats ] => :support do |_, args|
require 'benchmarks/load'
- Benchmarks::Load.report(File.join(Rake.application.original_dir, 'lib'),
- args.repeats)
+ Benchmarks::Load.report(
+ File.join(Rake.application.original_dir, 'lib'),
+ args.repeats
+ )
end
desc 'Allocation counts'
@@ -115,14 +117,14 @@ namespace :test do
end
namespace :mime do
- desc "Download the current MIME type registrations from IANA."
- task :iana, [ :destination ] => 'support:nokogiri' do |t, args|
+ desc 'Download the current MIME type registrations from IANA.'
+ task :iana, [ :destination ] => 'support:nokogiri' do |_, args|
require 'iana_registry'
IANARegistry.download(to: args.destination)
end
- desc "Download the current MIME type configuration from Apache."
- task :apache, [ :destination ] => 'support:nokogiri' do |t, args|
+ desc 'Download the current MIME type configuration from Apache.'
+ task :apache, [ :destination ] => 'support:nokogiri' do |_, args|
require 'apache_mime_types'
ApacheMIMETypes.download(to: args.destination)
end
@@ -153,26 +155,26 @@ namespace :convert do
end
end
- desc "Convert documentation from RDoc to Markdown"
+ desc 'Convert documentation from RDoc to Markdown'
task docs: 'convert:docs:run'
namespace :yaml do
- desc "Convert from YAML to JSON"
- task :json, [ :source, :destination, :multiple_files ] => :support do |t, args|
+ desc 'Convert from YAML to JSON'
+ task :json, [ :source, :destination, :multiple_files ] => :support do |_, args|
require 'convert'
Convert.from_yaml_to_json(args)
end
- desc "Convert from YAML to Columnar"
- task :columnar, [ :source, :destination ] => :support do |t, args|
+ desc 'Convert from YAML to Columnar'
+ task :columnar, [ :source, :destination ] => :support do |_, args|
require 'convert/columnar'
Convert::Columnar.from_yaml_to_columnar(args)
end
end
namespace :json do
- desc "Convert from JSON to YAML"
- task :yaml, [ :source, :destination, :multiple_files ] => :support do |t, args|
+ desc 'Convert from JSON to YAML'
+ task :yaml, [ :source, :destination, :multiple_files ] => :support do |_, args|
require 'convert'
Convert.from_json_to_yaml(args)
end
@@ -180,6 +182,6 @@ namespace :convert do
end
Rake::Task['travis'].prerequisites.replace(%w(test:coveralls))
-Rake::Task['gem'].prerequisites.unshift("convert:yaml:json")
+Rake::Task['gem'].prerequisites.unshift('convert:yaml:json')
# vim: syntax=ruby
diff --git a/lib/mime.rb b/lib/mime.rb
index 8696894..52bc531 100644
--- a/lib/mime.rb
+++ b/lib/mime.rb
@@ -18,7 +18,7 @@ class << MIME
when :private, :protected
"and will be #{message}"
when nil
- "and will be removed"
+ 'and will be removed'
else
message
end
@@ -30,7 +30,7 @@ class << MIME
# Provide a single warning about this fact in the interim.
def const_missing(name) # :nodoc:
case name.to_s
- when "InvalidContentType"
+ when 'InvalidContentType'
warn_about_moved_constants(name)
MIME::Type.const_get(name.to_sym)
else
@@ -39,7 +39,10 @@ class << MIME
end
private
+
def warn_about_moved_constants(name) # :nodoc:
- warn "#{caller[1]}: MIME::#{name} is deprecated. Use MIME::Type::#{name} instead."
+ warn <<-warning.chomp
+#{caller[1]}: MIME::#{name} is deprecated. Use MIME::Type::#{name} instead.
+ warning
end
end
diff --git a/lib/mime/type.rb b/lib/mime/type.rb
index 352af93..2a86bdc 100644
--- a/lib/mime/type.rb
+++ b/lib/mime/type.rb
@@ -50,8 +50,9 @@ class MIME::Type
end
def to_s
- "Invalid Encoding #{@encoding.inspect} (valid values: #{VALID_ENCODINGS.inspect})."
+ "Invalid Encoding #{@encoding.inspect}"
end
+
# :startdoc:
end
@@ -71,16 +72,17 @@ class MIME::Type
TEXT_ENCODINGS = %w(7bit quoted-printable)
VALID_ENCODINGS = DEFAULT_ENCODINGS + BINARY_ENCODINGS + TEXT_ENCODINGS
- IANA_URL = "http://www.iana.org/assignments/media-types/%s/%s"
- RFC_URL = "http://rfc-editor.org/rfc/rfc%s.txt"
- DRAFT_URL = "http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=%s"
- CONTACT_URL = "http://www.iana.org/assignments/contact-people.htm#%s"
+ IANA_URL = 'http://www.iana.org/assignments/media-types/%s/%s'
+ RFC_URL = 'http://rfc-editor.org/rfc/rfc%s.txt'
+ DRAFT_URL = 'http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=%s' # rubocop:disable Metrics/LineLength
+ CONTACT_URL = 'http://www.iana.org/assignments/contact-people.htm#%s'
# :startdoc:
if respond_to? :private_constant
private_constant :MEDIA_TYPE_RE, :UNREGISTERED_RE, :I18N_RE, :PLATFORM_RE,
- :DEFAULT_ENCODINGS, :BINARY_ENCODINGS, :TEXT_ENCODINGS, :VALID_ENCODINGS,
- :IANA_URL, :RFC_URL, :DRAFT_URL, :CONTACT_URL
+ :DEFAULT_ENCODINGS, :BINARY_ENCODINGS, :TEXT_ENCODINGS,
+ :VALID_ENCODINGS, :IANA_URL, :RFC_URL, :DRAFT_URL,
+ :CONTACT_URL
end
# Builds a MIME::Type object from the +content_type+, a MIME Content Type
@@ -97,33 +99,33 @@ class MIME::Type
#
# Yields the newly constructed +self+ object.
def initialize(content_type) # :yields self:
- @friendly = {}
- self.system = nil
- self.obsolete = false
- self.registered = nil
+ @friendly = {}
+ self.system = nil
+ self.obsolete = false
+ self.registered = nil
self.use_instead = nil
- self.signature = nil
+ self.signature = nil
case content_type
when Hash
init_with(content_type)
when Array
self.content_type = content_type[0]
- self.extensions = content_type[1] || []
+ self.extensions = content_type[1] || []
when MIME::Type
init_with(content_type.to_h)
else
self.content_type = content_type
end
- self.extensions ||= []
- self.docs ||= []
- self.encoding ||= :default
+ self.extensions ||= []
+ self.docs ||= []
+ self.encoding ||= :default
# This value will be deprecated in the future, as it will be an
# alternative view on #xrefs. Silence an unnecessary warning for now by
# assigning directly to the instance variable.
- @references ||= []
- self.xrefs ||= {}
+ @references ||= []
+ self.xrefs ||= {}
yield self if block_given?
end
@@ -281,11 +283,11 @@ class MIME::Type
attr_reader :encoding
def encoding=(enc) # :nodoc:
if DEFAULT_ENCODINGS.include?(enc)
- @encoding = self.default_encoding
+ @encoding = default_encoding
elsif BINARY_ENCODINGS.include?(enc) or TEXT_ENCODINGS.include?(enc)
@encoding = enc
else
- raise InvalidEncoding, enc
+ fail InvalidEncoding, enc
end
end
@@ -303,7 +305,7 @@ class MIME::Type
if os.nil? or os.kind_of?(Regexp)
@system = os
else
- @system = %r|#{os}|
+ @system = %r{#{os}}
end
end
@@ -350,7 +352,7 @@ class MIME::Type
when Hash
@friendly.merge!(lang)
else
- raise ArgumentError
+ fail ArgumentError
end
end
@@ -468,7 +470,7 @@ class MIME::Type
}
when 'person'.freeze
values.map { |data|
- 'http://www.iana.org/assignments/media-types/media-types.xhtml#%s'.freeze % data
+ 'http://www.iana.org/assignments/media-types/media-types.xhtml#%s'.freeze % data # rubocop:disable Metrics/LineLength
}
when 'template'.freeze
values.map { |data|
@@ -519,7 +521,7 @@ class MIME::Type
# formats. This method returns +false+ when the MIME::Type encoding is
# set to <tt>base64</tt>.
def ascii?
- not binary?
+ !binary?
end
# Returns +true+ when the simplified MIME::Type is in the list of known
@@ -538,7 +540,7 @@ class MIME::Type
# mime-types.
def system?(__internal__ = false)
MIME.deprecated(self, __method__) unless __internal__
- not @system.nil?
+ !@system.nil?
end
# Returns +true+ if the MIME::Type is specific to the current operating
@@ -554,7 +556,7 @@ class MIME::Type
# Returns +true+ if the MIME::Type specifies an extension list,
# indicating that it is a complete MIME::Type.
def complete?
- not @extensions.empty?
+ !@extensions.empty?
end
# Returns the MIME::Type as a string.
@@ -636,7 +638,7 @@ class MIME::Type
def init_with(coder)
self.content_type = coder['content-type']
self.docs = coder['docs'] || []
- self.friendly(coder['friendly'] || {})
+ friendly(coder['friendly'] || {})
self.encoding = coder['encoding']
self.extensions = coder['extensions'] || []
self.obsolete = coder['obsolete']
@@ -666,13 +668,13 @@ class MIME::Type
MEDIA_TYPE_RE.match(content_type)
end
- if matchdata
- matchdata.captures.map { |e|
- e.downcase!
- e.gsub!(UNREGISTERED_RE, ''.freeze)
- e
- }.join('/'.freeze)
- end
+ return unless matchdata
+
+ matchdata.captures.map { |e|
+ e.downcase!
+ e.gsub!(UNREGISTERED_RE, ''.freeze)
+ e
+ }.join('/'.freeze)
end
# Converts a provided +content_type+ into a translation key suitable for
@@ -685,14 +687,14 @@ class MIME::Type
MEDIA_TYPE_RE.match(content_type)
end
- if matchdata
- matchdata.captures.map { |e|
- e.downcase!
- e.gsub!(UNREGISTERED_RE, ''.freeze)
- e.gsub!(I18N_RE, '-'.freeze)
- e
- }.join('.'.freeze)
- end
+ return unless matchdata
+
+ matchdata.captures.map { |e|
+ e.downcase!
+ e.gsub!(UNREGISTERED_RE, ''.freeze)
+ e.gsub!(I18N_RE, '-'.freeze)
+ e
+ }.join('.'.freeze)
end
# Creates a MIME::Type from an +args+ array in the form of:
@@ -721,7 +723,8 @@ class MIME::Type
args = args.first if args.first.kind_of? Array
unless args.size.between?(1, 8)
- raise ArgumentError, "Array provided must contain between one and eight elements."
+ fail ArgumentError,
+ 'Array provided must contain between one and eight elements.'
end
MIME::Type.new(args.shift) do |t|
@@ -801,9 +804,10 @@ class MIME::Type
end
private
+
def content_type=(type_string)
match = MEDIA_TYPE_RE.match(type_string)
- raise InvalidContentType, type_string if match.nil?
+ fail InvalidContentType, type_string if match.nil?
@content_type = type_string
@raw_media_type, @raw_sub_type = match.captures
diff --git a/lib/mime/types.rb b/lib/mime/types.rb
index e12d8cc..d8e7c0b 100644
--- a/lib/mime/types.rb
+++ b/lib/mime/types.rb
@@ -140,7 +140,7 @@ class MIME::Types
# platform) is currently supported but deprecated.
def [](type_id, flags = {})
if flags[:platform]
- MIME.deprecated(self, __method__, "using the :platform flag")
+ MIME.deprecated(self, __method__, 'using the :platform flag')
end
matches = case type_id
@@ -176,8 +176,7 @@ class MIME::Types
}.compact.sort { |a, b| a.priority_compare(b) }.uniq
if platform
- MIME.deprecated(self, __method__,
- "using the platform parameter")
+ MIME.deprecated(self, __method__, 'using the platform parameter')
types.select(&:platform?)
else
types
@@ -279,6 +278,7 @@ class MIME::Types
end
private
+
def lazy_load?
(lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
end
@@ -293,7 +293,7 @@ class MIME::Types
end
end
- def load_default_mime_types(mode = load_mode())
+ def load_default_mime_types(mode = load_mode()) # rubocop:disable Metrics/LineLength, Style/MethodCallParentheses
@__types__ = MIME::Types::Cache.load
unless @__types__
@__types__ = MIME::Types::Loader.load(mode)
@@ -304,6 +304,7 @@ class MIME::Types
end
private
+
def add_type_variant!(mime_type)
@type_variants[mime_type.simplified] << mime_type
end
@@ -313,14 +314,14 @@ class MIME::Types
end
def prune_matches(matches, flags)
- matches.delete_if { |e| not e.complete? } if flags[:complete]
- matches.delete_if { |e| not e.platform? } if flags[:platform]
- matches.delete_if { |e| not e.registered? } if flags[:registered]
+ matches.delete_if { |e| !e.complete? } if flags[:complete]
+ matches.delete_if { |e| !e.platform? } if flags[:platform]
+ matches.delete_if { |e| !e.registered? } if flags[:registered]
matches
end
def match(pattern)
- @type_variants.select { |k, v| k =~ pattern }.values.flatten
+ @type_variants.select { |k, _| k =~ pattern }.values.flatten
end
load_default_mime_types(load_mode) unless lazy_load?
diff --git a/lib/mime/types/cache.rb b/lib/mime/types/cache.rb
index c2e8dce..8b5bbe8 100644
--- a/lib/mime/types/cache.rb
+++ b/lib/mime/types/cache.rb
@@ -16,14 +16,14 @@ class MIME::Types
# 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 = cache_file || ENV['RUBY_MIME_TYPES_CACHE']
+ cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
return nil unless cache_file and File.exist?(cache_file)
cache = Marshal.load(File.binread(cache_file))
if cache.version == MIME::Types::VERSION
Marshal.load(cache.data)
else
- warn "Could not load MIME::Types cache: invalid version"
+ warn 'Could not load MIME::Types cache: invalid version'
nil
end
rescue => e
@@ -41,10 +41,10 @@ class MIME::Types
# +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 = cache_file || ENV['RUBY_MIME_TYPES_CACHE']
+ cache_file ||= ENV['RUBY_MIME_TYPES_CACHE']
return nil unless cache_file
- types = types || MIME::Types.send(:__types__)
+ types ||= MIME::Types.send(:__types__)
File.open(cache_file, 'wb') do |f|
f.write(Marshal.dump(new(types.data_version, Marshal.dump(types))))
diff --git a/lib/mime/types/columnar.rb b/lib/mime/types/columnar.rb
index 7b63651..883a7aa 100644
--- a/lib/mime/types/columnar.rb
+++ b/lib/mime/types/columnar.rb
@@ -49,7 +49,7 @@ module MIME::Types::Columnar
line.chomp!
if lookup
- next unless type = @__mime_data__[i += 1]
+ type = @__mime_data__[i += 1] or next
yield type, line
else
yield line
@@ -117,26 +117,19 @@ module MIME::Types::Columnar
end
def load_use_instead
- empty = '-'
- pipe = '|'
each_file_line('use_instead') do |type, line|
type.use_instad = (line unless line == '-'.freeze)
end
end
def dict(line)
- empty = '-'.freeze
- pipe = '|'.freeze
- caret = '^'.freeze
-
- if line == empty
+ if line == '-'.freeze
{}
else
- line.split(pipe).inject({}) { |h, l|
- k, v = line.split(caret)
+ line.split('|'.freeze).each_with_object({}) { |h, l|
+ k, v = l.split('^'.freeze)
v = [ nil ] if v.empty?
h[k] = v
- h
}
end
end
diff --git a/lib/mime/types/loader.rb b/lib/mime/types/loader.rb
index 7833a2a..0cf7546 100644
--- a/lib/mime/types/loader.rb
+++ b/lib/mime/types/loader.rb
@@ -166,7 +166,7 @@ class MIME::Types::Loader
#{filename}:#{index + 1}: Parsing error in v1 MIME type definition.
=> #{line}
EOS
- raise BadV1Format, line
+ fail BadV1Format, line
end
unregistered, obsolete, platform, mediatype, subtype, extensions,
@@ -181,7 +181,7 @@ class MIME::Types::Loader
use_instead = nil
else
use_instead = docs.scan(%r{use-instead:(\S+)}).flatten.first
- docs = docs.gsub(%r{use-instead:\S+}, "").squeeze(" \t")
+ docs = docs.gsub(%r{use-instead:\S+}, '').squeeze(' \t')
end
mime_type = MIME::Type.new("#{mediatype}/#{subtype}") do |t|
@@ -195,7 +195,7 @@ class MIME::Types::Loader
# This is being removed. Cheat to silence it for now.
t.instance_variable_set :@references,
- Array(urls).flatten.compact.uniq
+ Array(urls).flatten.compact.uniq
end
mime.add_type(mime_type, true)
@@ -233,12 +233,14 @@ class MIME::Types::Loader
end
private
+
def read_file(filename)
File.open(filename, 'r:UTF-8:-') { |f| f.read }
end
end
private
+
def yaml_path
File.join(path, '*.y{,a}ml')
end
diff --git a/support/apache_mime_types.rb b/support/apache_mime_types.rb
index 1786fa4..3a3a11b 100644
--- a/support/apache_mime_types.rb
+++ b/support/apache_mime_types.rb
@@ -25,7 +25,7 @@ class ApacheMIMETypes
dest = Pathname(options[:to] || DEFAULTS[:to]).expand_path
url = options.fetch(:url, DEFAULTS[:url])
- puts "Downloading Apache MIME type list."
+ puts 'Downloading Apache MIME type list.'
puts "\t#{url}"
data = open(url) { |f| f.read }.split($/)
data.delete_if { |line| line =~ /\A#/ }
@@ -91,6 +91,7 @@ class ApacheMIMETypes
end
private
+
def mime_types_for(file)
if file.exist?
MIME::Types::Loader.load_from_yaml(file)
diff --git a/support/benchmarks/load.rb b/support/benchmarks/load.rb
index 91c9ae3..3dd23c8 100644
--- a/support/benchmarks/load.rb
+++ b/support/benchmarks/load.rb
@@ -15,7 +15,7 @@ module Benchmarks
@load_path = load_path
end
- def reload_mime_types(repeats = 1, options = { force_load: false, columnar: false })
+ def reload_mime_types(repeats = 1, options = {})
force_load = options.fetch(:force_load, false)
columnar = options.fetch(:columnar, false)
@@ -36,22 +36,22 @@ module Benchmarks
remove_cache
Benchmark.bm(17) do |mark|
- mark.report("Normal:") { reload_mime_types(@repeats) }
- mark.report("Columnar:") { reload_mime_types(@repeats, columnar: true) }
+ mark.report('Normal:') { reload_mime_types(@repeats) }
+ mark.report('Columnar:') { reload_mime_types(@repeats, columnar: true) }
ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
- mark.report("Lazy:") { reload_mime_types(@repeats) }
- mark.report("Lazy+Load:") { reload_mime_types(@repeats, force_load: true) }
+ mark.report('Lazy:') { reload_mime_types(@repeats) }
+ mark.report('Lazy+Load:') { reload_mime_types(@repeats, force_load: true) }
ENV.delete('RUBY_MIME_TYPES_LAZY_LOAD')
ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
reload_mime_types
- mark.report("Cached:") { reload_mime_types(@repeats) }
+ mark.report('Cached:') { reload_mime_types(@repeats) }
ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
- mark.report("Lazy Cached:") { reload_mime_types(@repeats) }
- mark.report("Lazy Cached Load:") { reload_mime_types(@repeats, force_load: true) }
+ mark.report('Lazy Cached:') { reload_mime_types(@repeats) }
+ mark.report('Lazy Cached Load:') { reload_mime_types(@repeats, force_load: true) }
end
ensure
remove_cache
diff --git a/support/benchmarks/load_allocations.rb b/support/benchmarks/load_allocations.rb
index b0f9768..39e1f3a 100644
--- a/support/benchmarks/load_allocations.rb
+++ b/support/benchmarks/load_allocations.rb
@@ -24,11 +24,10 @@ module Benchmarks
@mime_types_only = !!mime_types_only
@top_x = top_x
- if @top_x
- @top_x = top_x.to_i
- @top_x = 10 if @top_x <= 0
- end
+ return unless @top_x
+ @top_x = top_x.to_i
+ @top_x = 10 if @top_x <= 0
end
def report
@@ -38,6 +37,7 @@ module Benchmarks
end
private
+
def report_top_x
table = @allocations.sort_by { |_, v| v.first }.reverse.first(@top_x)
table.map! { |(location, allocs)|
@@ -58,8 +58,8 @@ module Benchmarks
end
end
- pattern = [ "%%-%ds" ]
- pattern << ([ "%% %ds" ] * (max_widths.length - 1))
+ pattern = [ '%%-%ds' ]
+ pattern << ([ '%% %ds' ] * (max_widths.length - 1))
pattern = pattern.join("\t") % max_widths
table.each { |row| puts pattern % row }
puts
diff --git a/support/benchmarks/object_counts.rb b/support/benchmarks/object_counts.rb
index 4ed9c37..0d13f8d 100644
--- a/support/benchmarks/object_counts.rb
+++ b/support/benchmarks/object_counts.rb
@@ -14,8 +14,8 @@ module Benchmarks
collect
@before.keys.grep(/T_/).map { |key|
[ key, @after[key] - @before[key] ]
- }.sort_by { |key, delta| -delta }.each { |key, delta|
- puts "%10s +%6d" % [ key, delta ]
+ }.sort_by { |_, delta| -delta }.each { |key, delta|
+ puts '%10s +%6d' % [ key, delta ]
}
end
diff --git a/support/convert.rb b/support/convert.rb
index 02e37d0..849bbd4 100644
--- a/support/convert.rb
+++ b/support/convert.rb
@@ -29,7 +29,7 @@ class Convert
to_json(
destination: json_path(args.destination),
multiple_files: multiple_files(args.multiple_files || 'single')
- )
+ )
end
# Converts from JSON to YAML. Defaults to converting to multiple files.
@@ -38,12 +38,13 @@ class Convert
to_yaml(
destination: yaml_path(args.destination),
multiple_files: multiple_files(args.multiple_files || 'multiple')
- )
+ )
end
private :new
private
+
def yaml_path(path)
path_or_default(path, 'type-lists'.freeze)
end
@@ -72,10 +73,9 @@ class Convert
def initialize(options = {})
if options[:path].nil? or options[:path].empty?
- raise ArgumentError, ':path is required'
- end
- if options[:from].nil? or options[:from].empty?
- raise ArgumentError, ':from is required'
+ fail ArgumentError, ':path is required'
+ elsif options[:from].nil? or options[:from].empty?
+ fail ArgumentError, ':from is required'
end
@loader = MIME::Types::Loader.new(options[:path])
@@ -95,6 +95,7 @@ class Convert
end
private
+
def load_from(source_type)
method = :"load_#{source_type}"
@loader.send(method)
@@ -140,8 +141,8 @@ class Convert
end
def must_be_directory!(path)
- if File.exist?(path) and not File.directory?(path)
- raise ArgumentError, 'Cannot write multiple files to a file.'
+ if File.exist?(path) and !File.directory?(path)
+ fail ArgumentError, 'Cannot write multiple files to a file.'
end
FileUtils.mkdir_p(path) unless File.exist?(path)
diff --git a/support/iana_registry.rb b/support/iana_registry.rb
index 9aaab3a..a781aa0 100644
--- a/support/iana_registry.rb
+++ b/support/iana_registry.rb
@@ -21,7 +21,7 @@ class IANARegistry
dest = Pathname(options[:to] || DEFAULTS[:to]).expand_path
url = options.fetch(:url, DEFAULTS[:url])
- puts "Downloading IANA MIME type assignments."
+ puts 'Downloading IANA MIME type assignments.'
puts "\t#{url}"
xml = Nokogiri::XML(open(url) { |f| f.read })
@@ -48,7 +48,7 @@ class IANARegistry
yield self if block_given?
end
- ASSIGNMENT_FILE_REF = "{%s=http://www.iana.org/assignments/media-types/%s}"
+ ASSIGNMENT_FILE_REF = '{%s=http://www.iana.org/assignments/media-types/%s}'
def parse
@registry.css('record').each do |record|
@@ -63,9 +63,11 @@ class IANARegistry
subtype, notes = subtype.split(/ /, 2)
- refs, xrefs = parse_refs_and_files(record.css('xref'),
- record.css('file'),
- subtype)
+ refs, xrefs = parse_refs_and_files(
+ record.css('xref'),
+ record.css('file'),
+ subtype
+ )
xrefs['notes'] << notes if notes
@@ -102,6 +104,7 @@ class IANARegistry
end
private
+
def mime_types_for(file)
if file.exist?
MIME::Types::Loader.load_from_yaml(file)
@@ -115,7 +118,8 @@ class IANARegistry
r = []
refs.each do |xref|
- type, data = xref["type"], xref["data"]
+ type = xref['type']
+ data = xref['data']
r << ref_from_type(type, data)
@@ -129,11 +133,11 @@ class IANARegistry
file.text
end
- if file["type"] == "template"
+ if file['type'] == 'template'
r << (ASSIGNMENT_FILE_REF % [ file_name, file_name ])
end
- xr[file["type"]] << file_name
+ xr[file['type']] << file_name
end
[ r, xr ]
diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb
index 63ee66a..d0f5d2d 100644
--- a/test/minitest_helper.rb
+++ b/test/minitest_helper.rb
@@ -8,12 +8,13 @@ require 'minitest/autorun'
require 'minitest/focus'
module Minitest::MIMEDeprecated
- def assert_deprecated(name, message = "and will be removed")
- assert_output nil,
- /#{Regexp.escape(name)} is deprecated #{Regexp.escape(message)}./ do
+ def assert_deprecated name, message = 'and will be removed'
+ name = Regexp.escape(name)
+ message = Regexp.escape(message)
+
+ assert_output nil, /#{name} is deprecated #{message}./ do
yield
end
- ensure
end
Minitest::Test.send(:include, self)
diff --git a/test/test_mime_type.rb b/test/test_mime_type.rb
index 906ad2b..e6f23b2 100644
--- a/test/test_mime_type.rb
+++ b/test/test_mime_type.rb
@@ -46,15 +46,19 @@ class TestMIMEType < Minitest::Test
def setup
@applzip = MIME::Type.new('x-appl/x-zip') { |t|
- t.extensions = ['zip', 'zp']
+ t.extensions = %w(zip zp)
}
end
def test_class_from_array
yaml = nil
assert_deprecated('MIME::Type.from_array') do
- yaml = MIME::Type.from_array('text/x-yaml', %w(yaml yml), '8bit',
- 'd9d172f608')
+ yaml = MIME::Type.from_array(
+ 'text/x-yaml',
+ %w(yaml yml),
+ '8bit',
+ 'd9d172f608'
+ )
end
assert_instance_of(MIME::Type, yaml)
assert_equal('text/yaml', yaml.simplified)
@@ -106,7 +110,7 @@ class TestMIMEType < Minitest::Test
end
def test_spaceship_compare # '<=>'
- assert(MIME::Type.new('text/plain') == MIME::Type.new('text/plain'))
+ assert(MIME::Type.new('text/plain') == MIME::Type.new('text/plain')) # rubocop:disable Lint/UselessComparison
assert(MIME::Type.new('text/plain') != MIME::Type.new('image/jpeg'))
assert(MIME::Type.new('text/plain') == 'text/plain')
assert(MIME::Type.new('text/plain') != 'image/jpeg')
@@ -152,7 +156,7 @@ class TestMIMEType < Minitest::Test
assert_equal('text/vCard', MIME::Type.new('text/vCard').content_type)
assert_equal('application/pkcs7-mime',
MIME::Type.new('application/pkcs7-mime').content_type)
- assert_equal('x-appl/x-zip', @applzip.content_type);
+ assert_equal('x-appl/x-zip', @applzip.content_type)
assert_equal('base64', @applzip.encoding)
end
@@ -446,7 +450,7 @@ class TestMIMEType < Minitest::Test
def assert_type_has_keys(type, *keys)
hash = type.to_h
- keys.flatten.each { |key| assert(hash.has_key?(key)) }
+ keys.flatten.each { |key| assert(hash.key?(key)) }
end
def test_to_h
@@ -455,8 +459,10 @@ class TestMIMEType < Minitest::Test
assert_type_has_keys(make(t) { |v| v.docs = 'Something' }, 'docs')
assert_type_has_keys(make(t) { |v| v.extensions = %w(b) }, 'extensions')
assert_type_has_keys(make(t) { |v| v.obsolete = true }, 'obsolete')
- assert_type_has_keys(make(t) { |v| v.obsolete = true; v.use_instead = 'c/d' },
- 'obsolete', 'use-instead')
+ assert_type_has_keys(make(t) { |v|
+ v.obsolete = true
+ v.use_instead = 'c/d'
+ }, 'obsolete', 'use-instead')
assert_type_has_keys(make(t) { |v|
assert_deprecated('MIME::Type#references=') { v.references = 'IANA' }
}, 'references')
@@ -484,7 +490,7 @@ class TestMIMEType < Minitest::Test
begin
MIME::Type.new(nil)
rescue MIME::Type::InvalidContentType => ex
- assert_equal("Invalid Content-Type nil", ex.message)
+ assert_equal('Invalid Content-Type nil', ex.message)
end
end
@@ -501,17 +507,17 @@ class TestMIMEType < Minitest::Test
def test_references_equals
yaml = make_yaml_mime_type
assert_deprecated('MIME::Type#references=') do
- yaml.references = "IANA"
+ yaml.references = 'IANA'
end
assert_deprecated('MIME::Type#references') do
- assert_equal(%W(IANA), yaml.references)
+ assert_equal(%w(IANA), yaml.references)
end
assert_deprecated('MIME::Type#references=') do
yaml.references = %w(IANA IANA)
end
assert_deprecated('MIME::Type#references') do
- assert_equal(%W(IANA), yaml.references)
+ assert_equal(%w(IANA), yaml.references)
end
end
@@ -527,23 +533,23 @@ class TestMIMEType < Minitest::Test
def test_xref_urls
js = make_javascript do |j|
- j.xrefs = j.xrefs.merge({
+ j.xrefs = j.xrefs.merge(
'draft' => [ 'RFC-ietf-appsawg-json-merge-patch-07' ],
'person' => [ 'David_Singer' ],
'rfc-errata' => [ '3245' ],
'uri' => [ 'http://exmple.org' ],
'text' => [ 'text' ]
- })
+ )
end
assert_equal(
[
- "http://www.iana.org/go/rfc4239",
- "http://www.iana.org/assignments/media-types/application/javascript",
- "http://www.iana.org/go/draft-ietf-appsawg-json-merge-patch-07",
- "http://www.iana.org/assignments/media-types/media-types.xhtml#David_Singer",
- "http://www.rfc-editor.org/errata_search.php?eid=3245",
- "http://exmple.org",
- "text"
+ 'http://www.iana.org/go/rfc4239',
+ 'http://www.iana.org/assignments/media-types/application/javascript',
+ 'http://www.iana.org/go/draft-ietf-appsawg-json-merge-patch-07',
+ 'http://www.iana.org/assignments/media-types/media-types.xhtml#David_Singer',
+ 'http://www.rfc-editor.org/errata_search.php?eid=3245',
+ 'http://exmple.org',
+ 'text'
],
js.xref_urls
)
@@ -558,10 +564,10 @@ class TestMIMEType < Minitest::Test
def test_url_equals
yaml = make_yaml_mime_type
assert_deprecated('MIME::Type#url=') do
- yaml.url = "IANA"
+ yaml.url = 'IANA'
end
assert_deprecated('MIME::Type#url') do
- assert_equal(%W(IANA), yaml.url)
+ assert_equal(%w(IANA), yaml.url)
end
end
@@ -578,10 +584,10 @@ class TestMIMEType < Minitest::Test
assert_deprecated('MIME::Type#urls') do
assert_equal(
%w(
- http://www.iana.org/assignments/media-types/text/yaml
- http://rfc-editor.org/rfc/rfc123.txt
- http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=xyz
- http://www.iana.org/assignments/contact-people.htm#abc
+ http://www.iana.org/assignments/media-types/text/yaml
+ http://rfc-editor.org/rfc/rfc123.txt
+ http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=xyz
+ http://www.iana.org/assignments/contact-people.htm#abc
),
yaml.urls
)
@@ -639,7 +645,7 @@ class TestMIMEType < Minitest::Test
end
def test_friendly_set
- assert_equal({ 'en' => 'Zip' }, @applzip.friendly([ 'en', 'Zip' ]))
+ assert_equal({ 'en' => 'Zip' }, @applzip.friendly(%w(en Zip)))
assert_equal({ 'en' => 'Zip Archive' }, @applzip.friendly('en' => 'Zip Archive'))
end
diff --git a/test/test_mime_types.rb b/test/test_mime_types.rb
index 1c7e31a..db3776d 100644
--- a/test/test_mime_types.rb
+++ b/test/test_mime_types.rb
@@ -11,7 +11,7 @@ class TestMIMETypes < Minitest::Test
MIME::Type.new('application/x-wordperfect6.1'),
MIME::Type.new('content-type' => 'application/x-www-form-urlencoded', 'registered' => true),
MIME::Type.new(['application/x-gzip', %w(gz)]),
- MIME::Type.new(['application/gzip', %W(gz)]))
+ MIME::Type.new(['application/gzip', %w(gz)]))
end
def test_enumerable
@@ -52,15 +52,15 @@ class TestMIMETypes < Minitest::Test
end
def test_index_with_platform_flag
- assert_deprecated("MIME::Types#[]", "using the :platform flag") do
+ assert_deprecated('MIME::Types#[]', 'using the :platform flag') do
assert_empty(MIME::Types['text/plain', platform: true])
end
end
def test_add
- eruby = MIME::Type.new("application/x-eruby") do |t|
- t.extensions = "rhtml"
- t.encoding = "8bit"
+ eruby = MIME::Type.new('application/x-eruby') do |t|
+ t.extensions = 'rhtml'
+ t.encoding = '8bit'
end
@mime_types.add(eruby)
@@ -74,11 +74,11 @@ class TestMIMETypes < Minitest::Test
assert_equal(%w(image/jpeg), MIME::Types.of('foo.jpeg'))
assert_equal(%w(image/jpeg text/plain),
MIME::Types.type_for(%w(foo.txt foo.jpeg)))
- assert_deprecated("MIME::Types#type_for", "using the platform parameter") do
+ assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
assert_equal(@mime_types.of('gif', true), @mime_types['image/gif'])
end
assert_empty(MIME::Types.type_for('coverallsjson'))
- assert_deprecated("MIME::Types#type_for", "using the platform parameter") do
+ assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
assert_empty(MIME::Types.type_for('jpeg', true))
end
assert_empty(@mime_types.type_for('zzz'))
@@ -91,7 +91,7 @@ class TestMIMETypes < Minitest::Test
# This tests the instance implementation through the class implementation.
def test_add_type_variant
xtxp = MIME::Type.new('x-text/x-plain')
- assert_deprecated("MIME::Types#add_type_variant", "and will be private") do
+ assert_deprecated('MIME::Types#add_type_variant', 'and will be private') do
@mime_types.add_type_variant(xtxp)
end
assert_includes(@mime_types['text/plain'], xtxp)
@@ -104,17 +104,17 @@ class TestMIMETypes < Minitest::Test
# This tests the instance implementation through the class implementation.
def test_index_extensions
xtxp = MIME::Type.new(['x-text/x-plain', %w(tzt)])
- assert_deprecated("MIME::Types#index_extensions", "and will be private") do
+ assert_deprecated('MIME::Types#index_extensions', 'and will be private') do
@mime_types.index_extensions(xtxp)
end
assert_includes(@mime_types.of('tzt'), xtxp)
end
def test_defined_types
- assert_deprecated("MIME::Types#defined_types") do
+ assert_deprecated('MIME::Types#defined_types') do
assert_empty(MIME::Types.new.defined_types)
end
- assert_deprecated("MIME::Types#defined_types") do
+ assert_deprecated('MIME::Types#defined_types') do
refute_empty(@mime_types.defined_types)
end
end
diff --git a/test/test_mime_types_class.rb b/test/test_mime_types_class.rb
index 359636c..5772c26 100644
--- a/test/test_mime_types_class.rb
+++ b/test/test_mime_types_class.rb
@@ -16,7 +16,7 @@ class TestMIMETypesQueryClassMethods < Minitest::Test
def test_load_from_file
fn = File.expand_path('../fixture/old-data', __FILE__)
- assert_deprecated("MIME::Types.load_from_file") do
+ assert_deprecated('MIME::Types.load_from_file') do
MIME::Types.load_from_file(fn)
end
end
@@ -53,7 +53,7 @@ class TestMIMETypesQueryClassMethods < Minitest::Test
end
def test_index_with_platform_flag
- assert_deprecated("MIME::Types#[]", "using the :platform flag") do
+ assert_deprecated('MIME::Types#[]', 'using the :platform flag') do
assert_empty(MIME::Types['text/plain', platform: true])
end
end
@@ -63,7 +63,7 @@ class TestMIMETypesQueryClassMethods < Minitest::Test
assert_equal(%w(image/gif), MIME::Types.of('foo.gif'))
assert_equal(%w(application/xml image/gif text/xml),
MIME::Types.type_for(%w(xml gif)))
- assert_deprecated("MIME::Types#type_for", "using the platform parameter") do
+ assert_deprecated('MIME::Types#type_for', 'using the platform parameter') do
assert_empty(MIME::Types.type_for('gif', true))
end
assert_empty(MIME::Types.type_for('zzz'))
@@ -71,16 +71,16 @@ class TestMIMETypesQueryClassMethods < Minitest::Test
def test_count
assert(MIME::Types.count.nonzero?,
- "A lot of types are expected to be known.")
+ 'A lot of types are expected to be known.')
end
def test_cache_file
ENV['RUBY_MIME_TYPES_CACHE'] = 'foo'
- assert_deprecated("MIME::Types.cache_file") do
+ assert_deprecated('MIME::Types.cache_file') do
assert_equal('foo', MIME::Types.cache_file)
end
ENV.delete('RUBY_MIME_TYPES_CACHE')
- assert_deprecated("MIME::Types.cache_file") do
+ assert_deprecated('MIME::Types.cache_file') do
assert_nil(MIME::Types.cache_file)
end
end
@@ -93,37 +93,37 @@ class TestMIMETypesClassMethods < Minitest::Test
end
def test_add_with_type
- MIME::Types.add(MIME::Type.new("application/x-eruby"))
+ MIME::Types.add(MIME::Type.new('application/x-eruby'))
refute_empty(MIME::Types['application/x-eruby'])
end
def test_add_with_types
mt = MIME::Types.new
- mt.add MIME::Type.new("application/x-eruby")
+ mt.add MIME::Type.new('application/x-eruby')
MIME::Types.add(mt)
refute_empty(MIME::Types['application/x-eruby'])
end
def test_add_with_array
- MIME::Types.add([MIME::Type.new("application/x-eruby")])
+ MIME::Types.add([MIME::Type.new('application/x-eruby')])
refute_empty(MIME::Types['application/x-eruby'])
end
def test_add_with_noise_suppression
assert_silent do
- MIME::Types.add(MIME::Type.new("application/x-eruby"))
+ MIME::Types.add(MIME::Type.new('application/x-eruby'))
end
assert_output(nil, %r{application/x-eruby is already registered}) do
- MIME::Types.add(MIME::Type.new("application/x-eruby"))
+ MIME::Types.add(MIME::Type.new('application/x-eruby'))
end
assert_silent do
- MIME::Types.add(MIME::Type.new("application/x-eruby"), :silent)
+ MIME::Types.add(MIME::Type.new('application/x-eruby'), :silent)
end
end
def test_add_type_variant
xtxp = MIME::Type.new('x-text/x-plain')
- assert_deprecated("MIME::Types#add_type_variant", "and will be private") do
+ assert_deprecated('MIME::Types#add_type_variant', 'and will be private') do
MIME::Types.add_type_variant(xtxp)
end
assert_includes(MIME::Types['text/plain'], xtxp)
@@ -131,7 +131,7 @@ class TestMIMETypesClassMethods < Minitest::Test
def test_index_extensions
xtxp = MIME::Type.new(['x-text/x-plain', %w(tzt)])
- assert_deprecated("MIME::Types#index_extensions", "and will be private") do
+ assert_deprecated('MIME::Types#index_extensions', 'and will be private') do
MIME::Types.index_extensions(xtxp)
end
assert_includes(MIME::Types.of('tzt'), xtxp)
diff --git a/test/test_mime_types_loader.rb b/test/test_mime_types_loader.rb
index 5733762..d088743 100644
--- a/test/test_mime_types_loader.rb
+++ b/test/test_mime_types_loader.rb
@@ -25,10 +25,10 @@ class TestMIMETypesLoader < Minitest::Test
assert_equal('Fixes a bug with IE6 and progressive JPEGs',
types['image/pjpeg'].first.docs)
- assert_deprecated("MIME::Type#system?") do
+ assert_deprecated('MIME::Type#system?') do
assert(types['application/x-apple-diskimage'].first.system?)
end
- assert_deprecated("MIME::Type#system") do
+ assert_deprecated('MIME::Type#system') do
assert_equal(/mac/, types['application/x-apple-diskimage'].first.system)
end
@@ -45,7 +45,7 @@ class TestMIMETypesLoader < Minitest::Test
end
def test_load_v1
- assert_deprecated("MIME::Types::Loader.load_v1") do
+ assert_deprecated('MIME::Types::Loader.load_v1') do
assert_correctly_loaded(@loader.load_v1)
end
end