Class: RubyPushNotifications::MPNS::MPNSConnection
- Inherits:
-
Object
- Object
- RubyPushNotifications::MPNS::MPNSConnection
- 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
-
.build_headers(type, delay) ⇒ Hash
Build Header based on type and delay.
-
.extract_headers(response) ⇒ Hash
Extract headers from response.
-
.post(n, cert = nil, options = {}) ⇒ Array
Issues a POST request to the MPNS send endpoint to submit the given notifications.
Class Method Details
.build_headers(type, delay) ⇒ Hash
Build Header based on type and delay
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
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.
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, = {}) 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 = .fetch(:open_timeout, 30) http.read_timeout = .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 |