Class: Expo::Push::Chunk

Inherits:
Object
  • Object
show all
Defined in:
lib/push/chunk.rb

Overview

rubocop:disable Style/Documentation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChunk

Returns a new instance of Chunk.



32
33
34
35
# File 'lib/push/chunk.rb', line 32

def initialize
  self.notifications = []
  self.remaining = PUSH_NOTIFICATION_CHUNK_LIMIT
end

Instance Attribute Details

#remainingObject

Returns the value of attribute remaining.



30
31
32
# File 'lib/push/chunk.rb', line 30

def remaining
  @remaining
end

Class Method Details

.for(notifications) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/push/chunk.rb', line 6

def self.for(notifications) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  Array(notifications).each_with_object([]) do |notification, chunks|
    # There can be at most n notifications in a chunk. This finds the last chunk,
    # checks how much space is left, and generates a new chunk if necessary.
    chunk = chunks.last || Chunk.new.tap { |c| chunks << c }

    targets = notification.recipients.dup

    while targets.length.positive?
      chunk = Chunk.new.tap { |c| chunks << c } if chunk.remaining <= 0

      # Take at most <remaining> destinations for this notificiation.
      count = [targets.length, chunk.remaining].min
      chunk_targets = targets.slice(0, count)

      # Prepare the notification
      chunk << notification.prepare(chunk_targets)

      # Remove targets from the targets list
      targets = targets.drop(count)
    end
  end
end

Instance Method Details

#<<(notification) ⇒ Object



37
38
39
40
41
42
# File 'lib/push/chunk.rb', line 37

def <<(notification)
  self.remaining -= notification.count
  notifications << notification

  self
end

#as_jsonObject



48
49
50
# File 'lib/push/chunk.rb', line 48

def as_json
  notifications.map(&:as_json)
end

#countObject



44
45
46
# File 'lib/push/chunk.rb', line 44

def count
  notifications.sum(&:count)
end