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.

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



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

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

Returns:

  • (String)


48
49
50
# File 'lib/cloud_events/content_type.rb', line 48

def canonical_string
  @canonical_string
end

#charsetString (readonly)

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

Returns:

  • (String)


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

def charset
  @charset
end

#error_messageString? (readonly)

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

Returns:

  • (String, nil)


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

def error_message
  @error_message
end

#media_typeString (readonly)

The media type.

Returns:

  • (String)


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

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


81
82
83
# File 'lib/cloud_events/content_type.rb', line 81

def params
  @params
end

#stringString (readonly) Also known as: to_s

The original header content string.

Returns:

  • (String)


40
41
42
# File 'lib/cloud_events/content_type.rb', line 40

def string
  @string
end

#subtypeString (readonly)

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

Returns:

  • (String)


61
62
63
# File 'lib/cloud_events/content_type.rb', line 61

def subtype
  @subtype
end

#subtype_baseString (readonly)

The portion of the content subtype before any plus sign.

Returns:

  • (String)


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

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)


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

Parameters:

  • key (String)

Returns:

  • (Array<String>)


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