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
@return [Integer] the number of received canonical IDS
(https://developer.android.com/google/gcm/server-ref.html#table4)
@return [Integer] the number of failed notifications
@return [Array] Array of a GCMResult
for every receiver of the notification
sent indicating the result of the operation.
@return [Array] Array of a GCMResult
for every receiver of the notification
sent indicating the result of the operation.
@return [Integer] the number of successfully sent notifications
Public Class Methods
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
# 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
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
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