class Rpush::Daemon::Wpns::Delivery

msdn.microsoft.com/en-us/library/windowsphone/develop/ff941100%28v=vs.105%29.aspx

Constants

FAILURE_MESSAGES

Public Class Methods

new(app, http, notification, batch) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 11
def initialize(app, http, notification, batch)
  @app = app
  @http = http
  @notification = notification
  @batch = batch
end

Public Instance Methods

perform() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 18
def perform
  handle_response(do_post)
rescue SocketError => error
  mark_retryable(@notification, Time.now + 10.seconds, error)
  raise
rescue StandardError => error
  mark_failed(error)
  raise
ensure
  @batch.notification_processed
end

Private Instance Methods

clean_param_string(string) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 125
def clean_param_string(string)
  string.gsub(/&/, "&amp;").gsub(/</, "&lt;") \
    .gsub(/>/, "&gt;").gsub(/'/, "&apos;").gsub(/"/, "&quot;")
end
do_post() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 92
def do_post
  body = notification_to_xml
  post = Net::HTTP::Post.new(URI.parse(@notification.uri).path, "Content-Length" => body.length.to_s,
                                                                "Content-Type" => "text/xml",
                                                                "X-WindowsPhone-Target" => "toast",
                                                                "X-NotificationClass" => '2')
  post.body = body
  @http.request(URI.parse(@notification.uri), post)
end
handle_failure(code, msg = nil) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 48
def handle_failure(code, msg = nil)
  unless msg
    msg = FAILURE_MESSAGES.key?(code) ? FAILURE_MESSAGES[code] : Rpush::Daemon::HTTP_STATUS_CODES[code]
  end
  fail Rpush::DeliveryError.new(code, @notification.id, msg)
end
handle_response(response) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 32
def handle_response(response)
  code = response.code.to_i
  case code
  when 200
    ok(response)
  when 406
    not_acceptable
  when 412
    precondition_failed
  when 503
    service_unavailable
  else
    handle_failure(code)
  end
end
not_acceptable() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 69
def not_acceptable
  retry_notification("Per-day throttling limit reached.")
end
notification_to_xml() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 111
def notification_to_xml
  title = clean_param_string(@notification.data['title']) if @notification.data['title'].present?
  body = clean_param_string(@notification.data['body']) if @notification.data['body'].present?
  param = clean_param_string(@notification.data['param']) if @notification.data['param'].present?
  "<?xml version=\"1.0\" encoding=\"utf-8\"?>
   <wp:Notification xmlns:wp=\"WPNotification\">
     <wp:Toast>
       <wp:Text1>#{title}</wp:Text1>
       <wp:Text2>#{body}</wp:Text2>
       <wp:Param>#{param}</wp:Param>
     </wp:Toast>
   </wp:Notification>"
end
ok(response) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 55
def ok(response)
  status = status_from_response(response)
  case status[:notification]
  when ["Received"]
    mark_delivered
    log_info("#{@notification.id} sent successfully")
  when ["QueueFull"]
    mark_retryable(@notification, Time.now + (60 * 10))
    log_warn("#{@notification.id} cannot be sent. The Queue is full.")
  when ["Suppressed"]
    handle_failure(200, "Notification was received but suppressed by the service.")
  end
end
precondition_failed() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 73
def precondition_failed
  retry_notification("Device unreachable.")
end
retry_message() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 82
def retry_message
  "Notification #{@notification.id} will be retried after #{@notification.deliver_after.strftime('%Y-%m-%d %H:%M:%S')} (retry #{@notification.retries})."
end
retry_notification(reason) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 86
def retry_notification(reason)
  deliver_after = Time.now + (60 * 60)
  mark_retryable(@notification, deliver_after)
  log_warn("#{reason} " + retry_message)
end
service_unavailable() click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 77
def service_unavailable
  mark_retryable_exponential(@notification)
  log_warn("Service Unavailable. " + retry_message)
end
status_from_response(response) click to toggle source
# File lib/rpush/daemon/wpns/delivery.rb, line 102
def status_from_response(response)
  headers = response.to_hash
  {
    notification:         headers["x-notificationstatus"],
    notification_channel: headers["x-subscriptionstatus"],
    device_connection:    headers["x-deviceconnectionstatus"]
  }
end