module Rpush
module Daemon
module Wns
class ToastRequest
def self.create(notification, access_token)
body = ToastRequestPayload.new(notification).to_xml
uri = URI.parse(notification.uri)
post = Net::HTTP::Post.new(
uri.request_uri,
"Content-Length" => body.length.to_s,
"Content-Type" => "text/xml",
"X-WNS-Type" => "wns/toast",
"X-WNS-RequestForStatus" => "true",
"Authorization" => "Bearer #{access_token}"
)
post.body = body
post
end
end
class ToastRequestPayload
def initialize(notification)
@title = notification.data['title'] || ''
@body = notification.data['body'] || ''
@launch = notification.data['launch']
@sound = notification.sound unless notification.sound.eql?("default".freeze)
end
def to_xml
launch_string = "" unless @launch
launch_string = " launch='#{CleanParamString.clean(@launch)}'" if @launch
audio_string = "" unless @sound
audio_string = "" if @sound
"
#{CleanParamString.clean(@title)}
#{CleanParamString.clean(@body)}
#{audio_string}
"
end
end
class CleanParamString
def self.clean(string)
string.gsub(/&/, "&").gsub(/, "<") \
.gsub(/>/, ">").gsub(/'/, "'").gsub(/"/, """)
end
end
end
end
end