Class: RubyPushNotifications::WNS::WNSConnection

Inherits:
Object
  • Object
show all
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

Class Method Details

.build_headers(access_token, type, body_length) ⇒ Hash

Parameters:

  • type (String)

    . Access token

  • type (Symbol)

    . The type of notification

  • type (String)

    . Content length of body

Returns:

  • (Hash)

    . Correct delay based on notification type



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

Parameters:

  • response (Net::HTTPResponse)

    . HTTP response for request

Returns:

  • (Hash)

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



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.

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

Parameters:

  • notifications (WNSNotification)

    . The notifications object to POST

  • access_token (String)

    required. Access token for send push

  • 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



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, options = {})
  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 = options.fetch(:open_timeout, 30)
    http.read_timeout = options.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