Class | MIME::Type |
In: |
lib/s33r/mimetypes.rb
|
Parent: | Object |
The definition of one MIME content-type.
require 'mime/types' plaintext = MIME::Types['text/plain'] print plaintext.media_type # => 'text' print plaintext.sub_type # => 'plain' puts plaintext.extensions.join(" ") # => 'asc txt c cc h hh cpp' puts plaintext.encoding # => 8bit puts plaintext.binary? # => false puts plaintext.ascii? # => true puts plaintext == 'text/plain' # => true puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'
VERSION | = | '1.15' |
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" |
LTSW_URL | = | "http://www.ltsw.se/knbase/internet/%s.htp" |
CONTACT_URL | = | "http://www.iana.org/assignments/contact-people.htm#%s" |
content_type | [R] |
Returns the whole MIME content-type string.
text/plain => text/plain x-chemical/x-pdb => x-chemical/x-pdb |
default_encoding | [R] | Returns the default encoding for the MIME::Type based on the media type. |
docs | [RW] | The documentation for this MIME::Type. Documentation about media types will be found on a media type definition as a comment. Documentation will be found through docs. |
encoding | [RW] |
The encoding (7bit, 8bit, quoted-printable, or base64) required to
transport the data of this content type safely across a network, which
roughly corresponds to Content-Transfer-Encoding. A value of nil
or :default will reset the encoding to the default_encoding for the MIME::Type. Raises ArgumentError if the encoding
provided is invalid.
If the encoding is not provided on construction, this will be either ‘quoted-printable’ (for text/* media types) and ‘base64’ for eveything else. |
extensions | [RW] | The list of extensions which are known to be used for this MIME::Type. Non-array values will be coerced into an array with to_a. Array values will be flattened and nil values removed. |
media_type | [R] |
Returns the media type of the simplified MIME
type.
text/plain => text x-chemical/x-pdb => chemical |
obsolete | [W] | Sets the obsolescence indicator for this media type. |
raw_media_type | [R] |
Returns the media type of the unmodified MIME
type.
text/plain => text x-chemical/x-pdb => x-chemical |
raw_sub_type | [R] |
Returns the media type of the unmodified MIME
type.
text/plain => plain x-chemical/x-pdb => x-pdb |
simplified | [R] |
The MIME types main- and sub-label can both
start with x-, which indicates that it is a non-registered name.
Of course, after registration this flag can disappear, adds to the
confusing proliferation of MIME types. The
simplified string has the x- removed and are translated to
lowercase.
text/plain => text/plain x-chemical/x-pdb => chemical/pdb |
sub_type | [R] |
Returns the sub-type of the simplified MIME
type.
text/plain => plain x-chemical/x-pdb => pdb |
system | [RW] | The regexp for the operating system that this MIME::Type is specific to. |
url | [RW] | The encoded URL list for this MIME::Type. See urls for |
use_instead | [R] | Returns the media type or types that should be used instead of this media type, if it is obsolete. If there is no replacement media type, or it is not obsolete, nil will be returned. |
Creates a MIME::Type from an array in the form of:
[type-name, [extensions], encoding, system]
extensions, encoding, and system are optional.
MIME::Type.from_array("application/x-ruby", ['rb'], '8bit') MIME::Type.from_array(["application/x-ruby", ['rb'], '8bit'])
These are equivalent to:
MIME::Type.new('application/x-ruby') do |t| t.extensions = %w(rb) t.encoding = '8bit' end
# File lib/s33r/mimetypes.rb, line 276 276: def from_array(*args) #:yields MIME::Type.new: 277: # Dereferences the array one level, if necessary. 278: args = args[0] if args[0].kind_of?(Array) 279: 280: if args.size.between?(1, 8) 281: m = MIME::Type.new(args[0]) do |t| 282: t.extensions = args[1] if args.size > 1 283: t.encoding = args[2] if args.size > 2 284: t.system = args[3] if args.size > 3 285: t.obsolete = args[4] if args.size > 4 286: t.docs = args[5] if args.size > 5 287: t.url = args[6] if args.size > 6 288: t.registered = args[7] if args.size > 7 289: end 290: yield m if block_given? 291: else 292: raise ArgumentError, "Array provided must contain between one and eight elements." 293: end 294: m 295: end
Creates a MIME::Type from a hash. Keys are case-insensitive, dashes may be replaced with underscores, and the internal Symbol of the lowercase-underscore version can be used as well. That is, Content-Type can be provided as content-type, Content_Type, content_type, or :content_type.
Known keys are Content-Type, Content-Transfer-Encoding, Extensions, and System.
MIME::Type.from_hash('Content-Type' => 'text/x-yaml', 'Content-Transfer-Encoding' => '8bit', 'System' => 'linux', 'Extensions' => ['yaml', 'yml'])
This is equivalent to:
MIME::Type.new('text/x-yaml') do |t| t.encoding = '8bit' t.system = 'linux' t.extensions = ['yaml', 'yml'] end
# File lib/s33r/mimetypes.rb, line 319 319: def from_hash(hash) #:yields MIME::Type.new: 320: type = {} 321: hash.each_pair do |k, v| 322: type[k.to_s.tr('-A-Z', '_a-z').to_sym] = v 323: end 324: 325: m = MIME::Type.new(type[:content_type]) do |t| 326: t.extensions = type[:extensions] 327: t.encoding = type[:content_transfer_encoding] 328: t.system = type[:system] 329: t.obsolete = type[:obsolete] 330: t.docs = type[:docs] 331: t.url = type[:url] 332: t.registered = type[:registered] 333: end 334: 335: yield m if block_given? 336: m 337: end
Essentially a copy constructor.
MIME::Type.from_mime_type(plaintext)
is equivalent to:
MIME::Type.new(plaintext.content_type.dup) do |t| t.extensions = plaintext.extensions.dup t.system = plaintext.system.dup t.encoding = plaintext.encoding.dup end
# File lib/s33r/mimetypes.rb, line 350 350: def from_mime_type(mime_type) #:yields the new MIME::Type: 351: m = MIME::Type.new(mime_type.content_type.dup) do |t| 352: t.extensions = mime_type.extensions.dup 353: t.system = mime_type.system.dup 354: t.encoding = mime_type.encoding.dup 355: end 356: 357: yield m if block_given? 358: end
Builds a MIME::Type object from the provided MIME Content Type value (e.g., ‘text/plain’ or ‘applicaton/x-eruby’). The constructed object is yielded to an optional block for additional configuration, such as associating extensions and encoding information.
# File lib/s33r/mimetypes.rb, line 365 365: def initialize(content_type) #:yields self: 366: matchdata = MEDIA_TYPE_RE.match(content_type) 367: 368: if matchdata.nil? 369: raise InvalidContentType, "Invalid Content-Type provided ('#{content_type}')" 370: end 371: 372: @content_type = content_type 373: @raw_media_type = matchdata.captures[0] 374: @raw_sub_type = matchdata.captures[1] 375: 376: @simplified = MIME::Type.simplified(@content_type) 377: matchdata = MEDIA_TYPE_RE.match(@simplified) 378: @media_type = matchdata.captures[0] 379: @sub_type = matchdata.captures[1] 380: 381: self.extensions = nil 382: self.encoding = :default 383: self.system = nil 384: self.registered = true 385: 386: yield self if block_given? 387: end
The MIME types main- and sub-label can both start with x-, which indicates that it is a non-registered name. Of course, after registration this flag can disappear, adds to the confusing proliferation of MIME types. The simplified string has the x- removed and are translated to lowercase.
# File lib/s33r/mimetypes.rb, line 249 249: def simplified(content_type) 250: matchdata = MEDIA_TYPE_RE.match(content_type) 251: 252: if matchdata.nil? 253: simplified = nil 254: else 255: media_type = matchdata.captures[0].downcase.gsub(UNREG_RE, '') 256: subtype = matchdata.captures[1].downcase.gsub(UNREG_RE, '') 257: simplified = "#{media_type}/#{subtype}" 258: end 259: simplified 260: end
Returns true if the MIME::Type specifies an extension list, indicating that it is a complete MIME::Type.
# File lib/s33r/mimetypes.rb, line 436 436: def complete? 437: not @extensions.empty? 438: end
# File lib/s33r/mimetypes.rb, line 165 165: def default_encoding 166: (@media_type == 'text') ? 'quoted-printable' : 'base64' 167: end
# File lib/s33r/mimetypes.rb, line 191 191: def docs=(d) 192: if d 193: a = d.scan(%r{use-instead:#{MEDIA_TYPE_RE}}) 194: 195: if a.empty? 196: @use_instead = nil 197: else 198: @use_instead = a.map { |el| "#{el[0]}/#{el[1]}" } 199: end 200: end 201: end
Returns true if the simplified type matches the current
# File lib/s33r/mimetypes.rb, line 58 58: def like?(other) 59: if other.respond_to?(:simplified) 60: @simplified == other.simplified 61: else 62: @simplified == Type.simplified(other) 63: end 64: end
Returns true if the media type is obsolete.
# File lib/s33r/mimetypes.rb, line 180 180: def obsolete? 181: @obsolete ? true : false 182: end
Returns true if the MIME::Type is specific to the current operating system as represented by RUBY_PLATFORM.
# File lib/s33r/mimetypes.rb, line 430 430: def platform? 431: system? and (RUBY_PLATFORM =~ @system) 432: end
MIME content-types which are not regestered by IANA nor defined in RFCs are required to start with x-. This counts as well for a new media type as well as a new sub-type of an existing media type. If either the media-type or the content-type begins with x-, this method will return false.
# File lib/s33r/mimetypes.rb, line 394 394: def registered? 395: if (@raw_media_type =~ UNREG_RE) || (@raw_sub_type =~ UNREG_RE) 396: false 397: else 398: @registered 399: end 400: end
Returns true if the MIME::Type is specific to an operating system.
# File lib/s33r/mimetypes.rb, line 424 424: def system? 425: not @system.nil? 426: end
Returns the MIME type as an array suitable for use with MIME::Type.from_array.
# File lib/s33r/mimetypes.rb, line 452 452: def to_a 453: [ @content_type, @extensions, @encoding, @system, @obsolete, @docs, 454: @url, registered? ] 455: end
Returns the MIME type as an array suitable for use with MIME::Type.from_hash.
# File lib/s33r/mimetypes.rb, line 459 459: def to_hash 460: { 'Content-Type' => @content_type, 461: 'Content-Transfer-Encoding' => @encoding, 462: 'Extensions' => @extensions, 463: 'System' => @system, 464: 'Obsolete' => @obsolete, 465: 'Docs' => @docs, 466: 'URL' => @url, 467: 'Registered' => registered?, 468: } 469: end
The decoded URL list for this MIME::Type. The special URL value IANA will be translated into:
http://www.iana.org/assignments/media-types/<mediatype>/<subtype>
The special URL value RFC### will be translated into:
http://www.rfc-editor.org/rfc/rfc###.txt
The special URL value DRAFT:name will be translated into:
https://datatracker.ietf.org/public/idindex.cgi? command=id_detail&filename=<name>
The special URL value LTSW will be translated into:
http://www.ltsw.se/knbase/internet/<mediatype>.htp
The special URL value [token] will be translated into:
http://www.iana.org/assignments/contact-people.htm#<token>
These values will be accessible through url, which always returns an array.
# File lib/s33r/mimetypes.rb, line 224 224: def urls 225: @url.map { |el| 226: case el 227: when %r{^IANA$} 228: IANA_URL % [ @media_type, @sub_type ] 229: when %r{^RFC(\d+)$} 230: RFC_URL % $1 231: when %r{^DRAFT:(.+)$} 232: DRAFT_URL % $1 233: when %r{^LTSW$} 234: LTSW_URL % @media_type 235: when %r{^\[([^\]]+)\]} 236: CONTACT_URL % $1 237: else 238: el 239: end 240: } 241: end