Class: Chronos::Core::PayloadSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/core/payload_serializer.rb

Overview

Converts a Notice into the versioned Chronos JSON envelope.

Examples:

event = serializer.call(notice)
event.body #=> "{...}"

Constant Summary collapse

MAX_DEPTH =
10
MAX_KEYS =
100
MAX_ITEMS =
100
MAX_STRING_BYTES =
8192

Instance Method Summary collapse

Constructor Details

#initialize(config, clock = nil) ⇒ PayloadSerializer

Returns a new instance of PayloadSerializer.



46
47
48
49
# File 'lib/chronos/core/payload_serializer.rb', line 46

def initialize(config, clock = nil)
  @config = config
  @clock = clock || proc { Time.now }
end

Instance Method Details

#call(notice) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
# File 'lib/chronos/core/payload_serializer.rb', line 51

def call(notice)
  envelope = build_envelope(notice)
  body = JSON.generate(envelope)
  body = JSON.generate(compact_envelope(envelope)) if body.bytesize > @config.max_payload_size
  raise Error, "event exceeds max_payload_size" if body.bytesize > @config.max_payload_size

  SerializedEvent.new(notice.event_id, body)
end