Class: PostHog::MessageBatch Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Defaults::MessageBatch, Logging
Defined in:
lib/posthog/message_batch.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A batch of messages to be sent to the API.

Defined Under Namespace

Classes: JSONGenerationError

Constant Summary

Constants included from Defaults::MessageBatch

Defaults::MessageBatch::MAX_BYTES, Defaults::MessageBatch::MAX_SIZE

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(max_message_count) ⇒ MessageBatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MessageBatch.

Parameters:

  • max_message_count (Integer)

    Maximum number of messages in the batch.



19
20
21
22
23
# File 'lib/posthog/message_batch.rb', line 19

def initialize(max_message_count)
  @messages = []
  @max_message_count = max_message_count
  @json_size = 0
end

Instance Method Details

#<<(message) ⇒ Array<Hash>?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • message (Hash)

    Message to add to the batch.

Returns:

  • (Array<Hash>, nil)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/posthog/message_batch.rb', line 27

def <<(message)
  begin
    message_json = message.to_json
  rescue StandardError => e
    raise JSONGenerationError, "Serialization error: #{e}"
  end

  message_json_size = message_json.bytesize
  if message_too_big?(message_json_size)
    logger.error('a message exceeded the maximum allowed size')
  else
    @messages << message
    @json_size += message_json_size + 1 # One byte for the comma
  end
end

#clearvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



49
50
51
52
# File 'lib/posthog/message_batch.rb', line 49

def clear
  @messages.clear
  @json_size = 0
end

#full?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Whether the batch is full.

Returns:

  • (Boolean)

    Whether the batch is full.



44
45
46
# File 'lib/posthog/message_batch.rb', line 44

def full?
  item_count_exhausted? || size_exhausted?
end