Class MIME::Types
In: lib/s33r/mimetypes.rb
Parent: Object

MIME::Types

MIME types are used in MIME-compliant communications, as in e-mail or HTTP traffic, to indicate the type of content which is transmitted. MIME::Types provides the ability for detailed information about MIME entities (provided as a set of MIME::Type objects) to be determined and used programmatically. There are many types defined by RFCs and vendors, so the list is long but not complete; don’t hesitate to ask to add additional information. This library follows the IANA collection of MIME types (see below for reference).

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.

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.obsolete?             # => false
 puts plaintext.registered?           # => true
 puts plaintext == 'text/plain'       # => true
 puts MIME::Type.simplified('x-appl/x-zip') # => 'appl/zip'

This module is built to conform to the MIME types of RFCs 2045 and 2231. It follows the official IANA registry at www.iana.org/assignments/media-types/ and ftp.iana.org/assignments/media-types with some unofficial types added from the the collection at www.ltsw.se/knbase/internet/mime.htp

This is originally based on Perl MIME::Types by Mark Overmeer.

Author

Copyright:Copyright © 2002 - 2006 by Austin Ziegler <austin@rubyforge.org>
Version:1.15
Based On:Perl MIME::Types, Copyright © 2001 - 2005 by Mark Overmeer <mimetypes@overmeer.net>.
Licence:Ruby’s, Perl Artistic, or GPL version 2 (or later)
See Also:www.iana.org/assignments/media-types/ www.ltsw.se/knbase/internet/mime.htp

Methods

[]   []   add   add   new   of   of   type_for   type_for  

Constants

VERSION = '1.15'   The released version of Ruby MIME::Types

Attributes

data_version  [R]  The data version.

Public Class methods

Returns a list of MIME::Type objects, which may be empty. The optional flag parameters are :complete (finds only complete MIME::Type objects) and :platform (finds only MIME::Types for the current platform). It is possible for multiple matches to be returned for either type (in the example below, ‘text/plain’ returns two values — one for the general case, and one for VMS systems.

  puts "\nMIME::Types['text/plain']"
  MIME::Types['text/plain'].each { |t| puts t.to_a.join(", ") }

  puts "\nMIME::Types[/^image/, :complete => true]"
  MIME::Types[/^image/, :complete => true].each do |t|
    puts t.to_a.join(", ")
  end

[Source]

     # File lib/s33r/mimetypes.rb, line 641
641:       def [](type_id, flags = {})
642:         @__types__[type_id, flags]
643:       end

Add one or more MIME::Type objects to the set of known types. Each type should be experimental (e.g., ‘application/x-ruby’). If the type is already known, a warning will be displayed.

Please inform the maintainer of this module when registered types are missing.

[Source]

     # File lib/s33r/mimetypes.rb, line 668
668:       def add(*types)
669:         @__types__.add(*types)
670:       end

[Source]

     # File lib/s33r/mimetypes.rb, line 533
533:     def initialize(data_version = nil)
534:       @type_variants    = Hash.new { |h, k| h[k] = [] }
535:       @extension_index  = Hash.new { |h, k| h[k] = [] }
536:     end

A synonym for MIME::Types.type_for

[Source]

     # File lib/s33r/mimetypes.rb, line 658
658:       def of(filename, platform = false)
659:         @__types__.type_for(filename, platform)
660:       end

Return the list of MIME::Types which belongs to the file based on its filename extension. If platform is true, then only file types that are specific to the current platform will be returned.

  puts "MIME::Types.type_for('citydesk.xml')
    => "#{MIME::Types.type_for('citydesk.xml')}"
  puts "MIME::Types.type_for('citydesk.gif')
    => "#{MIME::Types.type_for('citydesk.gif')}"

[Source]

     # File lib/s33r/mimetypes.rb, line 653
653:       def type_for(filename, platform = false)
654:         @__types__.type_for(filename, platform)
655:       end

Public Instance methods

Returns a list of MIME::Type objects, which may be empty. The optional flag parameters are :complete (finds only complete MIME::Type objects) and :platform (finds only MIME::Types for the current platform). It is possible for multiple matches to be returned for either type (in the example below, ‘text/plain’ returns two values — one for the general case, and one for VMS systems.

  puts "\nMIME::Types['text/plain']"
  MIME::Types['text/plain'].each { |t| puts t.to_a.join(", ") }

  puts "\nMIME::Types[/^image/, :complete => true]"
  MIME::Types[/^image/, :complete => true].each do |t|
    puts t.to_a.join(", ")
  end

[Source]

     # File lib/s33r/mimetypes.rb, line 562
562:     def [](type_id, flags = {})
563:       if type_id.kind_of?(Regexp)
564:         matches = []
565:         @type_variants.each_key do |k|
566:           matches << @type_variants[k] if k =~ type_id
567:         end
568:         matches.flatten!
569:       elsif type_id.kind_of?(MIME::Type)
570:         matches = [type_id]
571:       else
572:         matches = @type_variants[MIME::Type.simplified(type_id)]
573:       end
574: 
575:       matches.delete_if { |e| not e.complete? } if flags[:complete]
576:       matches.delete_if { |e| not e.platform? } if flags[:platform]
577:       matches
578:     end

Add one or more MIME::Type objects to the set of known types. Each type should be experimental (e.g., ‘application/x-ruby’). If the type is already known, a warning will be displayed.

Please inform the maintainer of this module when registered types are missing.

[Source]

     # File lib/s33r/mimetypes.rb, line 606
606:     def add(*types)
607:       types.each do |mime_type|
608:         if @type_variants.include?(mime_type.simplified)
609:           if @type_variants[mime_type.simplified].include?(mime_type)
610:             warn "Type #{mime_type} already registered as a variant of #{mime_type.simplified}."
611:           end
612:         end
613:         add_type_variant(mime_type)
614:         index_extensions(mime_type)
615:       end
616:     end

A synonym for MIME::Types.type_for

[Source]

     # File lib/s33r/mimetypes.rb, line 596
596:     def of(filename, platform = false)
597:       type_for(filename, platform)
598:     end

Return the list of MIME::Types which belongs to the file based on its filename extension. If platform is true, then only file types that are specific to the current platform will be returned.

  puts "MIME::Types.type_for('citydesk.xml')
    => "#{MIME::Types.type_for('citydesk.xml')}"
  puts "MIME::Types.type_for('citydesk.gif')
    => "#{MIME::Types.type_for('citydesk.gif')}"

[Source]

     # File lib/s33r/mimetypes.rb, line 588
588:     def type_for(filename, platform = false)
589:       ext = filename.chomp.downcase.gsub(/.*\./o, '')
590:       list = @extension_index[ext]
591:       list.delete_if { |e| not e.platform? } if platform
592:       list
593:     end

[Validate]