Class: CloudEvents::ContentType

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ ContentType

Parse the given header value.

Parameters:

  • string (String)

    Content-Type header value in RFC 2045 format



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloud_events/content_type.rb', line 25

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
  full_freeze
end

Instance Attribute Details

#canonical_stringString (readonly)

A "canonical" header content string with spacing and capitalization normalized.

Returns:

  • (String)


51
52
53
# File 'lib/cloud_events/content_type.rb', line 51

def canonical_string
  @canonical_string
end

#charsetString (readonly)

The charset, defaulting to "us-ascii" if none is explicitly set.

Returns:

  • (String)


90
91
92
# File 'lib/cloud_events/content_type.rb', line 90

def charset
  @charset
end

#error_messageString? (readonly)

The error message when parsing, or nil if there was no error message.

Returns:

  • (String, nil)


96
97
98
# File 'lib/cloud_events/content_type.rb', line 96

def error_message
  @error_message
end

#media_typeString (readonly)

The media type.

Returns:

  • (String)


57
58
59
# File 'lib/cloud_events/content_type.rb', line 57

def media_type
  @media_type
end

#paramsArray<Array(String,String)> (readonly)

An array of parameters, each element as a two-element array of the parameter name and value.

Returns:

  • (Array<Array(String,String)>)


84
85
86
# File 'lib/cloud_events/content_type.rb', line 84

def params
  @params
end

#stringString (readonly) Also known as: to_s

The original header content string.

Returns:

  • (String)


43
44
45
# File 'lib/cloud_events/content_type.rb', line 43

def string
  @string
end

#subtypeString (readonly)

The entire content subtype (which could include an extension delimited by a plus sign).

Returns:

  • (String)


64
65
66
# File 'lib/cloud_events/content_type.rb', line 64

def subtype
  @subtype
end

#subtype_baseString (readonly)

The portion of the content subtype before any plus sign.

Returns:

  • (String)


70
71
72
# File 'lib/cloud_events/content_type.rb', line 70

def subtype_base
  @subtype_base
end

#subtype_formatString? (readonly)

The portion of the content subtype after any plus sign, or nil if there is no plus sign in the subtype.

Returns:

  • (String, nil)


77
78
79
# File 'lib/cloud_events/content_type.rb', line 77

def subtype_format
  @subtype_format
end

Instance Method Details

#param_values(key) ⇒ Array<String>

An array of values for the given parameter name

Parameters:

  • key (String)

Returns:

  • (Array<String>)


103
104
105
106
# File 'lib/cloud_events/content_type.rb', line 103

def param_values key
  key = key.downcase
  @params.inject([]) { |a, (k, v)| key == k ? a << v : a }
end