Class: CloudEvents::Event::V0
- Inherits:
-
Object
- Object
- CloudEvents::Event::V0
- Includes:
- CloudEvents::Event
- Defined in:
- lib/cloud_events/event/v0.rb
Overview
A CloudEvents V0 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 V0.3, 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/v0.3/spec.md for descriptions of the standard attributes.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The event-specific data, or
nilif there is no data. -
#data_content_encoding ⇒ String?
(also: #datacontentencoding)
readonly
The optional
datacontentencodingfield as aStringobject, ornilif the field is absent. -
#data_content_type ⇒ CloudEvents::ContentType?
(also: #datacontenttype)
readonly
The optional
datacontenttypefield as a ContentType object, ornilif the field is absent. -
#id ⇒ String
readonly
The
idfield. -
#schema_url ⇒ URI?
(also: #schemaurl)
readonly
The optional
schemaurlfield as aURIobject, ornilif the field is absent. -
#source ⇒ URI
readonly
The
sourcefield as aURIobject. -
#spec_version ⇒ String
(also: #specversion)
readonly
The
specversionfield. -
#subject ⇒ String?
readonly
The optional
subjectfield, ornilif the field is absent. -
#time ⇒ DateTime?
readonly
The optional
timefield as aDateTimeobject, ornilif the field is absent. -
#type ⇒ String
readonly
The
typefield.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Return the value of the given named attribute.
-
#initialize(attributes: nil, **args) ⇒ V0
constructor
Create a new cloud event object with the given data and attributes.
-
#to_h ⇒ Hash
Return a hash representation of this event.
-
#with(**changes) ⇒ FunctionFramework::CloudEvents::Event
Create and return a copy of this event with the given changes.
Methods included from CloudEvents::Event
Constructor Details
#initialize(attributes: nil, **args) ⇒ V0
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. thespecversionfield.) - :id [
String] - required - The eventidfield. - :source [
String,URI] - required - The eventsourcefield. - :type [
String] - required - The eventtypefield. - :data [
Object] - optional - The data associated with the event (i.e. thedatafield). - :data_content_encoding (or :datacontentencoding)
[
String] - optional - The content-encoding for the data (i.e. thedatacontentencodingfield.) - :data_content_type (or :datacontenttype) [
String, ContentType] - optional - The content-type for the data, if the data is a string (i.e. the eventdatacontenttypefield.) - :schema_url (or :schemaurl) [
String,URI] - optional - The eventschemaurlfield. - :subject [
String] - optional - The eventsubjectfield. - :time [
String,DateTime,Time] - optional - The eventtimefield.
Any additional attributes are assumed to be extension attributes. They are not available as separate methods, but can be accessed via the CloudEvents::Event::V1#[] 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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cloud_events/event/v0.rb', line 74 def initialize attributes: nil, **args interpreter = FieldInterpreter.new attributes || args @spec_version = interpreter.spec_version ["specversion", "spec_version"], accept: /^0\.3$/ @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_encoding = interpreter.string ["datacontentencoding", "data_content_encoding"] @data_content_type = interpreter.content_type ["datacontenttype", "data_content_type"] @schema_url = interpreter.uri ["schemaurl", "schema_url"] @subject = interpreter.string ["subject"] @time = interpreter.rfc3339_date_time ["time"] @attributes = interpreter.finish_attributes freeze end |
Instance Attribute Details
#data ⇒ Object (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
Stringusing theASCII-8BITencoding. - A string in some other encoding such as
UTF-8orUS-ASCII. - Any JSON data type, such as a Boolean, Integer, Array, Hash, or
nil.
178 179 180 |
# File 'lib/cloud_events/event/v0.rb', line 178 def data @data end |
#data_content_encoding ⇒ String? (readonly) Also known as: datacontentencoding
The optional datacontentencoding field as a String object, or
nil if the field is absent.
186 187 188 |
# File 'lib/cloud_events/event/v0.rb', line 186 def data_content_encoding @data_content_encoding end |
#data_content_type ⇒ CloudEvents::ContentType? (readonly) Also known as: datacontenttype
The optional datacontenttype field as a ContentType
object, or nil if the field is absent.
195 196 197 |
# File 'lib/cloud_events/event/v0.rb', line 195 def data_content_type @data_content_type end |
#id ⇒ String (readonly)
The id field. Required.
142 143 144 |
# File 'lib/cloud_events/event/v0.rb', line 142 def id @id end |
#schema_url ⇒ URI? (readonly) Also known as: schemaurl
The optional schemaurl field as a URI object, or nil if the
field is absent.
204 205 206 |
# File 'lib/cloud_events/event/v0.rb', line 204 def schema_url @schema_url end |
#source ⇒ URI (readonly)
The source field as a URI object. Required.
149 150 151 |
# File 'lib/cloud_events/event/v0.rb', line 149 def source @source end |
#spec_version ⇒ String (readonly) Also known as: specversion
The specversion field. Required.
163 164 165 |
# File 'lib/cloud_events/event/v0.rb', line 163 def spec_version @spec_version end |
#subject ⇒ String? (readonly)
The optional subject field, or nil if the field is absent.
212 213 214 |
# File 'lib/cloud_events/event/v0.rb', line 212 def subject @subject end |
#time ⇒ DateTime? (readonly)
The optional time field as a DateTime object, or nil if the
field is absent.
220 221 222 |
# File 'lib/cloud_events/event/v0.rb', line 220 def time @time end |
#type ⇒ String (readonly)
The type field. Required.
156 157 158 |
# File 'lib/cloud_events/event/v0.rb', line 156 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.
123 124 125 |
# File 'lib/cloud_events/event/v0.rb', line 123 def [] key @attributes[key.to_s] end |
#to_h ⇒ Hash
Return a hash representation of this event. The returned hash is an unfrozen deep copy. Modifications do not affect the original event.
133 134 135 |
# File 'lib/cloud_events/event/v0.rb', line 133 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.
99 100 101 102 |
# File 'lib/cloud_events/event/v0.rb', line 99 def with **changes attributes = @attributes.merge changes V0.new attributes: attributes end |