Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/push/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def as_json
notifications.map(&:as_json)
end

def all_recipients
notifications.flat_map(&:recipients)
end

private

attr_accessor :notifications
Expand Down
7 changes: 5 additions & 2 deletions lib/push/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def send(notifications)

threads = Chunk.for(notifications).map do |chunk|
expected_count = chunk.count

tokens = chunk.all_recipients
Thread.new do
pool.with do |http|
response = http.post(PUSH_API_URL, json: chunk.as_json)
Expand All @@ -197,7 +197,10 @@ def send(notifications)
elsif !data.is_a?(Array) || data.length != expected_count
TicketsExpectationFailed.new(expected_count: expected_count, data: data)
else
data.map { |ticket| Ticket.new(ticket) }
data.map do |ticket|
current_ticket_token = tokens.shift(1)[0]
Ticket.new(ticket, current_ticket_token)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/push/receipts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def message
data.fetch('message')
end

def error_message
data.fetch('details').fetch('error')
end

def explain
Expo::Push::Error.explain((data['details'] || {})['error'])
end
Expand Down
22 changes: 12 additions & 10 deletions lib/push/tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ module Push
# valid. This is exposed via #original_push_token.
#
class Ticket
attr_reader :data
attr_reader :data, :token

def initialize(data)
def initialize(data, token)
self.data = data
self.token = token
end

def id
data.fetch('id')
end

def original_push_token
return nil if ok?

if message.include?('PushToken[')
return /Expo(?:nent)?PushToken\[(?:[^\]]+?)\]/.match(message) { |match| match[0] }
end

/\A[a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12}\z/i.match(message) { |match| match[0] }
token
end

def message
Expand All @@ -50,7 +45,7 @@ def error?

private

attr_writer :data
attr_writer :data, :token
end

##
Expand Down Expand Up @@ -80,6 +75,13 @@ def ids
end
end

def token_by_receipt_id
tokens_by_receipt_id = {}
tokens_by_receipt_id.tap do |hash|
each { |ticket| hash[ticket.id] = ticket.original_push_token }
end
end

def batch_ids
ids.each_slice(PUSH_NOTIFICATION_RECEIPT_CHUNK_LIMIT).to_a
end
Expand Down