class Pushr::Daemon::GcmSupport::ResponseHandler

Attributes

message[RW]
response[RW]

Public Class Methods

new(response, message) click to toggle source
# File lib/pushr/daemon/gcm_support/response_handler.rb, line 6
def initialize(response, message)
  self.response = response
  self.message = message
end

Public Instance Methods

handle() click to toggle source
# File lib/pushr/daemon/gcm_support/response_handler.rb, line 11
def handle
  hsh = MultiJson.load(response.body)
  hsh['results'].each_with_index do |result, index|
    handle_single(result, message.registration_ids[index])
  end
end
handle_single(result, registration_id) click to toggle source
# File lib/pushr/daemon/gcm_support/response_handler.rb, line 18
def handle_single(result, registration_id)
  if result.key?('error')
    if result['error'] == 'NotRegistered' || result['error'] == 'InvalidRegistration'
      Pushr::FeedbackGcm.create(app: message.app, failed_at: Time.now, device: registration_id, follow_up: 'delete')
    end

    if result['error'] == 'Unavailable'
      # TODO: If it is Unavailable, you could retry to send it in another request
      m = message.clone
      m.registration_ids = [registration_id]
      m.save
    end

    # Pushr::Daemon.logger.error("[#{@name}] Error received.")
    # fail Pushr::Daemon::DeliveryError.new(@response.code, nil, msg, 'GCM', false)

  elsif result.key?('registration_id')
    # success, but update device token
    hsh = { app: message.app, failed_at: Time.now, device: registration_id,
            follow_up: 'update', update_to: result['registration_id'] }
    Pushr::FeedbackGcm.create(hsh)
  end
end