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, default_charset: nil) ⇒ ContentType

Parse the given header value.

Parameters:

  • string (String)

    Content-Type header value in RFC 2045 format

  • default_charset (String) (defaults to: nil)

    Optional. The charset to use if none is specified. Defaults to us-ascii.



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_stringString (readonly)

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

Returns:

  • (String)


53
54
55
# File 'lib/cloud_events/content_type.rb', line 53

def canonical_string
  @canonical_string
end

#charsetString (readonly)

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

Returns:

  • (String)


92
93
94
# File 'lib/cloud_events/content_type.rb', line 92

def charset
  @charset
end

#error_messageString? (readonly)

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

Returns:

  • (String, nil)


98
99
100
# File 'lib/cloud_events/content_type.rb', line 98

def error_message
  @error_message
end

#media_typeString (readonly)

The media type.

Returns:

  • (String)


59
60
61
# File 'lib/cloud_events/content_type.rb', line 59

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)>)


86
87
88
# File 'lib/cloud_events/content_type.rb', line 86

def params
  @params
end

#stringString (readonly) Also known as: to_s

The original header content string.

Returns:

  • (String)


45
46
47
# File 'lib/cloud_events/content_type.rb', line 45

def string
  @string
end

#subtypeString (readonly)

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

Returns:

  • (String)


66
67
68
# File 'lib/cloud_events/content_type.rb', line 66

def subtype
  @subtype
end

#subtype_baseString (readonly)

The portion of the content subtype before any plus sign.

Returns:

  • (String)


72
73
74
# File 'lib/cloud_events/content_type.rb', line 72

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)


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

Parameters:

  • key (String)

Returns:

  • (Array<String>)


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