Class: CDP::Builders::Event

Inherits:
Object
  • Object
show all
Includes:
Value
Defined in:
lib/cdp/builders/event.rb

Constant Summary

Constants included from Value

Value::VALID_COMMAND_TYPE

Instance Method Summary collapse

Methods included from Value

build, build_payload_with_ref, supported_command

Constructor Details

#initialize(tenant_id:, event_schema:) ⇒ Event

Returns a new instance of Event.



10
11
12
13
# File 'lib/cdp/builders/event.rb', line 10

def initialize(tenant_id:, event_schema:)
  @tenant_id = tenant_id
  @event_schema = event_schema
end

Instance Method Details

#create(event) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cdp/builders/event.rb', line 15

def create(event)
  event = event.symbolize_keys

  raise ArgumentError, 'attribute event_identifier is missing' unless event.key?(:event_identifier)

  raise ArgumentError, 'attribute event_identifier is empty' if event[:event_identifier].empty?

  payload = event.except(:event_uuid, :event_identifier, :event_timestamp, :entity_uuid)

  validation_result = Validator.new(field_schema: @event_schema).create(attributes: payload)

  raise ::CDP::InvalidEntityError, validation_result.errors if validation_result.failed?

  CDP::Transaction.new(
    tenant_id: @tenant_id,
    commands: [
      CDP::Command.new(
        id: SecureRandom.uuid,
        create_event: CDP::CreateEvent.new(
          event_type: @event_schema[:name],
          event_uuid: event[:event_uuid] || SecureRandom.uuid,
          event_identifier: event[:event_identifier],
          event_timestamp: event[:event_timestamp] || Time.now.utc,
          entity_uuid: event[:entity_uuid],
          payload: build_payload_with_ref(@event_schema, payload)
        )
      )
    ]
  )
end