Module: CloudEvents::Event

Included in:
V0, V1
Defined in:
lib/cloud_events/event.rb,
lib/cloud_events/event/v0.rb,
lib/cloud_events/event/v1.rb,
lib/cloud_events/event/field_interpreter.rb

Overview

An Event object represents a complete CloudEvent, including both data and context attributes. The following are true of all event objects:

  • Event classes are defined within this module. For example, events conforming to the CloudEvents 1.0 specification are of type V1.
  • All event classes include this module itself, so you can use is_a? CloudEvents::Event to test whether an object is an event.
  • All event objects are immutable. Data and attribute values can be retrieved but not modified. To "modify" an event, make a copy with the desired changes. Generally, event classes will provide a helper method for this purpose.
  • All event objects have a spec_version method that returns the version of the CloudEvents spec implemented by that event. (Other methods may be different, depending on the spec version.)

To create an event, you may either:

  • Construct an instance of the event class directly, for example by calling V1.new and passing a set of attributes.
  • Call Event.create and pass a spec version and a set of attributes. This will choose the appropriate event class based on the version.
  • Decode an event from another representation. For example, use JsonFormat to decode an event from JSON, or use HttpBinding to decode an event from an HTTP request.

See https://github.com/cloudevents/spec for more information about CloudEvents. The documentation for the individual event classes V0 and V1 also include links to their respective specifications.

Defined Under Namespace

Classes: V0, V1

Class Method Summary collapse

Class Method Details

.create(spec_version:, **kwargs) ⇒ Object Also known as: new

Create a new cloud event object with the given version. Generally, you must also pass additional keyword arguments providing the event's data and attributes. For example, if you pass 1.0 as the spec_version, the remaining keyword arguments will be passed through to the V1 constructor.

Parameters:

  • spec_version (String)

    The required specversion field.

  • kwargs (keywords)

    Additional parameters for the event.



56
57
58
59
60
61
62
63
64
65
# File 'lib/cloud_events/event.rb', line 56

def create spec_version:, **kwargs
  case spec_version
  when "0.3"
    V0.new spec_version: spec_version, **kwargs
  when /^1(\.|$)/
    V1.new spec_version: spec_version, **kwargs
  else
    raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
  end
end