class RubyPushNotifications::WNS::WNSResponse
This class encapsulates a response received from the WNS
service and helps parsing and understanding the received messages/codes.
Attributes
@return [Integer] the number of failed notifications
@return [Array] Array of a WNSResult
for every receiver of the notification
sent indicating the result of the operation.
@return [Array] Array of a WNSResult
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 WNSResponse
and runs response parsing
@param responses [Array]. Array with device_urls and http responses
# File lib/ruby-push-notifications/wns/wns_response.rb, line 23 def initialize(responses) parse_response responses end
Public Instance Methods
# File lib/ruby-push-notifications/wns/wns_response.rb, line 27 def ==(other) (other.is_a?(WNSResponse) && success == other.success && failed == other.failed && results == other.results) || super(other) end
Private Instance Methods
Parses the response extracting counts for successful, failed messages. Also creates the results array assigning a WNSResult
subclass for each device URL the notification was sent to.
@param responses [Array]. Array of hash responses
# File lib/ruby-push-notifications/wns/wns_response.rb, line 41 def parse_response(responses) @success = responses.count { |response| response[:code] == 200 } @failed = responses.count { |response| response[:code] != 200 } @results = responses.map do |response| wns_result_for response[:code], response[:device_url], response[:headers] end end
Factory method that, for each WNS
result object assigns a WNSResult
subclass.
@param code [Integer]. The HTTP status code received @param device_url [String]. The receiver's WNS
device url. @param headers [Hash]. The HTTP headers received. @return [WNSResult]. Corresponding WNSResult
subclass
# File lib/ruby-push-notifications/wns/wns_response.rb, line 58 def wns_result_for(code, device_url, headers) case code when 200 WNSResultOK.new device_url, headers when 400 MalformedWNSResultError.new device_url when 401 WNSAuthError.new device_url when 404 WNSInvalidError.new device_url, headers when 406 WNSLimitError.new device_url, headers when 410 WNSExpiredError.new device_url, headers when 412 WNSPreConditionError.new device_url, headers when 500..599 WNSInternalError.new device_url else WNSResultError.new device_url end end