Class: Rudder::Analytics::MessageBatch

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

Overview

A batch of ‘Message`s 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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(max_message_count) ⇒ MessageBatch

Returns a new instance of MessageBatch.



18
19
20
21
22
# File 'lib/rudder/analytics/message_batch.rb', line 18

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

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



16
17
18
# File 'lib/rudder/analytics/message_batch.rb', line 16

def messages
  @messages
end

Instance Method Details

#<<(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rudder/analytics/message_batch.rb', line 24

def <<(message)
  begin
    message_json = message.to_json
    # puts message_json
  rescue StandardError => e
    raise JSONGenerationError, "Serialization error: #{e} for message: #{message.inspect}"
  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

#clearObject



45
46
47
48
# File 'lib/rudder/analytics/message_batch.rb', line 45

def clear
  @messages.clear
  @json_size = 0
end

#full?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rudder/analytics/message_batch.rb', line 41

def full?
  item_count_exhausted? || size_exhausted?
end