Class: Segment::Analytics::MessageBatch

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

Overview

A batch of ‘Message`s to be sent to the API

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

Returns a new instance of MessageBatch.



12
13
14
15
16
# File 'lib/segment/analytics/message_batch.rb', line 12

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

Instance Method Details

#<<(message) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/segment/analytics/message_batch.rb', line 18

def <<(message)
  message_json_size = message.to_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



33
34
35
36
# File 'lib/segment/analytics/message_batch.rb', line 33

def clear
  @messages.clear
  @json_size = 0
end

#full?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/segment/analytics/message_batch.rb', line 29

def full?
  item_count_exhausted? || size_exhausted?
end