Class: CloudEvents::TextFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_events/text_format.rb

Overview

An data encoder/decoder for text content types. This handles any media type of the form text/* or application/octet-stream, and passes strings through as-is.

Instance Method Summary collapse

Instance Method Details

#decode_data(content: nil, content_type: nil, **_other_kwargs) ⇒ Hash?

Trivially decode an event data string using text format. See Format#decode_data for a general description.

Expects :content and :content_type arguments, and will decline the request unless all three are provided.

If decoding succeeded, returns a hash with the following keys:

  • :data (Object) The payload object to set as the data attribute.
  • :content_type (ContentType) The content type to be set as the datacontenttype attribute.

Parameters:

  • content (String) (defaults to: nil)

    Serialized content to decode.

  • content_type (CloudEvents::ContentType) (defaults to: nil)

    The input content type.

Returns:

  • (Hash)

    if accepting the request.

  • (nil)

    if declining the request.



34
35
36
37
38
# File 'lib/cloud_events/text_format.rb', line 34

def decode_data content: nil, content_type: nil, **_other_kwargs
  return nil unless content
  return nil unless text_content_type? content_type
  { data: content.to_s, content_type: content_type }
end

#encode_data(data: UNSPECIFIED, content_type: nil, **_other_kwargs) ⇒ Hash?

Trivially an event data object using text format. See Format#encode_data for a general description.

Expects :data and :content_type arguments, and will decline the request unless all three are provided. The :data object will be converted to a string if it is not already a string.

If decoding succeeded, returns a hash with the following keys:

  • :content (String) The serialized form of the data.
  • :content_type (ContentType) The content type for the output.

Parameters:

  • data (Object) (defaults to: UNSPECIFIED)

    A data object to encode.

  • content_type (CloudEvents::ContentType) (defaults to: nil)

    The input content type

Returns:

  • (Hash)

    if accepting the request.

  • (nil)

    if declining the request.



60
61
62
63
64
# File 'lib/cloud_events/text_format.rb', line 60

def encode_data data: UNSPECIFIED, content_type: nil, **_other_kwargs
  return nil if data == UNSPECIFIED
  return nil unless text_content_type? content_type
  { content: data.to_s, content_type: content_type }
end