Class: Webbed::MediaType
- Inherits:
-
Object
- Object
- Webbed::MediaType
- Defined in:
- lib/webbed/media_type.rb
Constant Summary
- MIME_TYPE_REGEX =
Regular expression for parsing the type and subtype of a MIME type
/^([-\w.+]+)\/([-\w.+]*)$/- PARAMETERS_REGEX =
Regular expression for parsing the parameters of a Media Type
/=|\s*;\s*/
Instance Attribute Summary (collapse)
-
- (Hash<String => String>) parameters
The parameters of the Media Type.
-
- (String) subtype
The subtype of the MIME type.
-
- (String) type
The type of the MIME type.
Instance Method Summary (collapse)
-
- (Boolean) ==(other_media_type)
Compares the Media Type to another Media Type.
-
- (MediaType) initialize(media_type)
constructor
Creates a new Media Type.
-
- (Array<String>) interpretable_as
The MIME types that the Media Type can be interpreted as.
-
- (String) mime_type
The MIME type of the Media Type.
-
- mime_type=(new_mime_type)
Sets the MIME type of the Media Type.
-
- (String?) suffix
The suffix of the MIME type.
-
- (String) to_s
Converts the Media Type to a string.
-
- (Boolean) vendor_specific?
Whether or not the Media Type is vendor-specific.
Constructor Details
- (MediaType) initialize(media_type)
Creates a new Media Type
56 57 58 59 |
# File 'lib/webbed/media_type.rb', line 56 def initialize(media_type) self.mime_type, *parameters = media_type.split(PARAMETERS_REGEX) self.parameters = Hash[*parameters] || {} end |
Instance Attribute Details
- (Hash<String => String>) parameters
The parameters of the Media Type
According to RFC 2616, parameters are separated from the MIME type and each other using a semicolon.
35 36 37 |
# File 'lib/webbed/media_type.rb', line 35 def parameters @parameters end |
- (String) subtype
The subtype of the MIME type
According to RFC 2616, this is the after before the slash.
23 24 25 |
# File 'lib/webbed/media_type.rb', line 23 def subtype @subtype end |
- (String) type
The type of the MIME type
According to RFC 2616, this is the part before the slash.
12 13 14 |
# File 'lib/webbed/media_type.rb', line 12 def type @type end |
Instance Method Details
- (Boolean) ==(other_media_type)
Compares the Media Type to another Media Type
Two Media Types are equal if their #mime_type's are equal.
135 136 137 |
# File 'lib/webbed/media_type.rb', line 135 def ==(other_media_type) mime_type == other_media_type.mime_type end |
- (Array<String>) interpretable_as
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.
154 155 156 157 158 159 160 |
# File 'lib/webbed/media_type.rb', line 154 def interpretable_as if suffix [mime_type, "#{type}/#{suffix}"] else [mime_type] end end |
- (String) mime_type
The MIME type of the Media Type
64 65 66 |
# File 'lib/webbed/media_type.rb', line 64 def mime_type "#{type}/#{subtype}" end |
- mime_type=(new_mime_type)
Sets the MIME type of the Media Type
71 72 73 74 75 |
# File 'lib/webbed/media_type.rb', line 71 def mime_type=(new_mime_type) MIME_TYPE_REGEX =~ new_mime_type self.type = $1 self.subtype = $2 end |
- (String?) suffix
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 +.
119 120 121 122 123 124 125 126 127 |
# File 'lib/webbed/media_type.rb', line 119 def suffix suffix = subtype.split('+')[-1] if suffix != subtype suffix else nil end end |
- (String) to_s
Converts the Media Type to a string
80 81 82 83 84 85 86 87 |
# File 'lib/webbed/media_type.rb', line 80 def to_s if parameters.empty? mime_type else parameters = self.parameters.map { |k, v| "#{k}=#{v}" }.join('; ') "#{mime_type}; #{parameters}" end end |
- (Boolean) vendor_specific?
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.
102 103 104 |
# File 'lib/webbed/media_type.rb', line 102 def vendor_specific? subtype[0..3] == 'vnd.' end |