class RubyPushNotifications::GCM::GCMResponse

This class encapsulates a response received from the GCM service and helps parsing and understanding the received meesages/codes.

@author Carlos Alonso

Attributes

canonical_ids[R]

@return [Integer] the number of received canonical IDS

(https://developer.android.com/google/gcm/server-ref.html#table4)
failed[R]

@return [Integer] the number of failed notifications

individual_results[R]

@return [Array] Array of a GCMResult for every receiver of the notification

sent indicating the result of the operation.
results[R]

@return [Array] Array of a GCMResult for every receiver of the notification

sent indicating the result of the operation.
success[R]

@return [Integer] the number of successfully sent notifications

Public Class Methods

new(code, body) click to toggle source

Initializes the GCMResponse and runs response parsing

@param code [Integer]. The HTTP status code received @param body [String]. The response body received. @raise MalformedGCMJsonError if code == 400 Bad Request @raise GCMAuthError if code == 401 Unauthorized @raise GCMInternalError if code == 5xx @raise UnexpectedGCMResponseError if code != 200

# File lib/ruby-push-notifications/gcm/gcm_response.rb, line 34
def initialize(code, body)
  case code
  when 200
    parse_response body
  when 400
    raise MalformedGCMJSONError, body
  when 401
    raise GCMAuthError, body
  when 500..599
    raise GCMInternalError, body
  else
    raise UnexpectedGCMResponseError, code
  end
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/ruby-push-notifications/gcm/gcm_response.rb, line 49
def ==(other)
  (other.is_a?(GCMResponse) &&
    success == other.success &&
    failed == other.failed &&
    canonical_ids == other.canonical_ids &&
    results == other.results) || super(other)
end

Private Instance Methods

gcm_result_for(result) click to toggle source

Factory method that, for each GCM result object assigns a GCMResult subclass.

@param result [Hash]. GCM Result parsed hash @return [GCMResult]. Corresponding GCMResult subclass

# File lib/ruby-push-notifications/gcm/gcm_response.rb, line 78
def gcm_result_for(result)
  if canonical_id = result[:registration_id]
    GCMCanonicalIDResult.new canonical_id
  elsif error = result[:error]
    GCMResultError.new error
  else
    GCMResultOK.new
  end
end
parse_response(body) click to toggle source

Parses the response extracting counts for successful, failed and containing canonical ID messages. Also creates the results array assigning a GCMResult subclass for each registration ID the notification was sent to.

@param body [String]. The response body

# File lib/ruby-push-notifications/gcm/gcm_response.rb, line 65
def parse_response(body)
  json = JSON.parse body, symbolize_names: true
  @success = json[:success]
  @failed = json[:failure]
  @canonical_ids = json[:canonical_ids]
  @results = (json[:results] || []).map { |result| gcm_result_for result }
end