class RubyPushNotifications::MPNS::MPNSResponse

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

Attributes

failed[R]

@return [Integer] the number of failed notifications

individual_results[R]

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

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

@return [Array] Array of a MPNSResult 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(responses) click to toggle source

Initializes the MPNSResponse and runs response parsing

@param responses [Array]. Array with device_urls and http responses

# File lib/ruby-push-notifications/mpns/mpns_response.rb, line 24
def initialize(responses)
  parse_response responses
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/ruby-push-notifications/mpns/mpns_response.rb, line 28
def ==(other)
  (other.is_a?(MPNSResponse) &&
    success == other.success &&
    failed == other.failed &&
    results == other.results) || super(other)
end

Private Instance Methods

mpns_result_for(code, device_url, headers) click to toggle source

Factory method that, for each MPNS result object assigns a MPNSResult subclass.

@param code [Integer]. The HTTP status code received @param device_url [String]. The receiver's MPNS device url. @param headers [Hash]. The HTTP headers received. @return [MPNSResult]. Corresponding MPNSResult subclass

# File lib/ruby-push-notifications/mpns/mpns_response.rb, line 59
def mpns_result_for(code, device_url, headers)
  case code
  when 200
    MPNSResultOK.new device_url, headers
  when 400
    MalformedMPNSResultError.new device_url
  when 401
    MPNSAuthError.new device_url
  when 404
    MPNSInvalidError.new device_url, headers
  when 406
    MPNSLimitError.new device_url, headers
  when 412
    MPNSPreConditionError.new device_url, headers
  when 500..599
    MPNSInternalError.new device_url
  else
    MPNSResultError.new device_url
  end
end
parse_response(responses) click to toggle source

Parses the response extracting counts for successful, failed messages. Also creates the results array assigning a MPNSResult subclass for each device URL the notification was sent to.

@param responses [Array]. Array of hash responses

# File lib/ruby-push-notifications/mpns/mpns_response.rb, line 42
def parse_response(responses)
  @success = responses.count { |response| response[:code] == 200 }
  @failed = responses.count { |response| response[:code] != 200 }
  @results = responses.map do |response|
    mpns_result_for response[:code],
                    response[:device_url],
                    response[:headers]
  end
end