class RubyPushNotifications::WNS::WNSResponse

This class encapsulates a response received from the WNS 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 WNSResult for every receiver of the notification

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

@return [Array] Array of a WNSResult 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 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

==(other) click to toggle source
Calls superclass method
# 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

parse_response(responses) click to toggle source

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
wns_result_for(code, device_url, headers) click to toggle source

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