Class: RabbitFeed::Event

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

Constant Summary collapse

SCHEMA_VERSION =
'2.0.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, payload = {}, schema = nil, sensitive_fields = []) ⇒ Event

Returns a new instance of Event.



12
13
14
15
16
17
18
# File 'lib/rabbit_feed/event.rb', line 12

def initialize(, payload = {}, schema = nil, sensitive_fields = [])
  @schema   = schema
  @payload  = payload.with_indifferent_access if payload
  @metadata = .with_indifferent_access if 
  @sensitive_fields = Array(sensitive_fields).map(&:to_s).flatten
  validate!
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/rabbit_feed/event.rb', line 7

def 
  @metadata
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/rabbit_feed/event.rb', line 7

def payload
  @payload
end

#schemaObject (readonly)

Returns the value of attribute schema.



7
8
9
# File 'lib/rabbit_feed/event.rb', line 7

def schema
  @schema
end

#sensitive_fieldsObject (readonly)

Returns the value of attribute sensitive_fields.



7
8
9
# File 'lib/rabbit_feed/event.rb', line 7

def sensitive_fields
  @sensitive_fields
end

Class Method Details

.deserialize(serialized_event) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rabbit_feed/event.rb', line 43

def deserialize(serialized_event)
  datum_reader = Avro::IO::DatumReader.new
  reader       = Avro::DataFile::Reader.new (StringIO.new serialized_event), datum_reader
  event_hash   = nil
  reader.each do |datum|
    event_hash = datum
  end
  reader.close
  new event_hash['metadata'], event_hash['payload'], datum_reader.readers_schema
end

Instance Method Details

#applicationObject



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

def application
  [:application]
end

#created_at_utcObject



38
39
40
# File 'lib/rabbit_feed/event.rb', line 38

def created_at_utc
  (Time.iso8601 [:created_at_utc]) if [:created_at_utc].present?
end

#nameObject



34
35
36
# File 'lib/rabbit_feed/event.rb', line 34

def name
  [:name]
end

#serializeObject



20
21
22
23
24
25
26
27
28
# File 'lib/rabbit_feed/event.rb', line 20

def serialize
  buffer = StringIO.new
  writer = Avro::DataFile::Writer.new buffer, (Avro::IO::DatumWriter.new schema), schema
  writer << { 'metadata' => , 'payload' => payload }
  writer.close
  buffer.string
rescue Avro::IO::AvroTypeError
  raise Avro::IO::AvroTypeError.new(schema, sensitive_proof_payload)
end