struct MIME::Type

Overview

The definition of one MIME content-type.

Usage

require "mime/types"
plaintext = MIME::Types["text/plain"]
# returns [text/plain, text/plain]
text = plaintext.first
print text.media_type # => "text"
print text.sub_type   # => "plain"

puts text.extensions.join(" ") # => "asc txt c cc h hh cpp"

puts text.encoding                         # => 8bit
puts text.binary?                          # => false
puts text.ascii?                           # => true
puts text == "text/plain"                  # => true
puts MIME::Type.simplified("x-appl/x-zip") # => "appl/zip"

Included Modules

Defined in:

mime/type.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(content_type : String, extensions : Enumerable(String) = [] of String, *, encoding : String | Nil = nil, signature : Bool = false, registered : Bool = false, obsolete : Bool = false, docs : Nil | String = nil, xrefs : MIME::Type::XRefMap = XRefMap.new, friendly : Hash(String, String) = {} of String => String, use_instead : Nil | String = nil) #

Builds a MIME::Type object from the content_type, a 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.

  • When provided a Hash or a MIME::Type, the MIME::Type will be constructed with #init_with.
  • When provided an Array, the MIME::Type will be constructed using the first element as the content type and the remaining flattened elements as extensions.
  • Otherwise, the content_type will be used as a string.

Yields the newly constructed self object.


[View source]

Class Method Detail

def self.i18n_key(content_type) #

Converts a provided content_type into a translation key suitable for use with the I18n library.


[View source]
def self.match(content_type : String) #

Return a MatchData object of the content_type against pattern of media types.


[View source]
def self.simplified(content_type, remove_x = false) #

MIME media types are case-insensitive, but are typically presented in a case-preserving format in the type registry. This method converts content_type to lowercase.

In previous versions of mime-types, this would also remove any extension prefix (x-). This is no longer default behaviour, but may be provided by providing a truth value to remove_x_prefix.


[View source]

Instance Method Detail

def <=>(other : Type) #

Compares the other 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.


[View source]
def <=>(other : Nil) #

[View source]
def <=>(other) #

[View source]
def ==(other : Type) #

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


[View source]
def ==(other : String) #

[View source]
def ==(other) #
Description copied from struct Struct

Returns true if this struct is equal to other.

Both structs' instance vars are compared to each other. Thus, two structs are considered equal if each of their instance variables are equal. Subclasses should override this method to provide specific equality semantics.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p2 = Point.new 1, 2
p3 = Point.new 3, 4

p1 == p2 # => true
p1 == p3 # => false

[View source]
def 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.


[View source]
def 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.


[View source]
def complete? #

[View source]
def content_type : String #

Returns the whole MIME content-type string.

The content type is a presentation value from the MIME type registry and should not be used for comparison. The case of the content type is preserved, and extension markers (x-) are kept.

text/plain => text/plain x-chemical/x-pdb => x-chemical/x-pdb audio/QCELP => audio/QCELP


[View source]
def docs : String | Nil #

[View source]
def encoding : String #

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.


[View source]
def eql?(other : Type) #

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


[View source]
def eql?(other) #

[View source]
def extensions : Set(String) #

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, nil values removed, and made unique.


[View source]
def friendly(lang : String = "en") #

[View source]
def hash(hasher) #
Description copied from struct Struct

See Object#hash(hasher)


[View source]
def inspect(io) #

[View source]
def like?(other) #

Indicates that a MIME type is like another type. This differs from == because x- prefixes are removed for this comparison.


[View source]
def media_type #

Returns the media type of the simplified MIME::Type.

  • text/plain => text
  • x-chemical/x-pdb => x-chemical
  • audio/QCELP => audio

[View source]
def obsolete : Bool #

[View source]
def obsolete? : Bool #

Returns true if the media type is obsolete.


[View source]
def preferred_extension #

The preferred extension for this MIME type. If one is not set and there are exceptions defined, the first extension will be used.

When setting #preferred_extensions, if #extensions does not contain this extension, this will be added to #xtensions.


[View source]
def preferred_extension=(ext : String) #

[View source]
def priority_compare(other : Type) #

Compares the other 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. Complete definitions < incomplete definitions.
  4. Current definitions < obsolete definitions.
  5. Obselete with use-instead names < obsolete without.
  6. Obsolete use-instead definitions are compared.

While this method is public, its use is strongly discouraged by consumers of mime-types. In mime-types 3, this method is likely to see substantial revision and simplification to ensure current registered content types sort before unregistered or obsolete content types.


[View source]
def priority_compare(other : String) #

[View source]
def raw_media_type #

Returns the media type of the unmodified MIME::Type.

  • text/plain => text
  • x-chemical/x-pdb => x-chemical
  • audio/QCELP => audio

[View source]
def raw_sub_type #

Returns the media type of the unmodified MIME::Type.

  • text/plain => plain
  • x-chemical/x-pdb => x-pdb
  • audio/QCELP => QCELP

[View source]
def registered : Bool #

[View source]
def registered? : Bool #

Indicates whether the MIME type has been registered with IANA.


[View source]
def set_friendly(name : String, lang : String = "en") #

[View source]
def signature : Bool #

[View source]
def signature? : Bool #

Indicateswhether the MIME type is declared as a signature type.


[View source]
def simplified(remove_x = false) #

A simplified form of the MIME content-type string, suitable for case-insensitive comparison, with any extension markers (x-</tt) removed and converted to lowercase.

  • text/plain => text/plain
  • x-chemical/x-pdb => x-chemical/x-pdb
  • audio/QCELP => audio/qcelp

[View source]
def sub_type #

Returns the sub-type of the simplified MIME::Type.

  • text/plain => plain
  • x-chemical/x-pdb => pdb
  • audio/QCELP => qcelp

[View source]
def to_s(io) #

[View source]
def use_instead #

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.


[View source]
def xref_urls #

The decoded cross-reference URL list for this MIME::Type.


[View source]
def xrefs : XRefMap #

[View source]