Class: RubyPushNotifications::WNS::WNSConnection
- Inherits:
-
Object
- Object
- RubyPushNotifications::WNS::WNSConnection
- Defined in:
- lib/ruby-push-notifications/wns/wns_connection.rb
Overview
Encapsulates a connection to the WNS service Responsible for final connection with the service.
Constant Summary collapse
- CONTENT_TYPE_HEADER =
'Content-Type'.freeze
- CONTENT_LENGTH_HEADER =
'Content-Length'.freeze
- X_WNS_TYPE_HEADER =
'X-WNS-Type'.freeze
- AUTHORIZATION_HEADER =
'Authorization'.freeze
- REQUEST_FOR_STATUS_HEADER =
'X-WNS-RequestForStatus'.freeze
- CONTENT_TYPE =
{ badge: 'text/xml', toast: 'text/xml', tile: 'text/xml', raw: 'application/octet-stream' }.freeze
- WP_TARGETS =
{ badge: 'wns/badge', toast: 'wns/toast', tile: 'wns/tile', raw: 'wns/raw' }.freeze
Class Method Summary collapse
-
.build_headers(access_token, type, body_length) ⇒ Hash
Build Header based on type and delay.
-
.capitalize_headers(response) ⇒ Hash
Extract headers from response.
-
.post(notifications, access_token, options = {}) ⇒ Array
Issues a POST request to the WNS send endpoint to submit the given notifications.
Class Method Details
.build_headers(access_token, type, body_length) ⇒ Hash
Build Header based on type and delay
msdn.microsoft.com/en-us/library/windows/apps/hh465435.aspx#send_notification_request
76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby-push-notifications/wns/wns_connection.rb', line 76 def self.build_headers(access_token, type, body_length) { CONTENT_TYPE_HEADER => CONTENT_TYPE[type], X_WNS_TYPE_HEADER => WP_TARGETS[type], CONTENT_LENGTH_HEADER => body_length, AUTHORIZATION_HEADER => "Bearer #{access_token}", REQUEST_FOR_STATUS_HEADER => 'true' } end |
.capitalize_headers(response) ⇒ Hash
Extract headers from response
90 91 92 93 94 |
# File 'lib/ruby-push-notifications/wns/wns_connection.rb', line 90 def self.capitalize_headers(response) headers = {} response.each_header { |k, v| headers[k] = v.capitalize } headers end |
.post(notifications, access_token, options = {}) ⇒ Array
Issues a POST request to the WNS send endpoint to submit the given notifications.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby-push-notifications/wns/wns_connection.rb', line 51 def self.post(notifications, access_token, = {}) body = notifications.as_wns_xml headers = build_headers(access_token, notifications.data[:type], body.length.to_s) responses = [] notifications.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 url.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end response = http.post url.request_uri, body, headers responses << { device_url: url.to_s, headers: capitalize_headers(response), code: response.code.to_i } end WNSResponse.new responses end |