mime-types 2.5 → 2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Contributing.rdoc +72 -20
- data/History-Types.rdoc +11 -0
- data/History.rdoc +31 -1
- data/Manifest.txt +18 -1
- data/README.rdoc +142 -61
- data/Rakefile +59 -91
- data/data/mime-types.json +1 -1
- data/data/mime.content_type.column +1907 -0
- data/data/mime.docs.column +1907 -0
- data/data/mime.encoding.column +1907 -0
- data/data/mime.friendly.column +1907 -0
- data/data/mime.obsolete.column +1907 -0
- data/data/mime.references.column +1907 -0
- data/data/mime.registered.column +1907 -0
- data/data/mime.signature.column +1907 -0
- data/data/mime.system.column +1907 -0
- data/data/mime.use_instead.column +1907 -0
- data/data/mime.xrefs.column +1907 -0
- data/lib/mime/type.rb +192 -119
- data/lib/mime/type/columnar.rb +112 -0
- data/lib/mime/types.rb +39 -25
- data/lib/mime/types/cache.rb +41 -35
- data/lib/mime/types/columnar.rb +160 -0
- data/lib/mime/types/deprecations.rb +53 -0
- data/lib/mime/types/loader.rb +60 -20
- data/lib/mime/types/loader_path.rb +2 -3
- data/lib/mime/types/logger.rb +35 -0
- data/support/apache_mime_types.rb +8 -1
- data/support/benchmarks/load.rb +17 -8
- data/support/benchmarks/load_allocations.rb +83 -0
- data/support/benchmarks/object_counts.rb +41 -0
- data/support/convert.rb +44 -24
- data/support/convert/columnar.rb +90 -0
- data/support/iana_registry.rb +18 -8
- data/test/fixture/json.json +1 -1
- data/test/fixture/yaml.yaml +3 -6
- data/test/minitest_helper.rb +9 -10
- data/test/test_mime_type.rb +126 -62
- data/test/test_mime_types.rb +15 -11
- data/test/test_mime_types_class.rb +16 -14
- data/test/test_mime_types_lazy.rb +7 -1
- data/test/test_mime_types_loader.rb +17 -8
- metadata +54 -12
- data/lib/mime.rb +0 -51
data/lib/mime/type.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
1
|
# -*- ruby encoding: utf-8 -*-
|
2
2
|
|
3
|
-
require 'mime'
|
4
|
-
require 'json'
|
5
|
-
|
6
3
|
# The definition of one MIME content-type.
|
7
4
|
#
|
8
5
|
# == Usage
|
@@ -50,16 +47,17 @@ class MIME::Type
|
|
50
47
|
end
|
51
48
|
|
52
49
|
def to_s
|
53
|
-
"Invalid Encoding #{@encoding.inspect}
|
50
|
+
"Invalid Encoding #{@encoding.inspect}"
|
54
51
|
end
|
55
52
|
# :startdoc:
|
56
53
|
end
|
57
54
|
|
58
55
|
# The released version of the mime-types library.
|
59
|
-
VERSION = '2.
|
56
|
+
VERSION = '2.6'
|
60
57
|
|
61
58
|
include Comparable
|
62
59
|
|
60
|
+
# :stopdoc:
|
63
61
|
MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)}o
|
64
62
|
UNREGISTERED_RE = %r{[Xx]-}o
|
65
63
|
I18N_RE = %r{[^[:alnum:]]}o
|
@@ -70,20 +68,22 @@ class MIME::Type
|
|
70
68
|
TEXT_ENCODINGS = %w(7bit quoted-printable)
|
71
69
|
VALID_ENCODINGS = DEFAULT_ENCODINGS + BINARY_ENCODINGS + TEXT_ENCODINGS
|
72
70
|
|
73
|
-
IANA_URL =
|
74
|
-
RFC_URL =
|
75
|
-
DRAFT_URL =
|
76
|
-
CONTACT_URL =
|
71
|
+
IANA_URL = 'http://www.iana.org/assignments/media-types/%s/%s'
|
72
|
+
RFC_URL = 'http://rfc-editor.org/rfc/rfc%s.txt'
|
73
|
+
DRAFT_URL = 'http://datatracker.ietf.org/public/idindex.cgi?command=id_details&filename=%s' # rubocop:disable Metrics/LineLength
|
74
|
+
CONTACT_URL = 'http://www.iana.org/assignments/contact-people.htm#%s'
|
75
|
+
# :startdoc:
|
77
76
|
|
78
77
|
if respond_to? :private_constant
|
79
78
|
private_constant :MEDIA_TYPE_RE, :UNREGISTERED_RE, :I18N_RE, :PLATFORM_RE,
|
80
|
-
|
81
|
-
|
79
|
+
:DEFAULT_ENCODINGS, :BINARY_ENCODINGS, :TEXT_ENCODINGS,
|
80
|
+
:VALID_ENCODINGS, :IANA_URL, :RFC_URL, :DRAFT_URL,
|
81
|
+
:CONTACT_URL
|
82
82
|
end
|
83
83
|
|
84
|
-
# Builds a MIME::Type object from the
|
85
|
-
# (e.g., 'text/plain' or 'applicaton/x-eruby'). The constructed object
|
86
|
-
# yielded to an optional block for additional configuration, such as
|
84
|
+
# Builds a MIME::Type object from the +content_type+, a MIME Content Type
|
85
|
+
# value (e.g., 'text/plain' or 'applicaton/x-eruby'). The constructed object
|
86
|
+
# is yielded to an optional block for additional configuration, such as
|
87
87
|
# associating extensions and encoding information.
|
88
88
|
#
|
89
89
|
# * When provided a Hash or a MIME::Type, the MIME::Type will be
|
@@ -92,39 +92,41 @@ class MIME::Type
|
|
92
92
|
# the first two elements of the array as the content type and
|
93
93
|
# extensions.
|
94
94
|
# * Otherwise, the content_type will be used as a string.
|
95
|
+
#
|
96
|
+
# Yields the newly constructed +self+ object.
|
95
97
|
def initialize(content_type) # :yields self:
|
96
|
-
@friendly
|
97
|
-
self.system
|
98
|
-
self.obsolete
|
99
|
-
self.registered
|
98
|
+
@friendly = {}
|
99
|
+
self.system = nil
|
100
|
+
self.obsolete = false
|
101
|
+
self.registered = nil
|
100
102
|
self.use_instead = nil
|
101
|
-
self.signature
|
103
|
+
self.signature = nil
|
102
104
|
|
103
105
|
case content_type
|
104
106
|
when Hash
|
105
107
|
init_with(content_type)
|
106
108
|
when Array
|
107
109
|
self.content_type = content_type[0]
|
108
|
-
self.extensions
|
110
|
+
self.extensions = content_type[1] || []
|
109
111
|
when MIME::Type
|
110
112
|
init_with(content_type.to_h)
|
111
113
|
else
|
112
114
|
self.content_type = content_type
|
113
115
|
end
|
114
116
|
|
115
|
-
self.extensions
|
116
|
-
self.docs
|
117
|
-
self.encoding
|
117
|
+
self.extensions ||= []
|
118
|
+
self.docs ||= []
|
119
|
+
self.encoding ||= :default
|
118
120
|
# This value will be deprecated in the future, as it will be an
|
119
121
|
# alternative view on #xrefs. Silence an unnecessary warning for now by
|
120
122
|
# assigning directly to the instance variable.
|
121
|
-
@references
|
122
|
-
self.xrefs
|
123
|
+
@references ||= []
|
124
|
+
self.xrefs ||= {}
|
123
125
|
|
124
126
|
yield self if block_given?
|
125
127
|
end
|
126
128
|
|
127
|
-
# Returns +true+ if the simplified type matches the current
|
129
|
+
# Returns +true+ if the +other+ simplified type matches the current type.
|
128
130
|
def like?(other)
|
129
131
|
if other.respond_to?(:simplified)
|
130
132
|
@simplified == other.simplified
|
@@ -133,10 +135,10 @@ class MIME::Type
|
|
133
135
|
end
|
134
136
|
end
|
135
137
|
|
136
|
-
# Compares the MIME::Type against the exact content type or the
|
137
|
-
# type (the simplified type will be used if comparing against
|
138
|
-
# that can be treated as a String with #to_s). In comparisons, this
|
139
|
-
# done against the lowercase version of the MIME::Type.
|
138
|
+
# Compares the +other+ MIME::Type against the exact content type or the
|
139
|
+
# simplified type (the simplified type will be used if comparing against
|
140
|
+
# something that can be treated as a String with #to_s). In comparisons, this
|
141
|
+
# is done against the lowercase version of the MIME::Type.
|
140
142
|
def <=>(other)
|
141
143
|
if other.respond_to?(:content_type)
|
142
144
|
@content_type.downcase <=> other.content_type.downcase
|
@@ -145,7 +147,7 @@ class MIME::Type
|
|
145
147
|
end
|
146
148
|
end
|
147
149
|
|
148
|
-
# Compares the MIME::Type based on how reliable it is before doing a
|
150
|
+
# Compares the +other+ MIME::Type based on how reliable it is before doing a
|
149
151
|
# normal <=> comparison. Used by MIME::Types#[] to sort types. The
|
150
152
|
# comparisons involved are:
|
151
153
|
#
|
@@ -157,6 +159,11 @@ class MIME::Type
|
|
157
159
|
# 4. Current definitions < obsolete definitions.
|
158
160
|
# 5. Obselete with use-instead references < obsolete without.
|
159
161
|
# 6. Obsolete use-instead definitions are compared.
|
162
|
+
#
|
163
|
+
# While this method is public, its use is strongly discouraged by consumers
|
164
|
+
# of mime-types. In mime-types 3, this method is likely to see substantial
|
165
|
+
# revision and simplification to ensure current registered content types sort
|
166
|
+
# before unregistered or obsolete content types.
|
160
167
|
def priority_compare(other)
|
161
168
|
pc = simplified <=> other.simplified
|
162
169
|
if pc.zero?
|
@@ -184,7 +191,7 @@ class MIME::Type
|
|
184
191
|
pc
|
185
192
|
end
|
186
193
|
|
187
|
-
# Returns +true+ if the other object is a MIME::Type and the content types
|
194
|
+
# Returns +true+ if the +other+ object is a MIME::Type and the content types
|
188
195
|
# match.
|
189
196
|
def eql?(other)
|
190
197
|
other.kind_of?(MIME::Type) and self == other
|
@@ -235,12 +242,14 @@ class MIME::Type
|
|
235
242
|
attr_reader :extensions
|
236
243
|
def extensions=(ext) # :nodoc:
|
237
244
|
@extensions = Array(ext).flatten.compact.uniq
|
245
|
+
# TODO: In mime-types 3.x, we probably want to have a clue about the
|
246
|
+
# container(s) we belong to so we can trigger reindexing when this is done.
|
238
247
|
end
|
239
248
|
|
240
|
-
# Merge the extensions provided into this MIME::Type. The extensions added
|
249
|
+
# Merge the +extensions+ provided into this MIME::Type. The extensions added
|
241
250
|
# will be merged uniquely.
|
242
|
-
def add_extensions(*
|
243
|
-
self.extensions = self.extensions +
|
251
|
+
def add_extensions(*extensions)
|
252
|
+
self.extensions = self.extensions + extensions
|
244
253
|
end
|
245
254
|
|
246
255
|
##
|
@@ -253,33 +262,39 @@ class MIME::Type
|
|
253
262
|
extensions.first
|
254
263
|
end
|
255
264
|
|
256
|
-
|
257
|
-
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
#
|
261
|
-
#
|
265
|
+
##
|
266
|
+
# The encoding (+7bit+, +8bit+, <tt>quoted-printable</tt>, or +base64+)
|
267
|
+
# required to transport the data of this content type safely across a
|
268
|
+
# network, which roughly corresponds to Content-Transfer-Encoding. A value of
|
269
|
+
# +nil+ or <tt>:default</tt> will reset the #encoding to the
|
270
|
+
# #default_encoding for the MIME::Type. Raises ArgumentError if the encoding
|
271
|
+
# provided is invalid.
|
262
272
|
#
|
263
273
|
# If the encoding is not provided on construction, this will be either
|
264
274
|
# 'quoted-printable' (for text/* media types) and 'base64' for eveything
|
265
275
|
# else.
|
276
|
+
#
|
277
|
+
# :attr_accessor: encoding
|
278
|
+
|
279
|
+
##
|
266
280
|
attr_reader :encoding
|
267
281
|
def encoding=(enc) # :nodoc:
|
268
282
|
if DEFAULT_ENCODINGS.include?(enc)
|
269
|
-
@encoding =
|
283
|
+
@encoding = default_encoding
|
270
284
|
elsif BINARY_ENCODINGS.include?(enc) or TEXT_ENCODINGS.include?(enc)
|
271
285
|
@encoding = enc
|
272
286
|
else
|
273
|
-
|
287
|
+
fail InvalidEncoding, enc
|
274
288
|
end
|
275
289
|
end
|
276
290
|
|
277
291
|
# If the MIME::Type is a system-specific MIME::Type, returns the regular
|
278
292
|
# expression for the operating system indicated.
|
279
293
|
#
|
280
|
-
# This information about MIME content types is deprecated
|
294
|
+
# This information about MIME content types is deprecated and will be removed
|
295
|
+
# in mime-types 3.
|
281
296
|
def system
|
282
|
-
MIME.deprecated(self, __method__)
|
297
|
+
MIME::Types.deprecated(self, __method__)
|
283
298
|
@system
|
284
299
|
end
|
285
300
|
|
@@ -287,7 +302,7 @@ class MIME::Type
|
|
287
302
|
if os.nil? or os.kind_of?(Regexp)
|
288
303
|
@system = os
|
289
304
|
else
|
290
|
-
@system = %r
|
305
|
+
@system = %r{#{os}}
|
291
306
|
end
|
292
307
|
end
|
293
308
|
|
@@ -300,11 +315,17 @@ class MIME::Type
|
|
300
315
|
# Returns the media type or types that should be used instead of this
|
301
316
|
# media type, if it is obsolete. If there is no replacement media type, or
|
302
317
|
# it is not obsolete, +nil+ will be returned.
|
318
|
+
#
|
319
|
+
# :attr_accessor: use_instead
|
320
|
+
|
321
|
+
##
|
303
322
|
def use_instead
|
304
323
|
return nil unless obsolete?
|
305
324
|
@use_instead
|
306
325
|
end
|
307
|
-
|
326
|
+
|
327
|
+
##
|
328
|
+
attr_writer :use_instead
|
308
329
|
|
309
330
|
# Returns +true+ if the media type is obsolete.
|
310
331
|
def obsolete?
|
@@ -323,7 +344,7 @@ class MIME::Type
|
|
323
344
|
# call-seq:
|
324
345
|
# text_plain.friendly # => "Text File"
|
325
346
|
# text_plain.friendly('en') # => "Text File"
|
326
|
-
def friendly(lang = 'en')
|
347
|
+
def friendly(lang = 'en'.freeze)
|
327
348
|
@friendly ||= {}
|
328
349
|
|
329
350
|
case lang
|
@@ -334,7 +355,7 @@ class MIME::Type
|
|
334
355
|
when Hash
|
335
356
|
@friendly.merge!(lang)
|
336
357
|
else
|
337
|
-
|
358
|
+
fail ArgumentError
|
338
359
|
end
|
339
360
|
end
|
340
361
|
|
@@ -349,28 +370,59 @@ class MIME::Type
|
|
349
370
|
# # from application/x-msword
|
350
371
|
attr_reader :i18n_key
|
351
372
|
|
373
|
+
##
|
352
374
|
# The encoded references URL list for this MIME::Type. See #urls for more
|
353
375
|
# information.
|
354
376
|
#
|
355
377
|
# This was previously called #url.
|
356
|
-
|
378
|
+
#
|
379
|
+
# #references has been deprecated and both versions (#references and #url)
|
380
|
+
# will be removed in mime-types 3.
|
381
|
+
#
|
382
|
+
# :attr_accessor: references
|
383
|
+
|
384
|
+
##
|
385
|
+
def references(__internal__ = false)
|
386
|
+
MIME::Types.deprecated(self, __method__) unless __internal__
|
387
|
+
@references
|
388
|
+
end
|
389
|
+
|
390
|
+
##
|
357
391
|
def references=(r) # :nodoc:
|
358
|
-
MIME.deprecated(self, __method__)
|
392
|
+
MIME::Types.deprecated(self, __method__)
|
359
393
|
@references = Array(r).flatten.compact.uniq
|
360
394
|
end
|
361
395
|
|
362
|
-
|
363
|
-
|
364
|
-
|
396
|
+
##
|
397
|
+
# The encoded references URL list for this MIME::Type. See #urls for more
|
398
|
+
# information.
|
399
|
+
#
|
400
|
+
# #urls has been deprecated and both versions (#references and #url) will be
|
401
|
+
# removed in mime-types 3.
|
402
|
+
#
|
403
|
+
# :attr_accessor: url
|
404
|
+
|
405
|
+
##
|
406
|
+
def url
|
407
|
+
MIME::Types.deprecated(self, __method__)
|
408
|
+
references(true)
|
365
409
|
end
|
366
410
|
|
411
|
+
##
|
367
412
|
def url=(r) # :nodoc:
|
368
|
-
MIME.deprecated(self, __method__)
|
413
|
+
MIME::Types.deprecated(self, __method__)
|
369
414
|
self.references = r
|
370
415
|
end
|
371
416
|
|
417
|
+
##
|
372
418
|
# The cross-references list for this MIME::Type.
|
419
|
+
#
|
420
|
+
# :attr_accessor: xrefs
|
421
|
+
|
422
|
+
##
|
373
423
|
attr_reader :xrefs
|
424
|
+
|
425
|
+
##
|
374
426
|
def xrefs=(x) # :nodoc:
|
375
427
|
@xrefs = MIME::Types::Container.new.merge(x)
|
376
428
|
@xrefs.each_value(&:sort!)
|
@@ -394,8 +446,11 @@ class MIME::Type
|
|
394
446
|
#
|
395
447
|
# These values will be accessible through #urls, which always returns an
|
396
448
|
# array.
|
449
|
+
#
|
450
|
+
# This method is deprecated and will be removed in mime-types 3.
|
397
451
|
def urls
|
398
|
-
|
452
|
+
MIME::Types.deprecated(self, __method__)
|
453
|
+
references(true).map do |el|
|
399
454
|
case el
|
400
455
|
when %r{^IANA$}
|
401
456
|
IANA_URL % [ @media_type, @sub_type ]
|
@@ -417,32 +472,33 @@ class MIME::Type
|
|
417
472
|
|
418
473
|
# The decoded cross-reference URL list for this MIME::Type.
|
419
474
|
def xref_urls
|
420
|
-
xrefs.
|
475
|
+
xrefs.flat_map { |(type, values)|
|
421
476
|
case type
|
422
|
-
when 'rfc'
|
423
|
-
values.map { |data|
|
424
|
-
when 'draft'
|
477
|
+
when 'rfc'.freeze
|
478
|
+
values.map { |data| 'http://www.iana.org/go/%s'.freeze % data }
|
479
|
+
when 'draft'.freeze
|
425
480
|
values.map { |data|
|
426
|
-
|
481
|
+
'http://www.iana.org/go/%s'.freeze % data.sub(/\ARFC/, 'draft')
|
427
482
|
}
|
428
|
-
when 'rfc-errata'
|
483
|
+
when 'rfc-errata'.freeze
|
429
484
|
values.map { |data|
|
430
|
-
|
485
|
+
'http://www.rfc-editor.org/errata_search.php?eid=%s'.freeze % data
|
431
486
|
}
|
432
|
-
when 'person'
|
487
|
+
when 'person'.freeze
|
433
488
|
values.map { |data|
|
434
|
-
|
489
|
+
'http://www.iana.org/assignments/media-types/media-types.xhtml#%s'.freeze % data # rubocop:disable Metrics/LineLength
|
435
490
|
}
|
436
|
-
when 'template'
|
491
|
+
when 'template'.freeze
|
437
492
|
values.map { |data|
|
438
|
-
|
493
|
+
'http://www.iana.org/assignments/media-types/%s'.freeze % data
|
439
494
|
}
|
440
495
|
else # 'uri', 'text', etc.
|
441
496
|
values
|
442
497
|
end
|
443
|
-
}
|
498
|
+
}
|
444
499
|
end
|
445
500
|
|
501
|
+
##
|
446
502
|
# Prior to BCP 178 (RFC 6648), it could be assumed that MIME content types
|
447
503
|
# that start with <tt>x-</tt> were unregistered MIME. Per this BCP, this
|
448
504
|
# assumption is no longer being made by default in this library.
|
@@ -453,6 +509,10 @@ class MIME::Type
|
|
453
509
|
# - Unspecified, in which case the media-type and the content-type will be
|
454
510
|
# scanned to see if they start with <tt>x-</tt>, indicating that they
|
455
511
|
# are assumed unregistered.
|
512
|
+
#
|
513
|
+
# In mime-types 3, only a MIME content type that is explicitly registered
|
514
|
+
# will be used; there will be assumption that <tt>x-</tt> types are
|
515
|
+
# unregistered.
|
456
516
|
def registered?
|
457
517
|
if @registered.nil?
|
458
518
|
(@raw_media_type !~ UNREGISTERED_RE) and
|
@@ -477,11 +537,11 @@ class MIME::Type
|
|
477
537
|
# formats. This method returns +false+ when the MIME::Type encoding is
|
478
538
|
# set to <tt>base64</tt>.
|
479
539
|
def ascii?
|
480
|
-
|
540
|
+
!binary?
|
481
541
|
end
|
482
542
|
|
483
|
-
# Returns +true+ when the simplified MIME::Type is
|
484
|
-
#
|
543
|
+
# Returns +true+ when the simplified MIME::Type is one of the known digital
|
544
|
+
# signature types.
|
485
545
|
def signature?
|
486
546
|
!!@signature
|
487
547
|
end
|
@@ -492,25 +552,25 @@ class MIME::Type
|
|
492
552
|
|
493
553
|
# Returns +true+ if the MIME::Type is specific to an operating system.
|
494
554
|
#
|
495
|
-
# This method is deprecated.
|
555
|
+
# This method is deprecated and will be removed in mime-types 3.
|
496
556
|
def system?(__internal__ = false)
|
497
|
-
MIME.deprecated(self, __method__) unless __internal__
|
498
|
-
|
557
|
+
MIME::Types.deprecated(self, __method__) unless __internal__
|
558
|
+
!@system.nil?
|
499
559
|
end
|
500
560
|
|
501
561
|
# Returns +true+ if the MIME::Type is specific to the current operating
|
502
562
|
# system as represented by RUBY_PLATFORM.
|
503
563
|
#
|
504
|
-
# This method is deprecated.
|
564
|
+
# This method is deprecated and will be removed in mime-types 3.
|
505
565
|
def platform?(__internal__ = false)
|
506
|
-
MIME.deprecated(self, __method__) unless __internal__
|
566
|
+
MIME::Types.deprecated(self, __method__) unless __internal__
|
507
567
|
system?(__internal__) and (RUBY_PLATFORM =~ @system)
|
508
568
|
end
|
509
569
|
|
510
570
|
# Returns +true+ if the MIME::Type specifies an extension list,
|
511
571
|
# indicating that it is a complete MIME::Type.
|
512
572
|
def complete?
|
513
|
-
|
573
|
+
!@extensions.empty?
|
514
574
|
end
|
515
575
|
|
516
576
|
# Returns the MIME::Type as a string.
|
@@ -529,9 +589,9 @@ class MIME::Type
|
|
529
589
|
# Returns the MIME::Type as an array suitable for use with
|
530
590
|
# MIME::Type.from_array.
|
531
591
|
#
|
532
|
-
# This method is deprecated.
|
592
|
+
# This method is deprecated and will be removed in mime-types 3.
|
533
593
|
def to_a
|
534
|
-
MIME.deprecated(self, __method__)
|
594
|
+
MIME::Types.deprecated(self, __method__)
|
535
595
|
[ @content_type, @extensions, @encoding, @system, obsolete?, @docs,
|
536
596
|
@references, registered? ]
|
537
597
|
end
|
@@ -539,9 +599,9 @@ class MIME::Type
|
|
539
599
|
# Returns the MIME::Type as an array suitable for use with
|
540
600
|
# MIME::Type.from_hash.
|
541
601
|
#
|
542
|
-
# This method is deprecated.
|
602
|
+
# This method is deprecated and will be removed in mime-types 3.
|
543
603
|
def to_hash
|
544
|
-
MIME.deprecated(self, __method__)
|
604
|
+
MIME::Types.deprecated(self, __method__)
|
545
605
|
{ 'Content-Type' => @content_type,
|
546
606
|
'Content-Transfer-Encoding' => @encoding,
|
547
607
|
'Extensions' => @extensions,
|
@@ -555,6 +615,7 @@ class MIME::Type
|
|
555
615
|
|
556
616
|
# Converts the MIME::Type to a JSON string.
|
557
617
|
def to_json(*args)
|
618
|
+
require 'json'
|
558
619
|
to_h.to_json(*args)
|
559
620
|
end
|
560
621
|
|
@@ -567,6 +628,8 @@ class MIME::Type
|
|
567
628
|
# Populates the +coder+ with attributes about this record for
|
568
629
|
# serialization. The structure of +coder+ should match the structure used
|
569
630
|
# with #init_with.
|
631
|
+
#
|
632
|
+
# This method should be considered a private implementation detail.
|
570
633
|
def encode_with(coder)
|
571
634
|
coder['content-type'] = @content_type
|
572
635
|
coder['docs'] = @docs unless @docs.nil? or @docs.empty?
|
@@ -577,7 +640,7 @@ class MIME::Type
|
|
577
640
|
coder['obsolete'] = obsolete?
|
578
641
|
coder['use-instead'] = use_instead if use_instead
|
579
642
|
end
|
580
|
-
coder['references'] = references unless references.empty?
|
643
|
+
coder['references'] = references(true) unless references(true).empty?
|
581
644
|
coder['xrefs'] = xrefs unless xrefs.empty?
|
582
645
|
coder['registered'] = registered?
|
583
646
|
coder['signature'] = signature? if signature?
|
@@ -587,10 +650,12 @@ class MIME::Type
|
|
587
650
|
|
588
651
|
# Initialize an empty object from +coder+, which must contain the
|
589
652
|
# attributes necessary for initializing an empty object.
|
653
|
+
#
|
654
|
+
# This method should be considered a private implementation detail.
|
590
655
|
def init_with(coder)
|
591
656
|
self.content_type = coder['content-type']
|
592
657
|
self.docs = coder['docs'] || []
|
593
|
-
|
658
|
+
friendly(coder['friendly'] || {})
|
594
659
|
self.encoding = coder['encoding']
|
595
660
|
self.extensions = coder['extensions'] || []
|
596
661
|
self.obsolete = coder['obsolete']
|
@@ -604,14 +669,13 @@ class MIME::Type
|
|
604
669
|
self.xrefs = coder['xrefs'] || {}
|
605
670
|
self.use_instead = coder['use-instead']
|
606
671
|
end
|
607
|
-
# :startdoc:
|
608
672
|
|
609
673
|
class << self
|
610
674
|
# The MIME types main- and sub-label can both start with <tt>x-</tt>,
|
611
675
|
# which indicates that it is a non-registered name. Of course, after
|
612
|
-
# registration this flag
|
613
|
-
# proliferation of MIME types. The simplified string has the
|
614
|
-
# removed and
|
676
|
+
# registration this flag may disappear, adds to the confusing
|
677
|
+
# proliferation of MIME types. The simplified +content_type+ string has the
|
678
|
+
# <tt>x-</tt> removed and is translated to lowercase.
|
615
679
|
def simplified(content_type)
|
616
680
|
matchdata = case content_type
|
617
681
|
when MatchData
|
@@ -620,16 +684,16 @@ class MIME::Type
|
|
620
684
|
MEDIA_TYPE_RE.match(content_type)
|
621
685
|
end
|
622
686
|
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
687
|
+
return unless matchdata
|
688
|
+
|
689
|
+
matchdata.captures.map { |e|
|
690
|
+
e.downcase!
|
691
|
+
e.gsub!(UNREGISTERED_RE, ''.freeze)
|
692
|
+
e
|
693
|
+
}.join('/'.freeze)
|
630
694
|
end
|
631
695
|
|
632
|
-
# Converts a provided
|
696
|
+
# Converts a provided +content_type+ into a translation key suitable for
|
633
697
|
# use with the I18n library.
|
634
698
|
def i18n_key(content_type)
|
635
699
|
matchdata = case content_type
|
@@ -639,23 +703,23 @@ class MIME::Type
|
|
639
703
|
MEDIA_TYPE_RE.match(content_type)
|
640
704
|
end
|
641
705
|
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
706
|
+
return unless matchdata
|
707
|
+
|
708
|
+
matchdata.captures.map { |e|
|
709
|
+
e.downcase!
|
710
|
+
e.gsub!(UNREGISTERED_RE, ''.freeze)
|
711
|
+
e.gsub!(I18N_RE, '-'.freeze)
|
712
|
+
e
|
713
|
+
}.join('.'.freeze)
|
650
714
|
end
|
651
715
|
|
652
|
-
# Creates a MIME::Type from an array in the form of:
|
653
|
-
# [type-name, [extensions], encoding, system]
|
716
|
+
# Creates a MIME::Type from an +args+ array in the form of:
|
717
|
+
# [ type-name, [ extensions ], encoding, system ]
|
654
718
|
#
|
655
719
|
# +extensions+, +encoding+, and +system+ are optional.
|
656
720
|
#
|
657
|
-
# MIME::Type.from_array(
|
658
|
-
# MIME::Type.from_array([
|
721
|
+
# MIME::Type.from_array('application/x-ruby', %w(rb), '8bit')
|
722
|
+
# MIME::Type.from_array([ 'application/x-ruby', [ 'rb' ], '8bit' ])
|
659
723
|
#
|
660
724
|
# These are equivalent to:
|
661
725
|
#
|
@@ -664,15 +728,18 @@ class MIME::Type
|
|
664
728
|
# t.encoding = '8bit'
|
665
729
|
# end
|
666
730
|
#
|
667
|
-
#
|
668
|
-
|
669
|
-
|
731
|
+
# It will yield the type (+t+) if a block is given.
|
732
|
+
#
|
733
|
+
# This method is deprecated and will be removed in mime-types 3.
|
734
|
+
def from_array(*args) # :yields t:
|
735
|
+
MIME::Types.deprecated(self, __method__)
|
670
736
|
|
671
737
|
# Dereferences the array one level, if necessary.
|
672
738
|
args = args.first if args.first.kind_of? Array
|
673
739
|
|
674
740
|
unless args.size.between?(1, 8)
|
675
|
-
|
741
|
+
fail ArgumentError,
|
742
|
+
'Array provided must contain between one and eight elements.'
|
676
743
|
end
|
677
744
|
|
678
745
|
MIME::Type.new(args.shift) do |t|
|
@@ -682,7 +749,7 @@ class MIME::Type
|
|
682
749
|
end
|
683
750
|
end
|
684
751
|
|
685
|
-
# Creates a MIME::Type from a hash
|
752
|
+
# Creates a MIME::Type from a +hash+. Keys are case-insensitive, dashes
|
686
753
|
# may be replaced with underscores, and the internal Symbol of the
|
687
754
|
# lowercase-underscore version can be used as well. That is,
|
688
755
|
# Content-Type can be provided as content-type, Content_Type,
|
@@ -705,9 +772,12 @@ class MIME::Type
|
|
705
772
|
# t.extensions = ['yaml', 'yml']
|
706
773
|
# end
|
707
774
|
#
|
708
|
-
#
|
709
|
-
|
710
|
-
|
775
|
+
# It will yield the constructed type +t+ if a block has been provided.
|
776
|
+
#
|
777
|
+
#
|
778
|
+
# This method is deprecated and will be removed in mime-types 3.
|
779
|
+
def from_hash(hash) # :yields t:
|
780
|
+
MIME::Types.deprecated(self, __method__)
|
711
781
|
type = {}
|
712
782
|
hash.each_pair do |k, v|
|
713
783
|
type[k.to_s.tr('A-Z', 'a-z').gsub(/-/, '_').to_sym] = v
|
@@ -726,7 +796,7 @@ class MIME::Type
|
|
726
796
|
end
|
727
797
|
end
|
728
798
|
|
729
|
-
# Essentially a copy constructor
|
799
|
+
# Essentially a copy constructor for +mime_type+.
|
730
800
|
#
|
731
801
|
# MIME::Type.from_mime_type(plaintext)
|
732
802
|
#
|
@@ -738,17 +808,20 @@ class MIME::Type
|
|
738
808
|
# t.encoding = plaintext.encoding.dup
|
739
809
|
# end
|
740
810
|
#
|
741
|
-
#
|
811
|
+
# It will yield the type (+t+) if a block is given.
|
812
|
+
#
|
813
|
+
# This method is deprecated and will be removed in mime-types 3.
|
742
814
|
def from_mime_type(mime_type) # :yields the new MIME::Type:
|
743
|
-
MIME.deprecated(self, __method__)
|
815
|
+
MIME::Types.deprecated(self, __method__)
|
744
816
|
new(mime_type)
|
745
817
|
end
|
746
818
|
end
|
747
819
|
|
748
820
|
private
|
821
|
+
|
749
822
|
def content_type=(type_string)
|
750
823
|
match = MEDIA_TYPE_RE.match(type_string)
|
751
|
-
|
824
|
+
fail InvalidContentType, type_string if match.nil?
|
752
825
|
|
753
826
|
@content_type = type_string
|
754
827
|
@raw_media_type, @raw_sub_type = match.captures
|