struct MIME::Type
- MIME::Type
- Struct
- Value
- Object
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
- Comparable(MIME::Type)
Defined in:
mime/type.crConstructors
-
.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").
Class Method Summary
-
.i18n_key(content_type)
Converts a provided content_type into a translation key suitable for use with the I18n library.
-
.match(content_type : String)
Return a
MatchData
object of the content_type against pattern of media types. -
.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.
Instance Method Summary
-
#<=>(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).
- #<=>(other : Nil)
- #<=>(other)
-
#==(other : Type)
Returns true if the other object is a MIME::Type and the content types match.
- #==(other : String)
-
#==(other)
Returns
true
if this struct is equal to other. -
#ascii?
MIME types can be specified to be sent across a network in particular formats.
-
#binary?
MIME types can be specified to be sent across a network in particular formats.
- #complete?
-
#content_type : String
Returns the whole MIME content-type string.
- #docs : String | Nil
-
#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.
-
#eql?(other : Type)
Returns true if the other object is a MIME::Type and the content types match.
- #eql?(other)
-
#extensions : Set(String)
The list of extensions which are known to be used for this MIME::Type.
- #friendly(lang : String = "en")
-
#hash(hasher)
See
Object#hash(hasher)
- #inspect(io)
-
#like?(other)
Indicates that a MIME type is like another type.
-
#media_type
Returns the media type of the simplified MIME::Type.
- #obsolete : Bool
-
#obsolete? : Bool
Returns true if the media type is obsolete.
-
#preferred_extension
The preferred extension for this MIME type.
- #preferred_extension=(ext : String)
-
#priority_compare(other : Type)
Compares the other MIME::Type based on how reliable it is before doing a normal <=> comparison.
- #priority_compare(other : String)
-
#raw_media_type
Returns the media type of the unmodified MIME::Type.
-
#raw_sub_type
Returns the media type of the unmodified MIME::Type.
- #registered : Bool
-
#registered? : Bool
Indicates whether the MIME type has been registered with IANA.
- #set_friendly(name : String, lang : String = "en")
- #signature : Bool
-
#signature? : Bool
Indicateswhether the MIME type is declared as a signature type.
-
#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.
-
#sub_type
Returns the sub-type of the simplified MIME::Type.
- #to_s(io)
-
#use_instead
Returns the media type or types that should be used instead of this media type, if it is obsolete.
-
#xref_urls
The decoded cross-reference URL list for this MIME::Type.
- #xrefs : XRefMap
Constructor Detail
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
, theMIME::Type
will be constructed with #init_with. - When provided an
Array
, theMIME::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.
Class Method Detail
Converts a provided content_type into a translation key suitable for use with the I18n library.
Return a MatchData
object of the content_type against pattern of
media types.
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.
Instance Method Detail
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.
Returns true if the other object is a MIME::Type and the content types match.
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
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 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
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.
Returns true if the other object is a MIME::Type and the content types match.
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.
Indicates that a MIME type is like another type. This differs from == because x- prefixes are removed for this comparison.
Returns the media type of the simplified MIME::Type.
- text/plain => text
- x-chemical/x-pdb => x-chemical
- audio/QCELP => audio
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.
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:
- self.simplified <=> other.simplified (ensures that we don't try to compare different types)
- IANA-registered definitions < other definitions.
- Complete definitions < incomplete definitions.
- Current definitions < obsolete definitions.
- Obselete with use-instead names < obsolete without.
- 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.
Returns the media type of the unmodified MIME::Type.
- text/plain => text
- x-chemical/x-pdb => x-chemical
- audio/QCELP => audio
Returns the media type of the unmodified MIME::Type.
- text/plain => plain
- x-chemical/x-pdb => x-pdb
- audio/QCELP => QCELP
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
Returns the sub-type of the simplified MIME::Type.
- text/plain => plain
- x-chemical/x-pdb => pdb
- audio/QCELP => qcelp
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.