Class MIME::Type
In: mime/types.rb
Parent: Object
MIME Types RuntimeError InvalidContentType Comparable Type Module: MIME

Summary

Definition of one MIME content-type

Synopsis

 require 'MIME/types'
 plaintext = MIME::Types['text/plain']
 print plaintext.media_type                    # => 'text'
 print plaintext.sub_type                      # => 'plain'

 puts plaintext.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'

Description

MIME types are used in MIME entities, as in email or HTTP traffic. It is useful at times to have information available about MIME types (or, inversely, about files). A MIME::Type stores the known information about one MIME type.

Methods
ascii?    binary?    init_encoding    init_extensions    init_system    new    registered?    signature?    simplified   
Attributes
content_type  [R] 

Returns the whole MIME content-type string.

encoding  [R] 

Returns the type of encoding which is required to transport data of this type safely.

extensions  [R] 

Returns a list of extensions which are known to be used for this MIME type.

media_type  [R] 

Returns the media type of the simplified MIME type. For text/plain, it will return text.

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 which adds to the confusion. The simplified string has the x- removed and are translated to lowercase.

sub_type  [R] 

Returns the sub-type of the simplified MIME type. For text/plain, it will return plain.

system  [R] 

Returns the regular expression which can be used to determine whether this type is active on the system where you are working on.

Included modules
Comparable
Public Class methods
simplified(content_type)

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 which adds to the confusion. The simplified string has the x- removed and are translated to lowercase.

new(arg, &block)

This constructor takes advantage of a technique for Ruby object construction introduced by Andy Hunt and Dave Thomas (see reference), where optional values are set using commands in a block.

  MIME::Type.new("application/x-eruby") {
      init_extensions("rhtml")
      init_encoding("8bit")
  }

The available functions (which only work in the constructor block, as they are private) are:

#init_extensions:Sets the list of extensions known for a particular MIME content-type.
#init_encoding:Sets the encoding for a particular MIME content-type.
#init_system:Sets the operating system known for a particular MIME content-type. This value is used to allow for multiple definitions of some MIME content-types, where the standard extensions and/or encodings are different (e.g., text/plain on VMS systems).

arg may also be one of MIME::Type, Array, or Hash.

MIME::Type
Equivalent to a copy constructor.
  MIME::Type.new(plaintext)
Array
A four-element array in the form of: [type-name, [extensions], encoding, system]
  MIME::Type.new(["application/x-ruby", ["rb"], "8bit"])
Hash
A hash with the following keys:

Content-Type (or: content-type, content_type, type): String

Extensions (or: extensions): Array of String

Content-Transfer-Encoding (or: content-transfer-encoding, content_encoding, encoding): String

System (or: system): String

  MIME::Type.new('Content-Type' => 'text/x-yaml',
                 'Content-Transfer-Encoding' => '8bit',
                 'System' => 'linux',
                 'Extensions' => ['yaml', 'yml'])
Notes:If the encoding is not specified, then it will be defaulted to 'quoted-printable' for 'text/*' media types and 'base64' for every other type.
Reference:"Object Construction and Blocks" <www.pragmaticprogrammer.com/ruby/articles/insteval.html>
Public Instance methods
registered?()

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.

binary?()

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 or binary.

Notes:This is a slightly expanded test to the one defined in the Perl MIME::Type library, as it includes binary.
ascii?()

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 or binary.

Notes:This is a slightly different test than the one defined in the Perl MIME::Type library, as it is merely a reverse of #binary?.
signature?()

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

Private Instance methods
init_extensions(ext)

Sets the list of extensions known for a particular MIME content-type. Only works in constructor block.

init_encoding(enc)

Sets the encoding for a particular MIME content-type. Only works in constructor block.

init_system(os)

Sets the operating system known for a particular MIME content-type. This value is used to allow for multiple definitions of some MIME content-types, where the standard extensions and/or encodings are different (e.g., text/plain on VMS systems). Only works in constructor block.