class RubyPushNotifications::MPNS::MPNSConnection

Encapsulates a connection to the MPNS service Responsible for final connection with the service.

Constants

BASEBATCH

@private Enumators for notification types

BATCHADDS

@private Enumators for delay

CONTENT_TYPE_HEADER

@private Content-Type HTTP Header string

WP_TARGETS

@private Windows Phone Target Types

XML_CONTENT_TYPE

@private text/xml content type

X_NOTIFICATION_CLASS

@private text/xml content type

X_WINDOWSPHONE_TARGET

@private Windows Phone Target

Public Class Methods

build_headers(type, delay) click to toggle source

Build Header based on type and delay

@param type [Symbol]. The type of notification @param delay [Symbol]. The delay to be used @return [Hash]. Correct delay based on notification type

# File lib/ruby-push-notifications/mpns/mpns_connection.rb, line 66
def self.build_headers(type, delay)
  headers = {
    CONTENT_TYPE_HEADER => XML_CONTENT_TYPE,
    X_NOTIFICATION_CLASS => "#{(BASEBATCH[type] + (BATCHADDS[delay] || 0))}"
  }
  headers[X_WINDOWSPHONE_TARGET] = WP_TARGETS[type] unless type == :raw
  headers
end
extract_headers(response) click to toggle source

Extract headers from response @param response [Net::HTTPResponse]. HTTP response for request

@return [Hash]. Hash with headers with case-insensitive keys and string values

# File lib/ruby-push-notifications/mpns/mpns_connection.rb, line 79
def self.extract_headers(response)
  headers = {}
  response.each_header { |k, v| headers[k] = v }
  headers
end
post(n, cert = nil, options = {}) click to toggle source

Issues a POST request to the MPNS send endpoint to submit the given notifications.

@param n [MPNSNotification]. The notification object to POST @param cert [String] optional. Contents of the PEM encoded certificate. @param options [Hash] optional. Options for GCMPusher. Currently supports:

* open_timeout [Integer]: Number of seconds to wait for the connection to open. Defaults to 30.
* read_timeout [Integer]: Number of seconds to wait for one block to be read. Defaults to 30.

@return [Array]. The response of post (msdn.microsoft.com/pt-br/library/windows/apps/ff941099)

# File lib/ruby-push-notifications/mpns/mpns_connection.rb, line 42
def self.post(n, cert = nil, options = {})
  headers = build_headers(n.data[:type], n.data[:delay])
  body = n.as_mpns_xml
  responses = []
  n.each_device do |url|
    http = Net::HTTP.new url.host, url.port
    http.open_timeout = options.fetch(:open_timeout, 30)
    http.read_timeout = options.fetch(:read_timeout, 30)
    if cert && url.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      http.ca_file = cert
    end
    response = http.post url.path, body, headers
    responses << { device_url: url.to_s, headers: extract_headers(response), code: response.code.to_i }
  end
  MPNSResponse.new responses
end