Class: FunctionsFramework::CloudEvents::Event::V1
- Inherits:
-
Object
- Object
- FunctionsFramework::CloudEvents::Event::V1
- Includes:
- FunctionsFramework::CloudEvents::Event
- Defined in:
- lib/functions_framework/cloud_events/event/v1.rb
Overview
A CloudEvents V1 data type.
This object a complete CloudEvent, including the event data and its
context attributes. It supports the standard required and optional
attributes defined in CloudEvents V1, 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. 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/master/spec.md for descriptions of the standard attributes.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The event-specific data, or
nil
if there is no data. -
#data_content_type ⇒ FunctionsFramework::CloudEvents::ContentType?
(also: #datacontenttype)
readonly
The optional
datacontenttype
field as a ContentType object, ornil
if the field is absent. -
#data_content_type_string ⇒ String?
(also: #datacontenttype_string)
readonly
The string representation of the optional
datacontenttype
field, ornil
if the field is absent. -
#data_schema ⇒ URI?
(also: #dataschema)
readonly
The optional
dataschema
field as aURI
object, ornil
if the field is absent. -
#data_schema_string ⇒ String?
(also: #dataschema_string)
readonly
The string representation of the optional
dataschema
field, ornil
if the field is absent. -
#id ⇒ String
readonly
The
id
field. -
#source ⇒ URI
readonly
The
source
field as aURI
object. -
#source_string ⇒ String
readonly
The string representation of the
source
field. -
#spec_version ⇒ String
(also: #specversion)
readonly
The
specversion
field. -
#subject ⇒ String?
readonly
The optional
subject
field, ornil
if the field is absent. -
#time ⇒ DateTime?
readonly
The optional
time
field as aDateTime
object, ornil
if the field is absent. -
#time_string ⇒ String?
readonly
The rfc3339 string representation of the optional
time
field, ornil
if the field is absent. -
#type ⇒ String
readonly
The
type
field.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Return the value of the given named attribute.
-
#initialize(attributes: nil, **args) ⇒ V1
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 FunctionsFramework::CloudEvents::Event
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. thespecversion
field.) - :id [
String
] - required - The eventid
field. - :source [
String
,URI
] - required - The eventsource
field. - :type [
String
] - required - The eventtype
field. - :data [
Object
] - optional - The data associated with the event (i.e. thedata
field.) - :data_content_type (or :datacontenttype) [
String
, ContentType] - optional - The content-type for the data, if the data is a string (i.e. the eventdatacontenttype
field.) - :data_schema (or :dataschema) [
String
,URI
] - optional - The eventdataschema
field. - :subject [
String
] - optional - The eventsubject
field. - :time [
String
,DateTime
,Time
] - optional - The eventtime
field.
Any additional attributes are assumed to be extension attributes. They are not available as separate methods, but can be accessed via the #[] operator.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 75 def initialize attributes: nil, **args # rubocop:disable Metrics/AbcSize args = keys_to_strings(attributes || args) @attributes = {} @spec_version, _unused = interpret_string args, ["specversion", "spec_version"], required: true raise SpecVersionError, "Unrecognized specversion: #{@spec_version}" unless /^1(\.|$)/ =~ @spec_version @id, _unused = interpret_string args, ["id"], required: true @source, @source_string = interpret_uri args, ["source"], required: true @type, _unused = interpret_string args, ["type"], required: true @data, _unused = interpret_value args, ["data"], allow_nil: true @data_content_type, @data_content_type_string = interpret_content_type args, ["datacontenttype", "data_content_type"] @data_schema, @data_schema_string = interpret_uri args, ["dataschema", "data_schema"] @subject, _unused = interpret_string args, ["subject"] @time, @time_string = interpret_date_time args, ["time"] @attributes.merge! args 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
String
using theASCII-8BIT
encoding. - A string in some other encoding such as
UTF-8
orUS-ASCII
. - Any JSON data type, such as String, boolean, Integer, Array, or Hash
185 186 187 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 185 def data @data end |
#data_content_type ⇒ FunctionsFramework::CloudEvents::ContentType? (readonly) Also known as: datacontenttype
The optional datacontenttype
field as a
ContentType object, or nil
if
the field is absent.
194 195 196 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 194 def data_content_type @data_content_type end |
#data_content_type_string ⇒ String? (readonly) Also known as: datacontenttype_string
The string representation of the optional datacontenttype
field, or
nil
if the field is absent.
203 204 205 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 203 def data_content_type_string @data_content_type_string end |
#data_schema ⇒ URI? (readonly) Also known as: dataschema
The optional dataschema
field as a URI
object, or nil
if the
field is absent.
212 213 214 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 212 def data_schema @data_schema end |
#data_schema_string ⇒ String? (readonly) Also known as: dataschema_string
The string representation of the optional dataschema
field, or
nil
if the field is absent.
221 222 223 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 221 def data_schema_string @data_schema_string end |
#id ⇒ String (readonly)
The id
field. Required.
142 143 144 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 142 def id @id end |
#source ⇒ URI (readonly)
The source
field as a URI
object. Required.
149 150 151 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 149 def source @source end |
#source_string ⇒ String (readonly)
The string representation of the source
field. Required.
156 157 158 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 156 def source_string @source_string end |
#spec_version ⇒ String (readonly) Also known as: specversion
The specversion
field. Required.
170 171 172 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 170 def spec_version @spec_version end |
#subject ⇒ String? (readonly)
The optional subject
field, or nil
if the field is absent.
229 230 231 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 229 def subject @subject end |
#time ⇒ DateTime? (readonly)
The optional time
field as a DateTime
object, or nil
if the
field is absent.
237 238 239 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 237 def time @time end |
#time_string ⇒ String? (readonly)
The rfc3339 string representation of the optional time
field, or
nil
if the field is absent.
245 246 247 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 245 def time_string @time_string end |
#type ⇒ String (readonly)
The type
field. Required.
163 164 165 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 163 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 what is returned from corresponding attribute methods. For example:
event["time"] # => String rfc3339 representation
event.time # => DateTime object
event.time_string # => String rfc3339 representation
124 125 126 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 124 def [] key @attributes[key.to_s] end |
#to_h ⇒ Hash
Return a hash representation of this event.
133 134 135 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 133 def to_h @attributes.dup 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.
101 102 103 104 |
# File 'lib/functions_framework/cloud_events/event/v1.rb', line 101 def with **changes attributes = @attributes.merge keys_to_strings changes V1.new attributes: attributes end |