Class: Pushr::Daemon::GcmSupport::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pushr/daemon/gcm_support/response_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, message) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



6
7
8
9
# File 'lib/pushr/daemon/gcm_support/response_handler.rb', line 6

def initialize(response, message)
  self.response = response
  self.message = message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/pushr/daemon/gcm_support/response_handler.rb', line 5

def message
  @message
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/pushr/daemon/gcm_support/response_handler.rb', line 5

def response
  @response
end

Instance Method Details

#handleObject



11
12
13
14
15
16
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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