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.
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) ⇒ 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) ⇒ ContentType
Parse the given header value.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cloud_events/content_type.rb', line 23 def initialize string @string = string @media_type = "text" @subtype_base = @subtype = "plain" @subtype_format = nil @params = [] @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 end |
Instance Attribute Details
#canonical_string ⇒ String (readonly)
A "canonical" header content string with spacing and capitalization normalized.
48 49 50 |
# File 'lib/cloud_events/content_type.rb', line 48 def canonical_string @canonical_string end |
#charset ⇒ String (readonly)
The charset, defaulting to "us-ascii" if none is explicitly set.
87 88 89 |
# File 'lib/cloud_events/content_type.rb', line 87 def charset @charset end |
#error_message ⇒ String? (readonly)
The error message when parsing, or nil
if there was no error message.
93 94 95 |
# File 'lib/cloud_events/content_type.rb', line 93 def @error_message end |
#media_type ⇒ String (readonly)
The media type.
54 55 56 |
# File 'lib/cloud_events/content_type.rb', line 54 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.
81 82 83 |
# File 'lib/cloud_events/content_type.rb', line 81 def params @params end |
#string ⇒ String (readonly) Also known as: to_s
The original header content string.
40 41 42 |
# File 'lib/cloud_events/content_type.rb', line 40 def string @string end |
#subtype ⇒ String (readonly)
The entire content subtype (which could include an extension delimited by a plus sign).
61 62 63 |
# File 'lib/cloud_events/content_type.rb', line 61 def subtype @subtype end |
#subtype_base ⇒ String (readonly)
The portion of the content subtype before any plus sign.
67 68 69 |
# File 'lib/cloud_events/content_type.rb', line 67 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.
74 75 76 |
# File 'lib/cloud_events/content_type.rb', line 74 def subtype_format @subtype_format end |
Instance Method Details
#param_values(key) ⇒ Array<String>
An array of values for the given parameter name
100 101 102 103 |
# File 'lib/cloud_events/content_type.rb', line 100 def param_values key key = key.downcase @params.inject([]) { |a, (k, v)| key == k ? a << v : a } end |