Class: FunctionsFramework::CloudEvents::ContentType
- Inherits:
-
Object
- Object
- FunctionsFramework::CloudEvents::ContentType
- Defined in:
- lib/functions_framework/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.
Instance Attribute Summary collapse
-
#canonical_string ⇒ String
readonly
A "canonical" header content string with spacing and capitalization normalized.
-
#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_format ⇒ String?
readonly
The portion of the content subtype after any plus sign, or nil if there is no plus sign in the subtype.
-
#subtype_prefix ⇒ String
readonly
The portion of the content subtype before any plus sign.
Instance Method Summary collapse
-
#charset ⇒ String?
The first value of the "charset" parameter, or nil if there is no charset.
-
#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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 32 def initialize string @string = string # TODO: This handles simple cases but is not RFC-822 compliant. sections = string.to_s.split ";" media_type, subtype = sections.shift.split "/" subtype_prefix, subtype_format = subtype.split "+" @media_type = media_type.strip.downcase @subtype = subtype.strip.downcase @subtype_prefix = subtype_prefix.strip.downcase @subtype_format = subtype_format&.strip&.downcase @params = initialize_params sections @canonical_string = "#{@media_type}/#{@subtype}" + @params.map { |k, v| "; #{k}=#{v}" }.join end |
Instance Attribute Details
#canonical_string ⇒ String (readonly)
A "canonical" header content string with spacing and capitalization normalized.
59 60 61 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 59 def canonical_string @canonical_string end |
#media_type ⇒ String (readonly)
The media type.
65 66 67 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 65 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.
92 93 94 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 92 def params @params end |
#string ⇒ String (readonly) Also known as: to_s
The original header content string
51 52 53 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 51 def string @string end |
#subtype ⇒ String (readonly)
The entire content subtype (which could include an extension delimited by a plus sign)
72 73 74 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 72 def subtype @subtype 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.
85 86 87 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 85 def subtype_format @subtype_format end |
#subtype_prefix ⇒ String (readonly)
The portion of the content subtype before any plus sign.
78 79 80 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 78 def subtype_prefix @subtype_prefix end |
Instance Method Details
#charset ⇒ String?
The first value of the "charset" parameter, or nil if there is no charset.
109 110 111 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 109 def charset param_values("charset").first end |
#param_values(key) ⇒ Array<String>
An array of values for the given parameter name
99 100 101 102 |
# File 'lib/functions_framework/cloud_events/content_type.rb', line 99 def param_values key key = key.downcase @params.inject([]) { |a, (k, v)| key == k ? a << v : a } end |