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

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.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
ascii?    binary?    complete?    default_encoding    from_array    from_hash    from_mime_type    like?    new    platform?    registered?    signature?    simplified    system?    to_a    to_hash    to_s    to_str   
Attributes
content_type  [R]  Returns the whole MIME content-type string.
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.
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
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.
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.
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, adds to the confusing proliferation of MIME types. The simplified string has the x- removed and are translated to lowercase.

from_array(*args) {|m if block_given?| ...}

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'])

See MIME::Type.new for more information.

from_hash(hash) {|m if block_given?| ...}

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.

Acceptable 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'])

See MIME::Type.new for more information.

from_mime_type(mime_type) {|m if block_given?| ...}

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

MIME::Type
Equivalent to a copy constructor.
  MIME::Type.new(plaintext)
new(content_type) {|self if block_given?| ...}

MIME::Types are constructed with an alternative object construction technique where self is +yield+ed after initial construction.

  MIME::Type.new('application/x-eruby') do |t|
    t.extensions = 'rhtml'
    t.encoding = '8bit'
  end

Changes

In MIME::Types 1.07, the constructor accepted more argument types and called #instance_eval on the optional block provided. This is no longer the case as of 1.13. The full changes are noted below.

  1. The constructor +yield+s self instead of using #instance_eval and that the calls to #init_extensions, #init_encoding, and #init_system have been eliminated.
  2. MIME::Type.new no longer accepts a MIME::Type argument. Use MIME::Type.from_mime_type instead.
     # 1.07
      MIME::Type.new(plaintext)
        # 1.12
      MIME::Type.from_mime_type(plaintext)
    
  3. MIME::Type.new no longer accepts an Array argument. Use MIME::Type.from_array instead.
     # 1.07
      MIME::Type.new(["application/x-ruby", ["rb"], "8bit"])
        # 1.12
      MIME::Type.from_array("application/x-ruby", ['rb'], '8bit')
      MIME::Type.from_array(["application/x-ruby", ['rb'], '8bit'])
    
  4. MIME::Type.new no longer accepts a Hash argument. Use MIME::Type.from_hash instead.
     # 1.07
      MIME::Type.new('Content-Type' => 'text/x-yaml',
                     'Content-Transfer-Encoding' => '8bit',
                     'System' => 'linux',
                     'Extensions' => ['yaml', 'yml'])
        # 1.12
      MIME::Type.from_hash('Content-Type' => 'text/x-yaml',
                           'Content-Transfer-Encoding' => '8bit',
                           'System' => 'linux',
                           'Extensions' => ['yaml', 'yml'])
    
NOTE:If the encoding is not specified, then it will be defaulted to ‘quoted-printable’ for ‘text/*’ media types and ‘base64’ for every other type. See #encoding for more information.
Public Instance methods
like?(other)

Returns true if the simplified type matches the current

default_encoding()

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

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.

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.

signature?()

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

system?()

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

platform?()

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

complete?()

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

to_s()

Returns the MIME type as a string.

to_str()

Returns the MIME type as a string for implicit conversions.

to_a()

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

to_hash()

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