Class: RubyPushNotifications::MPNS::MPNSConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-push-notifications/mpns/mpns_connection.rb

Overview

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

Constant Summary collapse

CONTENT_TYPE_HEADER =
'Content-Type'
X_NOTIFICATION_CLASS =
'X-NotificationClass'
X_WINDOWSPHONE_TARGET =
'X-WindowsPhone-Target'
XML_CONTENT_TYPE =
'text/xml'
BASEBATCH =
{ tile: 1, toast: 2, raw: 3 }
BATCHADDS =
{ delay450: 10, delay900: 20 }
WP_TARGETS =
{ toast: 'toast', tile: 'token' }

Class Method Summary collapse

Class Method Details

.build_headers(type, delay) ⇒ Hash

Build Header based on type and delay

Parameters:

  • type (Symbol)

    . The type of notification

  • delay (Symbol)

    . The delay to be used

Returns:

  • (Hash)

    . Correct delay based on notification type



66
67
68
69
70
71
72
73
# 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) ⇒ Hash

Extract headers from response

Parameters:

  • response (Net::HTTPResponse)

    . HTTP response for request

Returns:

  • (Hash)

    . Hash with headers with case-insensitive keys and string values



79
80
81
82
83
# 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 = {}) ⇒ Array

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

(msdn.microsoft.com/pt-br/library/windows/apps/ff941099)

Parameters:

  • n (MPNSNotification)

    . The notification object to POST

  • cert (String) (defaults to: nil)

    optional. Contents of the PEM encoded certificate.

  • options (Hash) (defaults to: {})

    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.

Returns:

  • (Array)

    . The response of post



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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