Class: CloudEvents::Event::V1

Inherits:
Object
  • Object
show all
Includes:
CloudEvents::Event
Defined in:
lib/cloud_events/event/v1.rb

Overview

A CloudEvents V1 data type.

This object represents a complete CloudEvent, including the event data and context attributes. It supports the standard required and optional attributes defined in CloudEvents V1.0, and arbitrary extension attributes. All attribute values can be obtained (in their string form) via the #[] method. Additionally, standard attributes have their own accessor methods that may return typed objects (such as DateTime for the time attribute).

This object is immutable, and Ractor-shareable on Ruby 3. The data and attribute values can be retrieved but not modified. To obtain an event with modifications, use the #with method to create a copy with the desired changes.

See https://github.com/cloudevents/spec/blob/v1.0/spec.md for descriptions of the standard attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CloudEvents::Event

create

Constructor Details

#initialize(attributes: nil, **args) ⇒ V1

Create a new cloud event object with the given data and attributes.

Event attributes may be presented as keyword arguments, or as a Hash passed in via the attributes argument (but not both).

The following standard attributes are supported and exposed as attribute methods on the object.

  • :spec_version (or :specversion) [String] - required - The CloudEvents spec version (i.e. the specversion field.)
  • :id [String] - required - The event id field.
  • :source [String, URI] - required - The event source field.
  • :type [String] - required - The event type field.
  • :data [Object] - optional - The data associated with the event (i.e. the data field).
  • :data_content_type (or :datacontenttype) [String, ContentType] - optional - The content-type for the data, if the data is a string (i.e. the event datacontenttype field.)
  • :data_schema (or :dataschema) [String, URI] - optional - The event dataschema field.
  • :subject [String] - optional - The event subject field.
  • :time [String, DateTime, Time] - optional - The event time field.

Any additional attributes are assumed to be extension attributes. They are not available as separate methods, but can be accessed via the #[] operator.

Note that attribute objects passed in may get deep-frozen if they are used in the final event object. This is particularly important for the :data field, for example if you pass a structured hash. If this is an issue, make a deep copy of objects before passing to this constructor.

Parameters:

  • attributes (Hash) (defaults to: nil)

    The data and attributes, as a hash.

  • args (keywords)

    The data and attributes, as keyword arguments.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cloud_events/event/v1.rb', line 71

def initialize attributes: nil, **args
  interpreter = FieldInterpreter.new attributes || args
  @spec_version = interpreter.spec_version ["specversion", "spec_version"], accept: /^1(\.|$)/
  @id = interpreter.string ["id"], required: true
  @source = interpreter.uri ["source"], required: true
  @type = interpreter.string ["type"], required: true
  @data = interpreter.data_object ["data"]
  @data_content_type = interpreter.content_type ["datacontenttype", "data_content_type"]
  @data_schema = interpreter.uri ["dataschema", "data_schema"]
  @subject = interpreter.string ["subject"]
  @time = interpreter.rfc3339_date_time ["time"]
  @attributes = interpreter.finish_attributes
  freeze
end

Instance Attribute Details

#dataObject (readonly)

The event-specific data, or nil if there is no data.

Data may be one of the following types:

  • Binary data, represented by a String using the ASCII-8BIT encoding.
  • A string in some other encoding such as UTF-8 or US-ASCII.
  • Any JSON data type, such as a Boolean, Integer, Array, Hash, or nil.

Returns:

  • (Object)


174
175
176
# File 'lib/cloud_events/event/v1.rb', line 174

def data
  @data
end

#data_content_typeCloudEvents::ContentType? (readonly) Also known as: datacontenttype

The optional datacontenttype field as a ContentType object, or nil if the field is absent.

Returns:



182
183
184
# File 'lib/cloud_events/event/v1.rb', line 182

def data_content_type
  @data_content_type
end

#data_schemaURI? (readonly) Also known as: dataschema

The optional dataschema field as a URI object, or nil if the field is absent.

Returns:

  • (URI, nil)


191
192
193
# File 'lib/cloud_events/event/v1.rb', line 191

def data_schema
  @data_schema
end

#idString (readonly)

The id field. Required.

Returns:

  • (String)


138
139
140
# File 'lib/cloud_events/event/v1.rb', line 138

def id
  @id
end

#sourceURI (readonly)

The source field as a URI object. Required.

Returns:

  • (URI)


145
146
147
# File 'lib/cloud_events/event/v1.rb', line 145

def source
  @source
end

#spec_versionString (readonly) Also known as: specversion

The specversion field. Required.

Returns:

  • (String)


159
160
161
# File 'lib/cloud_events/event/v1.rb', line 159

def spec_version
  @spec_version
end

#subjectString? (readonly)

The optional subject field, or nil if the field is absent.

Returns:

  • (String, nil)


199
200
201
# File 'lib/cloud_events/event/v1.rb', line 199

def subject
  @subject
end

#timeDateTime? (readonly)

The optional time field as a DateTime object, or nil if the field is absent.

Returns:

  • (DateTime, nil)


207
208
209
# File 'lib/cloud_events/event/v1.rb', line 207

def time
  @time
end

#typeString (readonly)

The type field. Required.

Returns:

  • (String)


152
153
154
# File 'lib/cloud_events/event/v1.rb', line 152

def type
  @type
end

Instance Method Details

#[](key) ⇒ String?

Return the value of the given named attribute. Both standard and extension attributes are supported.

Attribute names must be given as defined in the standard CloudEvents specification. For example specversion rather than spec_version.

Results are given in their "raw" form, generally a string. This may be different from the Ruby object returned from corresponding attribute methods. For example:

event["time"]     # => String rfc3339 representation
event.time        # => DateTime object

Results are also always frozen and cannot be modified in place.

Parameters:

  • key (String, Symbol)

    The attribute name.

Returns:

  • (String, nil)


119
120
121
# File 'lib/cloud_events/event/v1.rb', line 119

def [] key
  @attributes[key.to_s]
end

#to_hHash

Return a hash representation of this event. The returned hash is an unfrozen deep copy. Modifications do not affect the original event.

Returns:

  • (Hash)


129
130
131
# File 'lib/cloud_events/event/v1.rb', line 129

def to_h
  Utils.deep_dup @attributes
end

#with(**changes) ⇒ FunctionFramework::CloudEvents::Event

Create and return a copy of this event with the given changes. See the constructor for the parameters that can be passed. In general, you can pass a new value for any attribute, or pass nil to remove an optional attribute.

Parameters:

  • changes (keywords)

    See #initialize for a list of arguments.

Returns:

  • (FunctionFramework::CloudEvents::Event)


95
96
97
98
# File 'lib/cloud_events/event/v1.rb', line 95

def with **changes
  attributes = @attributes.merge changes
  V1.new attributes: attributes
end