Class: FunctionsFramework::CloudEvents::ContentType

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

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



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

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

Returns:

  • (String)


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

def canonical_string
  @canonical_string
end

#media_typeString (readonly)

The media type.

Returns:

  • (String)


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

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


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

def params
  @params
end

#stringString (readonly) Also known as: to_s

The original header content string

Returns:

  • (String)


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

def string
  @string
end

#subtypeString (readonly)

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

Returns:

  • (String)


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

def subtype
  @subtype
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)


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

def subtype_format
  @subtype_format
end

#subtype_prefixString (readonly)

The portion of the content subtype before any plus sign.

Returns:

  • (String)


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

def subtype_prefix
  @subtype_prefix
end

Instance Method Details

#charsetString?

The first value of the "charset" parameter, or nil if there is no charset.

Returns:

  • (String, nil)


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

Parameters:

  • key (String)

Returns:

  • (Array<String>)


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