Parent

Included Modules

Class Index [+]

Quicksearch

MIME::Type

The definition of one MIME content-type.

Usage

 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'

Constants

VERSION

The released version of Ruby MIME::Types

MEDIA_TYPE_RE
UNREG_RE
ENCODING_RE
PLATFORM_RE
SIGNATURES
IANA_URL
RFC_URL
DRAFT_URL
LTSW_URL
CONTACT_URL

Attributes

content_type[R]

Returns the whole MIME content-type string.

  text/plain        => text/plain
  x-chemical/x-pdb  => x-chemical/x-pdb
media_type[R]

Returns the media type of the simplified MIME type.

  text/plain        => text
  x-chemical/x-pdb  => chemical
raw_media_type[R]

Returns the media type of the unmodified MIME type.

  text/plain        => text
  x-chemical/x-pdb  => x-chemical
sub_type[R]

Returns the sub-type of the simplified MIME type.

  text/plain        => plain
  x-chemical/x-pdb  => pdb
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
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 #. Array values will be flattened and nil values removed.

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 # to the # 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.

system[RW]

The regexp for the operating system that this MIME::Type is specific to.

default_encoding[R]

Returns the default encoding for the MIME::Type based on the media type.

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.

obsolete[W]

Sets the obsolescence indicator for this 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 #.

url[RW]

The encoded URL list for this MIME::Type. See # for more information.

Public Class Methods

from_array(*args) click to toggle source

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/mime/types.rb, line 318
318:       def from_array(*args) #:yields MIME::Type.new:
319:         # Dereferences the array one level, if necessary.
320:         args = args[0] if args[0].kind_of?(Array)
321: 
322:         if args.size.between?(1, 8)
323:           m = MIME::Type.new(args[0]) do |t|
324:             t.extensions  = args[1] if args.size > 1
325:             t.encoding    = args[2] if args.size > 2
326:             t.system      = args[3] if args.size > 3
327:             t.obsolete    = args[4] if args.size > 4
328:             t.docs        = args[5] if args.size > 5
329:             t.url         = args[6] if args.size > 6
330:             t.registered  = args[7] if args.size > 7
331:           end
332:           yield m if block_given?
333:         else
334:           raise ArgumentError, "Array provided must contain between one and eight elements."
335:         end
336:         m
337:       end
from_hash(hash) click to toggle source

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/mime/types.rb, line 361
361:       def from_hash(hash) #:yields MIME::Type.new:
362:         type = {}
363:         hash.each_pair do |k, v|
364:           type[k.to_s.tr('A-Z', 'a-z').gsub(/-/, '_').to_sym] = v
365:         end
366: 
367:         m = MIME::Type.new(type[:content_type]) do |t|
368:           t.extensions  = type[:extensions]
369:           t.encoding    = type[:content_transfer_encoding]
370:           t.system      = type[:system]
371:           t.obsolete    = type[:obsolete]
372:           t.docs        = type[:docs]
373:           t.url         = type[:url]
374:           t.registered  = type[:registered]
375:         end
376: 
377:         yield m if block_given?
378:         m
379:       end
from_mime_type(mime_type) click to toggle source

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/mime/types.rb, line 392
392:       def from_mime_type(mime_type) #:yields the new MIME::Type:
393:         m = MIME::Type.new(mime_type.content_type.dup) do |t|
394:           t.extensions = mime_type.extensions.map { |e| e.dup }
395:           t.url = mime_type.url && mime_type.url.map { |e| e.dup }
396: 
397:           mime_type.system && t.system = mime_type.system.dup
398:           mime_type.encoding && t.encoding = mime_type.encoding.dup
399: 
400:           t.obsolete = mime_type.obsolete?
401:           t.registered = mime_type.registered?
402: 
403:           mime_type.docs && t.docs = mime_type.docs.dup
404: 
405:         end
406: 
407:         yield m if block_given?
408:       end
new(content_type) click to toggle source

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/mime/types.rb, line 415
415:     def initialize(content_type) #:yields self:
416:       matchdata = MEDIA_TYPE_RE.match(content_type)
417: 
418:       if matchdata.nil?
419:         raise InvalidContentType, "Invalid Content-Type provided ('#{content_type}')"
420:       end
421: 
422:       @content_type = content_type
423:       @raw_media_type = matchdata.captures[0]
424:       @raw_sub_type = matchdata.captures[1]
425: 
426:       @simplified = MIME::Type.simplified(@content_type)
427:       matchdata = MEDIA_TYPE_RE.match(@simplified)
428:       @media_type = matchdata.captures[0]
429:       @sub_type = matchdata.captures[1]
430: 
431:       self.extensions   = nil
432:       self.encoding     = :default
433:       self.system       = nil
434:       self.registered   = true
435:       self.url          = nil
436:       self.obsolete     = nil
437:       self.docs         = nil
438: 
439:       yield self if block_given?
440:     end
simplified(content_type) click to toggle source

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/mime/types.rb, line 291
291:       def simplified(content_type)
292:         matchdata = MEDIA_TYPE_RE.match(content_type)
293: 
294:         if matchdata.nil?
295:           simplified = nil
296:         else
297:           media_type = matchdata.captures[0].downcase.gsub(UNREG_RE, '')
298:           subtype = matchdata.captures[1].downcase.gsub(UNREG_RE, '')
299:           simplified = "#{media_type}/#{subtype}"
300:         end
301:         simplified
302:       end

Public Instance Methods

<=>(other) click to toggle source

Compares the MIME::Type against the exact content type or the simplified type (the simplified type will be used if comparing against something that can be treated as a String with #). In comparisons, this is done against the lowercase version of the MIME::Type.

    # File lib/mime/types.rb, line 61
61:     def <=>(other)
62:       if other.respond_to?(:content_type)
63:         @content_type.downcase <=> other.content_type.downcase
64:       elsif other.respond_to?(:to_s)
65:         @simplified <=> Type.simplified(other.to_s)
66:       else
67:         @content_type.downcase <=> other.downcase
68:       end
69:     end
ascii?() click to toggle source

MIME types can be specified to be sent across a network in particular formats. This method returns false when the MIME type encoding is set to base64.

     # File lib/mime/types.rb, line 466
466:     def ascii?
467:       not binary?
468:     end
binary?() click to toggle source

MIME types can be specified to be sent across a network in particular formats. This method returns true when the MIME type encoding is set to base64.

     # File lib/mime/types.rb, line 459
459:     def binary?
460:       @encoding == 'base64'
461:     end
complete?() click to toggle source

Returns true if the MIME::Type specifies an extension list, indicating that it is a complete MIME::Type.

     # File lib/mime/types.rb, line 489
489:     def complete?
490:       not @extensions.empty?
491:     end
default_encoding() click to toggle source
     # File lib/mime/types.rb, line 201
201:     def default_encoding
202:       (@media_type == 'text') ? 'quoted-printable' : 'base64'
203:     end
docs=(d) click to toggle source
     # File lib/mime/types.rb, line 227
227:     def docs=(d)
228:       if d
229:         a = d.scan(%{use-instead:#{MEDIA_TYPE_RE}})
230: 
231:         if a.empty?
232:           @use_instead = nil
233:         else
234:           @use_instead = a.map { |el| "#{el[0]}/#{el[1]}" }
235:         end
236:       end
237:       @docs = d
238:     end
eql?(other) click to toggle source

Returns true if the other object is a MIME::Type and the content types match.

     # File lib/mime/types.rb, line 117
117:     def eql?(other)
118:       other.kind_of?(MIME::Type) and self == other
119:     end
like?(other) click to toggle source

Returns true if the simplified type matches the current

    # File lib/mime/types.rb, line 49
49:     def like?(other)
50:       if other.respond_to?(:simplified)
51:         @simplified == other.simplified
52:       else
53:         @simplified == Type.simplified(other)
54:       end
55:     end
obsolete?() click to toggle source

Returns true if the media type is obsolete.

     # File lib/mime/types.rb, line 216
216:     def obsolete?
217:       @obsolete ? true : false
218:     end
platform?() click to toggle source

Returns true if the MIME::Type is specific to the current operating system as represented by RUBY_PLATFORM.

     # File lib/mime/types.rb, line 483
483:     def platform?
484:       system? and (RUBY_PLATFORM =~ @system)
485:     end
priority_compare(other) click to toggle source

Compares the MIME::Type based on how reliable it is before doing a normal <=> comparison. Used by MIME::Types#[] to sort types. The comparisons involved are:

  1. self.simplified <=> other.simplified (ensures that we don’t try to compare different types)

  2. IANA-registered definitions > other definitions.

  3. Generic definitions > platform definitions.

  4. Complete definitions > incomplete definitions.

  5. Current definitions > obsolete definitions.

  6. Obselete with use-instead references > obsolete without.

  7. Obsolete use-instead definitions are compared.

     # File lib/mime/types.rb, line 83
 83:     def priority_compare(other)
 84:       pc = simplified <=> other.simplified
 85: 
 86:       if pc.zero? and registered? != other.registered?
 87:         pc = registered? ? 1 : 1
 88:       end
 89: 
 90:       if pc.zero? and platform? != other.platform?
 91:         pc = platform? ? 1 : 1
 92:       end
 93: 
 94:       if pc.zero? and complete? != other.complete?
 95:         pc = complete? ? 1 : 1
 96:       end
 97: 
 98:       if pc.zero? and obsolete? != other.obsolete?
 99:         pc = obsolete? ? 1 : 1
100:       end
101: 
102:       if pc.zero? and obsolete? and (use_instead != other.use_instead)
103:         pc = if use_instead.nil?
104:                1
105:              elsif other.use_instead.nil?
106:                1
107:              else
108:                use_instead <=> other.use_instead
109:              end
110:       end
111: 
112:       pc
113:     end
registered?() click to toggle source

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/mime/types.rb, line 447
447:     def registered?
448:       if (@raw_media_type =~ UNREG_RE) || (@raw_sub_type =~ UNREG_RE)
449:         false
450:       else
451:         @registered
452:       end
453:     end
signature?() click to toggle source

Returns true when the simplified MIME type is in the list of known digital signatures.

     # File lib/mime/types.rb, line 472
472:     def signature?
473:       SIGNATURES.include?(@simplified.downcase)
474:     end
system?() click to toggle source

Returns true if the MIME::Type is specific to an operating system.

     # File lib/mime/types.rb, line 477
477:     def system?
478:       not @system.nil?
479:     end
to_a() click to toggle source

Returns the MIME type as an array suitable for use with MIME::Type.from_array.

     # File lib/mime/types.rb, line 505
505:     def to_a
506:       [ @content_type, @extensions, @encoding, @system, @obsolete, @docs,
507:         @url, registered? ]
508:     end
to_hash() click to toggle source

Returns the MIME type as an array suitable for use with MIME::Type.from_hash.

     # File lib/mime/types.rb, line 512
512:     def to_hash
513:       { 'Content-Type'              => @content_type,
514:         'Content-Transfer-Encoding' => @encoding,
515:         'Extensions'                => @extensions,
516:         'System'                    => @system,
517:         'Obsolete'                  => @obsolete,
518:         'Docs'                      => @docs,
519:         'URL'                       => @url,
520:         'Registered'                => registered?,
521:       }
522:     end
to_s() click to toggle source

Returns the MIME type as a string.

     # File lib/mime/types.rb, line 494
494:     def to_s
495:       @content_type
496:     end
to_str() click to toggle source

Returns the MIME type as a string for implicit conversions.

     # File lib/mime/types.rb, line 499
499:     def to_str
500:       @content_type
501:     end
urls() click to toggle source

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 #, which always returns an array.

     # File lib/mime/types.rb, line 262
262:     def urls
263:       @url.map do |el|
264:         case el
265:         when %{^IANA$}
266:           IANA_URL % [ @media_type, @sub_type ]
267:         when %{^RFC(\d+)$}
268:           RFC_URL % $1
269:         when %{^DRAFT:(.+)$}
270:           DRAFT_URL % $1
271:         when %{^LTSW$}
272:           LTSW_URL % @media_type
273:         when %{^\{([^=]+)=([^\}]+)\}}
274:           [$1, $2]
275:         when %{^\[([^=]+)=([^\]]+)\]}
276:           [$1, CONTACT_URL % $2]
277:         when %{^\[([^\]]+)\]}
278:           CONTACT_URL % $1
279:         else
280:           el
281:         end
282:       end
283:     end
use_instead() click to toggle source
     # File lib/mime/types.rb, line 210
210:     def use_instead
211:       return nil unless @obsolete
212:       @use_instead
213:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.