Class: CloudEvents::ContentType
- Inherits:
-
Object
- Object
- CloudEvents::ContentType
- Defined in:
- lib/cloud_events/content_type.rb
Overview
A parsed content-type header.
This object represents the information contained in a Content-Type, obtained by parsing the header according to RFC 2045.
Case-insensitive fields, such as media_type and subtype, are normalized to lower case.
If parsing fails, this class will try to get as much information as it can, and fill the rest with defaults as recommended in RFC 2045 sec 5.2. In case of a parsing error, the #error_message field will be set.
This object is immutable, and Ractor-shareable on Ruby 3.
Instance Attribute Summary collapse
-
#canonical_string ⇒ String
readonly
A "canonical" header content string with spacing and capitalization normalized.
-
#charset ⇒ String
readonly
The charset, defaulting to "us-ascii" if none is explicitly set.
-
#error_message ⇒ String?
readonly
The error message when parsing, or
nil
if there was no error message. -
#media_type ⇒ String
readonly
The media type.
-
#params ⇒ Array<Array(String,String)>
readonly
An array of parameters, each element as a two-element array of the parameter name and value.
-
#string ⇒ String
(also: #to_s)
readonly
The original header content string.
-
#subtype ⇒ String
readonly
The entire content subtype (which could include an extension delimited by a plus sign).
-
#subtype_base ⇒ String
readonly
The portion of the content subtype before any plus sign.
-
#subtype_format ⇒ String?
readonly
The portion of the content subtype after any plus sign, or nil if there is no plus sign in the subtype.
Instance Method Summary collapse
-
#initialize(string, default_charset: nil) ⇒ ContentType
constructor
Parse the given header value.
-
#param_values(key) ⇒ Array<String>
An array of values for the given parameter name.
Constructor Details
#initialize(string, default_charset: nil) ⇒ ContentType
Parse the given header value.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cloud_events/content_type.rb', line 27 def initialize string, default_charset: nil @string = string.to_s @media_type = "text" @subtype_base = @subtype = "plain" @subtype_format = nil @params = [] @charset = default_charset || "us-ascii" @error_message = nil parse consume_comments string.strip @canonical_string = "#{@media_type}/#{@subtype}" + @params.map { |k, v| "; #{k}=#{maybe_quote v}" }.join full_freeze end |
Instance Attribute Details
#canonical_string ⇒ String (readonly)
A "canonical" header content string with spacing and capitalization normalized.
53 54 55 |
# File 'lib/cloud_events/content_type.rb', line 53 def canonical_string @canonical_string end |
#charset ⇒ String (readonly)
The charset, defaulting to "us-ascii" if none is explicitly set.
92 93 94 |
# File 'lib/cloud_events/content_type.rb', line 92 def charset @charset end |
#error_message ⇒ String? (readonly)
The error message when parsing, or nil
if there was no error message.
98 99 100 |
# File 'lib/cloud_events/content_type.rb', line 98 def @error_message end |
#media_type ⇒ String (readonly)
The media type.
59 60 61 |
# File 'lib/cloud_events/content_type.rb', line 59 def media_type @media_type end |
#params ⇒ Array<Array(String,String)> (readonly)
An array of parameters, each element as a two-element array of the parameter name and value.
86 87 88 |
# File 'lib/cloud_events/content_type.rb', line 86 def params @params end |
#string ⇒ String (readonly) Also known as: to_s
The original header content string.
45 46 47 |
# File 'lib/cloud_events/content_type.rb', line 45 def string @string end |
#subtype ⇒ String (readonly)
The entire content subtype (which could include an extension delimited by a plus sign).
66 67 68 |
# File 'lib/cloud_events/content_type.rb', line 66 def subtype @subtype end |
#subtype_base ⇒ String (readonly)
The portion of the content subtype before any plus sign.
72 73 74 |
# File 'lib/cloud_events/content_type.rb', line 72 def subtype_base @subtype_base end |
#subtype_format ⇒ String? (readonly)
The portion of the content subtype after any plus sign, or nil if there is no plus sign in the subtype.
79 80 81 |
# File 'lib/cloud_events/content_type.rb', line 79 def subtype_format @subtype_format end |
Instance Method Details
#param_values(key) ⇒ Array<String>
An array of values for the given parameter name
105 106 107 108 |
# File 'lib/cloud_events/content_type.rb', line 105 def param_values key key = key.downcase @params.inject([]) { |a, (k, v)| key == k ? a << v : a } end |