Class: Expo::Push::Chunk
- Inherits:
-
Object
- Object
- Expo::Push::Chunk
- Defined in:
- lib/push/chunk.rb
Overview
rubocop:disable Style/Documentation
Instance Attribute Summary collapse
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
Class Method Summary collapse
-
.for(notifications) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
Instance Method Summary collapse
- #<<(notification) ⇒ Object
- #as_json ⇒ Object
- #count ⇒ Object
-
#initialize ⇒ Chunk
constructor
A new instance of Chunk.
Constructor Details
#initialize ⇒ Chunk
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
#remaining ⇒ Object
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_json ⇒ Object
48 49 50 |
# File 'lib/push/chunk.rb', line 48 def as_json notifications.map(&:as_json) end |
#count ⇒ Object
44 45 46 |
# File 'lib/push/chunk.rb', line 44 def count notifications.sum(&:count) end |