Class MIME::Type
In: lib/mime/types.rb
Parent: Object

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'

Methods

Included Modules

Comparable

Constants

VERSION = '1.16'
MEDIA_TYPE_RE = %r{([-\w.+]+)/([-\w.+]*)}o
UNREG_RE = %r{[Xx]-}o
ENCODING_RE = %r{(?:base64|7bit|8bit|quoted\-printable)}o
PLATFORM_RE = %r|#{RUBY_PLATFORM}|o
SIGNATURES = %w(application/pgp-keys application/pgp application/pgp-signature application/pkcs10 application/pkcs7-mime application/pkcs7-signature text/vcard)
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"

Attributes

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 more information.
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.

Public Class methods

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

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

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

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.

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.

Public Instance methods

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 to_s). In comparisons, this is done against the lowercase version of the MIME::Type.

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.

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.

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

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

Returns true if the simplified type matches the current

Returns true if the media type is obsolete.

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

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.

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.

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

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

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

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

Returns the MIME type as a string.

Returns the MIME type as a string for implicit conversions.

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

[Validate]