Class: RabbitFeed::EventDefinitions::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/rabbit_feed/event_definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version) ⇒ Event

Returns a new instance of Event.



35
36
37
38
39
40
# File 'lib/rabbit_feed/event_definitions.rb', line 35

def initialize(name, version)
  @name    = name
  @version = version
  @fields  = []
  @sensitive_fields = []
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



30
31
32
# File 'lib/rabbit_feed/event_definitions.rb', line 30

def definition
  @definition
end

#fieldsObject (readonly)

Returns the value of attribute fields.



30
31
32
# File 'lib/rabbit_feed/event_definitions.rb', line 30

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/rabbit_feed/event_definitions.rb', line 30

def name
  @name
end

#sensitive_fieldsObject (readonly)

Returns the value of attribute sensitive_fields.



30
31
32
# File 'lib/rabbit_feed/event_definitions.rb', line 30

def sensitive_fields
  @sensitive_fields
end

#versionObject (readonly)

Returns the value of attribute version.



30
31
32
# File 'lib/rabbit_feed/event_definitions.rb', line 30

def version
  @version
end

Instance Method Details

#defined_as(&block) ⇒ Object



51
52
53
# File 'lib/rabbit_feed/event_definitions.rb', line 51

def defined_as(&block)
  @definition = yield if block.present?
end

#event_schemaObject



71
72
73
74
75
76
# File 'lib/rabbit_feed/event_definitions.rb', line 71

def event_schema
  [
    { name: 'payload', type: payload_schema, doc: 'The event payload (defined by the source system)' },
    { name: 'metadata', type: , doc: 'The event metadata (defined by rabbit feed)' }
  ]
end

#field(name, options) ⇒ Object



46
47
48
49
# File 'lib/rabbit_feed/event_definitions.rb', line 46

def field(name, options)
  sensitive_fields << name.to_s if options.delete(:sensitive)
  fields << (Field.new name, options[:type], options[:definition])
end

#metadata_schemaObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rabbit_feed/event_definitions.rb', line 59

def 
  { name: 'event_metadata', type: 'record', fields: [
    (Field.new 'application',    'string', 'The name of the application that created the event'),
    (Field.new 'host',           'string', 'The hostname of the server on which the event was created'),
    (Field.new 'environment',    'string', 'The environment in which the event was created'),
    (Field.new 'version',        'string', 'The version of the event payload'),
    (Field.new 'schema_version', 'string', 'The version of the event schema'),
    (Field.new 'name',           'string', 'The name of the event'),
    (Field.new 'created_at_utc', 'string', 'The UTC time that the event was created')
  ].map(&:schema) }
end

#payload_contains(&block) ⇒ Object



42
43
44
# File 'lib/rabbit_feed/event_definitions.rb', line 42

def payload_contains(&block)
  instance_eval(&block)
end

#payload_schemaObject



55
56
57
# File 'lib/rabbit_feed/event_definitions.rb', line 55

def payload_schema
  { name: "#{name}_payload", type: 'record', fields: fields.map(&:schema) }
end

#schemaObject



78
79
80
# File 'lib/rabbit_feed/event_definitions.rb', line 78

def schema
  @schema ||= Avro::Schema.parse({ name: name, type: 'record', doc: definition, fields: event_schema }.to_json)
end

#validate!Object

Raises:



82
83
84
# File 'lib/rabbit_feed/event_definitions.rb', line 82

def validate!
  raise ConfigurationError, "Bad event specification for #{name}: #{errors.messages}" if invalid?
end