mime-types 3.2.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'set'
4
- require 'forwardable'
3
+ require "set"
4
+ require "forwardable"
5
5
 
6
6
  # MIME::Types requires a serializable keyed container that returns an empty Set
7
7
  # on a key miss. Hash#default_value cannot be used because, while it traverses
@@ -9,7 +9,7 @@ require 'forwardable'
9
9
  # format (plus, a default of a mutable object resuls in a shared mess).
10
10
  # Hash#default_proc cannot be used without a wrapper because it prevents
11
11
  # Marshal serialization (and doesn't survive the round-trip).
12
- class MIME::Types::Container #:nodoc:
12
+ class MIME::Types::Container # :nodoc:
13
13
  extend Forwardable
14
14
 
15
15
  def initialize(hash = {})
@@ -22,12 +22,13 @@ class MIME::Types::Container #:nodoc:
22
22
  end
23
23
 
24
24
  def []=(key, value)
25
- case value
26
- when Set
27
- container[key] = value
28
- else
29
- container[key] = Set[*value]
30
- end
25
+ container[key] =
26
+ case value
27
+ when Set
28
+ value
29
+ else
30
+ Set[*value]
31
+ end
31
32
  end
32
33
 
33
34
  def merge(other)
@@ -36,8 +37,8 @@ class MIME::Types::Container #:nodoc:
36
37
 
37
38
  def merge!(other)
38
39
  tap {
39
- other = other.kind_of?(MIME::Types::Container) ? other.container : other
40
- self.container.merge!(other)
40
+ other = other.is_a?(MIME::Types::Container) ? other.container : other
41
+ container.merge!(other)
41
42
  normalize
42
43
  }
43
44
  end
@@ -70,7 +71,6 @@ class MIME::Types::Container #:nodoc:
70
71
  end
71
72
 
72
73
  def encode_with(coder)
73
- debugger
74
74
  container.each { |k, v| coder[k] = v.to_a }
75
75
  end
76
76
 
@@ -85,7 +85,8 @@ class MIME::Types::Container #:nodoc:
85
85
 
86
86
  def normalize
87
87
  container.each do |k, v|
88
- next if v.kind_of?(Set)
88
+ next if v.is_a?(Set)
89
+
89
90
  container[k] = Set[*v]
90
91
  end
91
92
  end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- ruby encoding: utf-8 -*-
4
-
5
- require 'mime/types/logger'
3
+ require "mime/types/logger"
6
4
 
7
5
  # The namespace for MIME applications, tools, and libraries.
8
6
  module MIME
@@ -10,25 +8,29 @@ module MIME
10
8
  class Types
11
9
  # Used to mark a method as deprecated in the mime-types interface.
12
10
  def self.deprecated(klass, sym, message = nil, &block) # :nodoc:
13
- level = case klass
11
+ level =
12
+ case klass
14
13
  when Class, Module
15
- '.'
14
+ "."
16
15
  else
17
16
  klass = klass.class
18
- '#'
19
- end
20
- message = case message
17
+ "#"
18
+ end
19
+ message =
20
+ case message
21
21
  when :private, :protected
22
22
  "and will be #{message}"
23
23
  when nil
24
- 'and will be removed'
24
+ "and will be removed"
25
25
  else
26
26
  message
27
- end
28
- MIME::Types.logger.warn <<-WARNING.chomp
29
- #{caller[1]}: #{klass}#{level}#{sym} is deprecated #{message}.
27
+ end
28
+ MIME::Types.logger.warn <<~WARNING.chomp
29
+ #{caller(2..2).first}: #{klass}#{level}#{sym} is deprecated #{message}.
30
30
  WARNING
31
- block.call if block
31
+
32
+ return unless block
33
+ block.call
32
34
  end
33
35
  end
34
36
  end
@@ -9,11 +9,11 @@ module MIME
9
9
  private
10
10
 
11
11
  def load_mode
12
- { columnar: false }
12
+ {columnar: false}
13
13
  end
14
14
  end
15
15
  end
16
16
  end
17
17
  end
18
18
 
19
- require 'mime/types'
19
+ require "mime/types"
@@ -1,13 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- ruby encoding: utf-8 -*-
4
-
5
3
  ##
6
4
  module MIME; end
5
+
7
6
  ##
8
7
  class MIME::Types; end
9
8
 
10
- require 'mime/types/data'
9
+ require "mime/types/data"
11
10
 
12
11
  # This class is responsible for initializing the MIME::Types registry from
13
12
  # the data files supplied with the mime-types library.
@@ -30,7 +29,7 @@ class MIME::Types::Loader
30
29
  # Creates a Loader object that can be used to load MIME::Types registries
31
30
  # into memory, using YAML, JSON, or Columnar registry format loaders.
32
31
  def initialize(path = nil, container = nil)
33
- path = path || ENV['RUBY_MIME_TYPES_DATA'] || MIME::Types::Data::PATH
32
+ path = path || ENV["RUBY_MIME_TYPES_DATA"] || MIME::Types::Data::PATH
34
33
  @container = container || MIME::Types.new
35
34
  @path = File.expand_path(path)
36
35
  end
@@ -68,7 +67,7 @@ class MIME::Types::Loader
68
67
  # Loads a MIME::Types registry from columnar files recursively found in
69
68
  # +path+.
70
69
  def load_columnar
71
- require 'mime/types/columnar' unless defined?(MIME::Types::Columnar)
70
+ require "mime/types/columnar" unless defined?(MIME::Types::Columnar)
72
71
  container.extend(MIME::Types::Columnar)
73
72
  container.load_base_data(path)
74
73
 
@@ -80,7 +79,7 @@ class MIME::Types::Loader
80
79
  #
81
80
  # This will load from columnar files (#load_columnar) if <tt>columnar:
82
81
  # true</tt> is provided in +options+ and there are columnar files in +path+.
83
- def load(options = { columnar: false })
82
+ def load(options = {columnar: false})
84
83
  if options[:columnar] && !Dir[columnar_path].empty?
85
84
  load_columnar
86
85
  else
@@ -90,7 +89,7 @@ class MIME::Types::Loader
90
89
 
91
90
  class << self
92
91
  # Loads the default MIME::Type registry.
93
- def load(options = { columnar: false })
92
+ def load(options = {columnar: false})
94
93
  new.load(options)
95
94
  end
96
95
 
@@ -105,12 +104,18 @@ class MIME::Types::Loader
105
104
  # NOTE: The purpose of this format is purely for maintenance reasons.
106
105
  def load_from_yaml(filename)
107
106
  begin
108
- require 'psych'
107
+ require "psych"
109
108
  rescue LoadError
110
109
  nil
111
110
  end
112
- require 'yaml'
113
- YAML.load(read_file(filename))
111
+
112
+ require "yaml"
113
+
114
+ if old_yaml?
115
+ YAML.safe_load(read_file(filename), [MIME::Type])
116
+ else
117
+ YAML.safe_load(read_file(filename), permitted_classes: [MIME::Type])
118
+ end
114
119
  end
115
120
 
116
121
  # Loads MIME::Types from a single JSON file.
@@ -119,28 +124,36 @@ class MIME::Types::Loader
119
124
  # The JSON format is the registry format for the MIME types registry
120
125
  # shipped with the mime-types library.
121
126
  def load_from_json(filename)
122
- require 'json'
127
+ require "json"
123
128
  JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) }
124
129
  end
125
130
 
126
131
  private
127
132
 
128
133
  def read_file(filename)
129
- File.open(filename, 'r:UTF-8:-', &:read)
134
+ File.open(filename, "r:UTF-8:-", &:read)
135
+ end
136
+
137
+ def old_yaml?
138
+ @old_yaml ||=
139
+ begin
140
+ require "rubygems/version"
141
+ Gem::Version.new(YAML::VERSION) < Gem::Version.new("3.1")
142
+ end
130
143
  end
131
144
  end
132
145
 
133
146
  private
134
147
 
135
148
  def yaml_path
136
- File.join(path, '*.y{,a}ml')
149
+ File.join(path, "*.y{,a}ml")
137
150
  end
138
151
 
139
152
  def json_path
140
- File.join(path, '*.json')
153
+ File.join(path, "*.json")
141
154
  end
142
155
 
143
156
  def columnar_path
144
- File.join(path, '*.column')
157
+ File.join(path, "*.column")
145
158
  end
146
159
  end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- ruby encoding: utf-8 -*-
4
-
5
- require 'logger'
3
+ require "logger"
6
4
 
7
5
  ##
8
6
  module MIME
@@ -14,8 +12,8 @@ module MIME
14
12
  attr_accessor :logger
15
13
  end
16
14
 
17
- class WarnLogger < ::Logger #:nodoc:
18
- class WarnLogDevice < ::Logger::LogDevice #:nodoc:
15
+ class WarnLogger < ::Logger # :nodoc:
16
+ class WarnLogDevice < ::Logger::LogDevice # :nodoc:
19
17
  def initialize(*)
20
18
  end
21
19
 
@@ -27,7 +25,7 @@ module MIME
27
25
  end
28
26
  end
29
27
 
30
- def initialize(_1, _2 = nil, _3 = nil)
28
+ def initialize(_one, _two = nil, _three = nil)
31
29
  super nil
32
30
  @logdev = WarnLogDevice.new
33
31
  @formatter = ->(_s, _d, _p, m) { m }
@@ -43,27 +43,27 @@ class << MIME::Types
43
43
  private
44
44
 
45
45
  def lazy_load?
46
- if ENV.key?('RUBY_MIME_TYPES_LAZY_LOAD')
47
- MIME::Types.logger.warn <<-WARNING.chomp
48
- Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
49
- WARNING
46
+ return unless ENV.key?("RUBY_MIME_TYPES_LAZY_LOAD")
50
47
 
51
- (lazy = ENV['RUBY_MIME_TYPES_LAZY_LOAD']) && (lazy != 'false')
52
- end
48
+ MIME::Types.logger.warn <<~WARNING.chomp
49
+ Lazy loading ($RUBY_MIME_TYPES_LAZY_LOAD) is deprecated and will be removed.
50
+ WARNING
51
+
52
+ (lazy = ENV["RUBY_MIME_TYPES_LAZY_LOAD"]) && (lazy != "false")
53
53
  end
54
54
 
55
55
  def __types__
56
- (defined?(@__types__) and @__types__) or load_default_mime_types
56
+ (defined?(@__types__) && @__types__) || load_default_mime_types
57
57
  end
58
58
 
59
59
  unless private_method_defined?(:load_mode)
60
60
  def load_mode
61
- { columnar: true }
61
+ {columnar: true}
62
62
  end
63
63
  end
64
64
 
65
65
  def load_default_mime_types(mode = load_mode)
66
- if @__types__ = MIME::Types::Cache.load
66
+ if (@__types__ = MIME::Types::Cache.load)
67
67
  __instances__.add(@__types__)
68
68
  else
69
69
  @__types__ = MIME::Types::Loader.load(mode)
data/lib/mime/types.rb CHANGED
@@ -7,7 +7,7 @@ module MIME
7
7
  end
8
8
  end
9
9
 
10
- require 'mime/type'
10
+ require "mime/type"
11
11
 
12
12
  # MIME::Types is a registry of MIME types. It is both a class (created with
13
13
  # MIME::Types.new) and a default registry (loaded automatically or through
@@ -61,6 +61,7 @@ require 'mime/type'
61
61
  # puts plaintext.ascii? # => true
62
62
  # puts plaintext.obsolete? # => false
63
63
  # puts plaintext.registered? # => true
64
+ # puts plaintext.provisional? # => false
64
65
  # puts plaintext == 'text/plain' # => true
65
66
  # puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
66
67
  #
@@ -72,8 +73,8 @@ class MIME::Types
72
73
 
73
74
  # Creates a new MIME::Types registry.
74
75
  def initialize
75
- @type_variants = Container.new
76
- @extension_index = Container.new
76
+ @type_variants = Container.new
77
+ @extension_index = Container.new
77
78
  end
78
79
 
79
80
  # Returns the number of known type variants.
@@ -122,14 +123,15 @@ class MIME::Types
122
123
  # 5. Obsolete definitions use-instead clauses are compared.
123
124
  # 6. Sort on name.
124
125
  def [](type_id, complete: false, registered: false)
125
- matches = case type_id
126
+ matches =
127
+ case type_id
126
128
  when MIME::Type
127
129
  @type_variants[type_id.simplified]
128
130
  when Regexp
129
131
  match(type_id)
130
132
  else
131
133
  @type_variants[MIME::Type.simplified(type_id)]
132
- end
134
+ end
133
135
 
134
136
  prune_matches(matches, complete, registered).sort { |a, b|
135
137
  a.priority_compare(b)
@@ -163,7 +165,7 @@ class MIME::Types
163
165
  # The last parameter may be the value <tt>:silent</tt> or +true+ which
164
166
  # will suppress duplicate MIME type warnings.
165
167
  def add(*types)
166
- quiet = ((types.last == :silent) or (types.last == true))
168
+ quiet = ((types.last == :silent) || (types.last == true))
167
169
 
168
170
  types.each do |mime_type|
169
171
  case mime_type
@@ -184,10 +186,10 @@ class MIME::Types
184
186
  # already known, a warning will be displayed. The +quiet+ parameter may be a
185
187
  # truthy value to suppress that warning.
186
188
  def add_type(type, quiet = false)
187
- if !quiet and @type_variants[type.simplified].include?(type)
188
- MIME::Types.logger.warn <<-warning
189
- Type #{type} is already registered as a variant of #{type.simplified}.
190
- warning
189
+ if !quiet && @type_variants[type.simplified].include?(type)
190
+ MIME::Types.logger.warn <<~WARNING
191
+ Type #{type} is already registered as a variant of #{type.simplified}.
192
+ WARNING
191
193
  end
192
194
 
193
195
  add_type_variant!(type)
@@ -202,6 +204,7 @@ Type #{type} is already registered as a variant of #{type.simplified}.
202
204
 
203
205
  def reindex_extensions!(mime_type)
204
206
  return unless @type_variants[mime_type.simplified].include?(mime_type)
207
+
205
208
  index_extensions!(mime_type)
206
209
  end
207
210
 
@@ -222,9 +225,9 @@ Type #{type} is already registered as a variant of #{type.simplified}.
222
225
  end
223
226
  end
224
227
 
225
- require 'mime/types/cache'
226
- require 'mime/types/container'
227
- require 'mime/types/loader'
228
- require 'mime/types/logger'
229
- require 'mime/types/_columnar'
230
- require 'mime/types/registry'
228
+ require "mime/types/cache"
229
+ require "mime/types/container"
230
+ require "mime/types/loader"
231
+ require "mime/types/logger"
232
+ require "mime/types/_columnar"
233
+ require "mime/types/registry"
data/lib/mime-types.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'mime/types'
3
+ require "mime/types"
@@ -1,15 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- ruby encoding: utf-8 -*-
3
+ require "mime/type"
4
+ require "fileutils"
4
5
 
5
- require 'mime/type'
6
- require 'fileutils'
6
+ gem "minitest"
7
+ require "minitest/focus"
8
+ require "minitest-bonus-assertions"
9
+ require "minitest/hooks"
7
10
 
8
- gem 'minitest'
9
- require 'fivemat/minitest/autorun'
10
- require 'minitest/focus'
11
- require 'minitest/rg'
12
- require 'minitest-bonus-assertions'
13
- require 'minitest/hooks'
14
-
15
- ENV['RUBY_MIME_TYPES_LAZY_LOAD'] = 'yes'
11
+ ENV["RUBY_MIME_TYPES_LAZY_LOAD"] = "yes"