class RubyPushNotifications::FCM::FCMResponse
This class encapsulates a response received from the FCM
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/fcm/server-ref.html#table4)
@return [Integer] the number of failed notifications
@return [Array] Array of a FCMResult
for every receiver of the notification
sent indicating the result of the operation.
@return [Array] Array of a FCMResult
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 FCMResponse
and runs response parsing
@param code [Integer]. The HTTP status code received @param body [String]. The response body received. @raise MalformedFCMJsonError if code == 400 Bad Request @raise FCMAuthError
if code == 401 Unauthorized @raise FCMInternalError
if code == 5xx @raise UnexpectedFCMResponseError
if code != 200
# File lib/ruby-push-notifications/fcm/fcm_response.rb, line 35 def initialize(code, body) case code when 200 parse_response body when 400 raise MalformedFCMJSONError, body when 401 raise FCMAuthError, body when 500..599 raise FCMInternalError, body else raise UnexpectedFCMResponseError, code end end
Public Instance Methods
# File lib/ruby-push-notifications/fcm/fcm_response.rb, line 50 def ==(other) (other.is_a?(FCMResponse) && 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 FCM
result object assigns a FCMResult
subclass.
@param result [Hash]. FCM
Result parsed hash @return [FCMResult]. Corresponding FCMResult
subclass
# File lib/ruby-push-notifications/fcm/fcm_response.rb, line 79 def fcm_result_for(result) if canonical_id = result[:registration_id] FCMCanonicalIDResult.new canonical_id elsif error = result[:error] FCMResultError.new error else FCMResultOK.new end end
Parses the response extracting counts for successful, failed and containing canonical ID messages. Also creates the results array assigning a FCMResult
subclass for each registration ID the notification was sent to.
@param body [String]. The response body
# File lib/ruby-push-notifications/fcm/fcm_response.rb, line 66 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| fcm_result_for result } end