Class: Webbed::MediaType
- Inherits:
-
Object
- Object
- Webbed::MediaType
- Defined in:
- lib/webbed/media_type.rb
Overview
Representation of an HTTP Media Type.
Constant Summary collapse
- MIME_TYPE_REGEX =
/^([-\w.+]+)\/([-\w.+]*)$/
- PARAMETERS_REGEX =
/=|\s*;\s*/
Instance Attribute Summary collapse
-
#parameters ⇒ Hash{String => String}
The parameters of the Media Type.
-
#subtype ⇒ String
The subtype of the MIME type.
-
#type ⇒ String
The type of the MIME type.
Instance Method Summary collapse
-
#==(other_media_type) ⇒ Boolean
Compares the Media Type to another Media Type.
-
#initialize(media_type) ⇒ MediaType
constructor
Creates a new Media Type.
-
#interpretable_as ⇒ Array<String>
The MIME types that the Media Type can be interpreted as.
-
#mime_type ⇒ String
The MIME type of the Media Type.
-
#mime_type=(mime_type)
Sets the MIME type of the Media Type.
-
#suffix ⇒ String?
The suffix of the MIME type.
-
#to_s ⇒ String
Converts the Media Type to a string.
-
#vendor_specific? ⇒ Boolean
Whether or not the Media Type is vendor-specific.
Constructor Details
#initialize(media_type) ⇒ MediaType
Creates a new Media Type.
54 55 56 57 |
# File 'lib/webbed/media_type.rb', line 54 def initialize(media_type) self.mime_type, *parameters = media_type.split(PARAMETERS_REGEX) self.parameters = Hash[*parameters] || {} end |
Instance Attribute Details
#parameters ⇒ Hash{String => String}
The parameters of the Media Type.
According to RFC 2616, parameters are separated from the MIME type and each other using a semicolon.
39 40 41 |
# File 'lib/webbed/media_type.rb', line 39 def parameters @parameters end |
#subtype ⇒ String
The subtype of the MIME type.
According to RFC 2616, this is the after before the slash.
27 28 29 |
# File 'lib/webbed/media_type.rb', line 27 def subtype @subtype end |
#type ⇒ String
The type of the MIME type.
According to RFC 2616, this is the part before the slash.
16 17 18 |
# File 'lib/webbed/media_type.rb', line 16 def type @type end |
Instance Method Details
#==(other_media_type) ⇒ Boolean
Compares the Media Type to another Media Type.
Two Media Types are equal if their #mime_type
's are equal.
133 134 135 |
# File 'lib/webbed/media_type.rb', line 133 def ==(other_media_type) mime_type == other_media_type.mime_type end |
#interpretable_as ⇒ Array<String>
The MIME types that the Media Type can be interpreted as.
This uses the suffix to generate a list of MIME types that can be used to interpret the Media Type. It's useful if you need to be able to parse XML or JSON Media Types that may or may not have suffixes using general XML or JSON parsers.
152 153 154 155 156 157 158 |
# File 'lib/webbed/media_type.rb', line 152 def interpretable_as if suffix [mime_type, "#{type}/#{suffix}"] else [mime_type] end end |
#mime_type ⇒ String
The MIME type of the Media Type.
62 63 64 |
# File 'lib/webbed/media_type.rb', line 62 def mime_type "#{type}/#{subtype}" end |
#mime_type=(mime_type)
Sets the MIME type of the Media Type.
69 70 71 72 73 |
# File 'lib/webbed/media_type.rb', line 69 def mime_type=(mime_type) MIME_TYPE_REGEX =~ mime_type self.type = $1 self.subtype = $2 end |
#suffix ⇒ String?
The suffix of the MIME type.
Suffixes follow the convention set forth by the Atom specification:
separated from the rest of the MIME Type by a +
.
117 118 119 120 121 122 123 124 125 |
# File 'lib/webbed/media_type.rb', line 117 def suffix suffix = subtype.split('+')[-1] if suffix != subtype suffix else nil end end |
#to_s ⇒ String
Converts the Media Type to a string.
78 79 80 81 82 83 84 85 |
# File 'lib/webbed/media_type.rb', line 78 def to_s if parameters.empty? mime_type else parameters = self.parameters.map { |k, v| "#{k}=#{v}" }.join('; ') "#{mime_type}; #{parameters}" end end |
#vendor_specific? ⇒ Boolean
Whether or not the Media Type is vendor-specific.
The method uses the vnd.
prefix convention to determine whether or not
it was created for a specific vendor.
100 101 102 |
# File 'lib/webbed/media_type.rb', line 100 def vendor_specific? subtype[0..3] == 'vnd.' end |