Class: FunctionsFramework::CloudEvents::Event

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

Overview

A cloud event data type.

This object represents both the event data and the context attributes. It 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 various attributes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, source:, type:, spec_version:, data: nil, data_content_type: nil, data_schema: nil, subject: nil, time: nil) ⇒ Event

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

Parameters:

  • id (String)

    The required id field

  • source (String, URI)

    The required source field

  • type (String)

    The required type field

  • spec_version (String)

    The required specversion field

  • data (String, Boolean, Integer, Array, Hash) (defaults to: nil)

    The optional data field

  • data_content_type (String, FunctionsFramework::CloudEvents::ContentType) (defaults to: nil)

    The optional datacontenttype field

  • data_schema (String, URI) (defaults to: nil)

    The optional dataschema field

  • subject (String) (defaults to: nil)

    The optional subject field

  • time (String, DateTime) (defaults to: nil)

    The optional time field



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/functions_framework/cloud_events/event.rb', line 47

def initialize \
    id:,
    source:,
    type:,
    spec_version:,
    data: nil,
    data_content_type: nil,
    data_schema: nil,
    subject: nil,
    time: nil
  @id = interpret_string "id", id, true
  @source, @source_string = interpret_uri "source", source, true
  @type = interpret_string "type", type, true
  @spec_version = interpret_string "spec_version", spec_version, true
  @data = data
  @data_content_type, @data_content_type_string =
    interpret_content_type "data_content_type", data_content_type
  @data_schema, @data_schema_string = interpret_uri "data_schema", data_schema
  @subject = interpret_string "subject", subject
  @time, @time_string = interpret_date_time "time", time
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 ASCII-8BIT encoding
  • A string in some other encoding such as UTF-8 or US-ASCII
  • Any JSON data type, such as String, boolean, Integer, Array, or Hash

Returns:

  • (Object)


133
134
135
# File 'lib/functions_framework/cloud_events/event.rb', line 133

def data
  @data
end

#data_content_typeFunctionsFramework::CloudEvents::ContentType? (readonly) Also known as: datacontenttype

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



142
143
144
# File 'lib/functions_framework/cloud_events/event.rb', line 142

def data_content_type
  @data_content_type
end

#data_content_type_stringString? (readonly) Also known as: datacontenttype_string

The string representation of the optional datacontenttype field, or nil if the field is absent

Returns:

  • (String, nil)


151
152
153
# File 'lib/functions_framework/cloud_events/event.rb', line 151

def data_content_type_string
  @data_content_type_string
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)


160
161
162
# File 'lib/functions_framework/cloud_events/event.rb', line 160

def data_schema
  @data_schema
end

#data_schema_stringString? (readonly) Also known as: dataschema_string

The string representation of the optional dataschema field, or nil if the field is absent

Returns:

  • (String, nil)


169
170
171
# File 'lib/functions_framework/cloud_events/event.rb', line 169

def data_schema_string
  @data_schema_string
end

#idString (readonly)

The id field

Returns:

  • (String)


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

def id
  @id
end

#sourceURI (readonly)

The source field as a URI object

Returns:

  • (URI)


102
103
104
# File 'lib/functions_framework/cloud_events/event.rb', line 102

def source
  @source
end

#source_stringString (readonly)

The string representation of the source field

Returns:

  • (String)


108
109
110
# File 'lib/functions_framework/cloud_events/event.rb', line 108

def source_string
  @source_string
end

#spec_versionString (readonly) Also known as: specversion

The specversion field

Returns:

  • (String)


120
121
122
# File 'lib/functions_framework/cloud_events/event.rb', line 120

def spec_version
  @spec_version
end

#subjectString? (readonly)

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

Returns:

  • (String, nil)


177
178
179
# File 'lib/functions_framework/cloud_events/event.rb', line 177

def subject
  @subject
end

#timeDateTime? (readonly)

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

Returns:

  • (DateTime, nil)


185
186
187
# File 'lib/functions_framework/cloud_events/event.rb', line 185

def time
  @time
end

#time_stringString? (readonly)

The string representation of the optional time field, or nil if the field is absent

Returns:

  • (String, nil)


193
194
195
# File 'lib/functions_framework/cloud_events/event.rb', line 193

def time_string
  @time_string
end

#typeString (readonly)

The type field

Returns:

  • (String)


114
115
116
# File 'lib/functions_framework/cloud_events/event.rb', line 114

def type
  @type
end

Instance Method Details

#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.

Parameters:

  • changes (keywords)

    See #initialize for a list of arguments.

Returns:

  • (FunctionFramework::CloudEvents::Event)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/functions_framework/cloud_events/event.rb', line 76

def with **changes
  params = {
    id:                id,
    source:            source,
    type:              type,
    spec_version:      spec_version,
    data:              data,
    data_content_type: data_content_type,
    data_schema:       data_schema,
    subject:           subject,
    time:              time
  }
  params.merge! changes
  Event.new(**params)
end