4
4
module MIME
5
5
end
6
6
7
+ require "mime/types/deprecations"
8
+
7
9
# The definition of one MIME content-type.
8
10
#
9
11
# == Usage
10
- # require ' mime/types'
12
+ # require " mime/types"
11
13
#
12
- # plaintext = MIME::Types[' text/plain' ] # => [ text/plain ]
14
+ # plaintext = MIME::Types[" text/plain" ] # => [ text/plain ]
13
15
# text = plaintext.first
14
- # puts text.media_type # => ' text'
15
- # puts text.sub_type # => ' plain'
16
+ # puts text.media_type # => " text"
17
+ # puts text.sub_type # => " plain"
16
18
#
17
- # puts text.extensions.join(' ' ) # => ' txt asc c cc h hh cpp hpp dat hlp'
18
- # puts text.preferred_extension # => ' txt'
19
- # puts text.friendly # => ' Text Document'
20
- # puts text.i18n_key # => ' text.plain'
19
+ # puts text.extensions.join(" " ) # => " txt asc c cc h hh cpp hpp dat hlp"
20
+ # puts text.preferred_extension # => " txt"
21
+ # puts text.friendly # => " Text Document"
22
+ # puts text.i18n_key # => " text.plain"
21
23
#
22
24
# puts text.encoding # => quoted-printable
23
25
# puts text.default_encoding # => quoted-printable
@@ -28,45 +30,45 @@ module MIME
28
30
# puts text.provisional? # => false
29
31
# puts text.complete? # => true
30
32
#
31
- # puts text # => ' text/plain'
33
+ # puts text # => " text/plain"
32
34
#
33
- # puts text == ' text/plain' # => true
34
- # puts ' text/plain' == text # => true
35
- # puts text == ' text/x-plain' # => false
36
- # puts ' text/x-plain' == text # => false
35
+ # puts text == " text/plain" # => true
36
+ # puts " text/plain" == text # => true
37
+ # puts text == " text/x-plain" # => false
38
+ # puts " text/x-plain" == text # => false
37
39
#
38
- # puts MIME::Type.simplified(' x-appl/x-zip' ) # => ' x-appl/x-zip'
39
- # puts MIME::Type.i18n_key(' x-appl/x-zip' ) # => ' x-appl.x-zip'
40
+ # puts MIME::Type.simplified(" x-appl/x-zip" ) # => " x-appl/x-zip"
41
+ # puts MIME::Type.i18n_key(" x-appl/x-zip" ) # => " x-appl.x-zip"
40
42
#
41
- # puts text.like?(' text/x-plain' ) # => true
42
- # puts text.like?(MIME::Type.new(' x-text/x-plain' )) # => true
43
+ # puts text.like?(" text/x-plain" ) # => true
44
+ # puts text.like?(MIME::Type.new("content-type" => " x-text/x-plain" )) # => true
43
45
#
44
46
# puts text.xrefs.inspect # => { "rfc" => [ "rfc2046", "rfc3676", "rfc5147" ] }
45
47
# puts text.xref_urls # => [ "http://www.iana.org/go/rfc2046",
46
48
# # "http://www.iana.org/go/rfc3676",
47
49
# # "http://www.iana.org/go/rfc5147" ]
48
50
#
49
- # xtext = MIME::Type.new(' x-text/x-plain' )
50
- # puts xtext.media_type # => ' text'
51
- # puts xtext.raw_media_type # => ' x-text'
52
- # puts xtext.sub_type # => ' plain'
53
- # puts xtext.raw_sub_type # => ' x-plain'
51
+ # xtext = MIME::Type.new(" x-text/x-plain" )
52
+ # puts xtext.media_type # => " text"
53
+ # puts xtext.raw_media_type # => " x-text"
54
+ # puts xtext.sub_type # => " plain"
55
+ # puts xtext.raw_sub_type # => " x-plain"
54
56
# puts xtext.complete? # => false
55
57
#
56
- # puts MIME::Types.any? { |type| type.content_type == ' text/plain' } # => true
58
+ # puts MIME::Types.any? { |type| type.content_type == " text/plain" } # => true
57
59
# puts MIME::Types.all?(&:registered?) # => false
58
60
#
59
61
# # Various string representations of MIME types
60
- # qcelp = MIME::Types[' audio/QCELP' ].first # => audio/QCELP
61
- # puts qcelp.content_type # => ' audio/QCELP'
62
- # puts qcelp.simplified # => ' audio/qcelp'
62
+ # qcelp = MIME::Types[" audio/QCELP" ].first # => audio/QCELP
63
+ # puts qcelp.content_type # => " audio/QCELP"
64
+ # puts qcelp.simplified # => " audio/qcelp"
63
65
#
64
- # xwingz = MIME::Types[' application/x-Wingz' ].first # => application/x-Wingz
65
- # puts xwingz.content_type # => ' application/x-Wingz'
66
- # puts xwingz.simplified # => ' application/x-wingz'
66
+ # xwingz = MIME::Types[" application/x-Wingz" ].first # => application/x-Wingz
67
+ # puts xwingz.content_type # => " application/x-Wingz"
68
+ # puts xwingz.simplified # => " application/x-wingz"
67
69
class MIME ::Type
68
70
# Reflects a MIME content-type specification that is not correctly
69
- # formatted (it isn't +type+/+subtype+).
71
+ # formatted (it is not +type+/+subtype+).
70
72
class InvalidContentType < ArgumentError
71
73
# :stopdoc:
72
74
def initialize ( type_string )
@@ -93,14 +95,20 @@ def to_s
93
95
end
94
96
95
97
# The released version of the mime-types library.
96
- VERSION = "3.5.2 "
98
+ VERSION = "3.6.0 "
97
99
98
100
include Comparable
99
101
100
102
# :stopdoc:
101
- # TODO verify mime-type character restrictions; I am pretty sure that this is
102
- # too wide open.
103
- MEDIA_TYPE_RE = %r{([-\w .+]+)/([-\w .+]*)} . freeze
103
+ # Full conformance with RFC 6838 §4.2 (the recommendation for < 64 characters is not
104
+ # enforced or reported because MIME::Types mostly deals with registered data). RFC 4288
105
+ # §4.2 does not restrict the first character to alphanumeric, but the total length of
106
+ # each part is limited to 127 characters. RFCC 2045 §5.1 does not restrict the character
107
+ # composition except for whitespace, but MIME::Type was always more strict than this.
108
+ restricted_name_first = "[0-9a-zA-Z]"
109
+ restricted_name_chars = "[-!#{ $&} ^_.+0-9a-zA-Z]{0,126}"
110
+ restricted_name = "#{ restricted_name_first } #{ restricted_name_chars } "
111
+ MEDIA_TYPE_RE = %r{(#{ restricted_name } )/(#{ restricted_name } )} . freeze
104
112
I18N_RE = /[^[:alnum:]]/ . freeze
105
113
BINARY_ENCODINGS = %w[ base64 8bit ] . freeze
106
114
ASCII_ENCODINGS = %w[ 7bit quoted-printable ] . freeze
@@ -110,12 +118,15 @@ def to_s
110
118
:ASCII_ENCODINGS
111
119
112
120
# Builds a MIME::Type object from the +content_type+, a MIME Content Type
113
- # value (e.g., ' text/plain' or ' application/x-eruby' ). The constructed object
121
+ # value (e.g., " text/plain" or " application/x-eruby" ). The constructed object
114
122
# is yielded to an optional block for additional configuration, such as
115
123
# associating extensions and encoding information.
116
124
#
117
125
# * When provided a Hash or a MIME::Type, the MIME::Type will be
118
126
# constructed with #init_with.
127
+ #
128
+ # There are two deprecated initialization forms:
129
+ #
119
130
# * When provided an Array, the MIME::Type will be constructed using
120
131
# the first element as the content type and the remaining flattened
121
132
# elements as extensions.
@@ -132,11 +143,23 @@ def initialize(content_type) # :yields: self
132
143
when Hash
133
144
init_with ( content_type )
134
145
when Array
146
+ MIME ::Types . deprecated (
147
+ class : MIME ::Type ,
148
+ method : :new ,
149
+ pre : "when called with an Array" ,
150
+ once : true
151
+ )
135
152
self . content_type = content_type . shift
136
153
self . extensions = content_type . flatten
137
154
when MIME ::Type
138
155
init_with ( content_type . to_h )
139
156
else
157
+ MIME ::Types . deprecated (
158
+ class : MIME ::Type ,
159
+ method : :new ,
160
+ pre : "when called with a String" ,
161
+ once : true
162
+ )
140
163
self . content_type = content_type
141
164
end
142
165
@@ -181,7 +204,7 @@ def <=>(other)
181
204
# comparisons involved are:
182
205
#
183
206
# 1. self.simplified <=> other.simplified (ensures that we
184
- # don't try to compare different types)
207
+ # do not try to compare different types)
185
208
# 2. IANA-registered definitions < other definitions.
186
209
# 3. Complete definitions < incomplete definitions.
187
210
# 4. Current definitions < obsolete definitions.
@@ -243,7 +266,7 @@ def eql?(other)
243
266
# +a.simplified+.
244
267
#
245
268
# Presumably, if <code>a.simplified <=> b.simplified</code> is +0+, then
246
- # +a.simplified+ has the same hash as +b.simplified+. So we assume it's
269
+ # +a.simplified+ has the same hash as +b.simplified+. So we assume it is
247
270
# suitable for #hash to delegate to #simplified in service of the #eql?
248
271
# invariant.
249
272
def hash
@@ -319,7 +342,7 @@ def add_extensions(*extensions)
319
342
# exceptions defined, the first extension will be used.
320
343
#
321
344
# When setting #preferred_extensions, if #extensions does not contain this
322
- # extension, this will be added to #xtensions .
345
+ # extension, this will be added to #extensions .
323
346
#
324
347
# :attr_accessor: preferred_extension
325
348
@@ -330,7 +353,9 @@ def preferred_extension
330
353
331
354
##
332
355
def preferred_extension = ( value ) # :nodoc:
333
- add_extensions ( value ) if value
356
+ if value
357
+ add_extensions ( value )
358
+ end
334
359
@preferred_extension = value
335
360
end
336
361
@@ -343,7 +368,7 @@ def preferred_extension=(value) # :nodoc:
343
368
# provided is invalid.
344
369
#
345
370
# If the encoding is not provided on construction, this will be either
346
- # ' quoted-printable' (for text/* media types) and ' base64' for eveything
371
+ # " quoted-printable" (for text/* media types) and " base64" for eveything
347
372
# else.
348
373
#
349
374
# :attr_accessor: encoding
@@ -393,7 +418,7 @@ def use_instead
393
418
#
394
419
# call-seq:
395
420
# text_plain.friendly # => "Text File"
396
- # text_plain.friendly('en' ) # => "Text File"
421
+ # text_plain.friendly("en" ) # => "Text File"
397
422
def friendly ( lang = "en" )
398
423
@friendly ||= { }
399
424
@@ -486,7 +511,7 @@ def to_s
486
511
# Returns the MIME::Type as a string for implicit conversions. This allows
487
512
# MIME::Type objects to appear on either side of a comparison.
488
513
#
489
- # ' text/plain' == MIME::Type.new(' text/plain' )
514
+ # " text/plain" == MIME::Type.new("content-type" => " text/plain" )
490
515
def to_str
491
516
content_type
492
517
end
@@ -627,7 +652,7 @@ def intern_string(string)
627
652
-string
628
653
end
629
654
else
630
- # MRI 2.2 and older don't have a method for string interning,
655
+ # MRI 2.2 and older do not have a method for string interning,
631
656
# so we simply freeze them for keeping a similar interface
632
657
def intern_string ( string )
633
658
string . freeze
0 commit comments