forked from AnilRh/rpush
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rpush#238 from mseppae/master
Add support for windows raw notifications (in json form)
- Loading branch information
Showing
15 changed files
with
259 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Rpush | ||
module Client | ||
module ActiveRecord | ||
module Wns | ||
class RawNotification < Rpush::Client::ActiveRecord::Notification | ||
validates_with Rpush::Client::ActiveModel::PayloadDataSizeValidator, | ||
limit: 5120 | ||
include Rpush::Client::ActiveModel::Wns::Notification | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rpush | ||
module Client | ||
module Mongoid | ||
module Wns | ||
class RawNotification < Rpush::Client::Mongoid::Notification | ||
include Rpush::Client::ActiveModel::Wns::Notification | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rpush | ||
module Client | ||
module Redis | ||
module Wns | ||
class RawNotification < Rpush::Client::Redis::Notification | ||
include Rpush::Client::ActiveModel::Wns::Notification | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Rpush | ||
module Daemon | ||
module Wns | ||
class PostRequest | ||
def self.create(notification, access_token) | ||
is_raw_notification = lambda do |n| | ||
n.class.name.match(/RawNotification/) | ||
end | ||
|
||
stringify_keys = lambda do |data| | ||
data.keys.each { |key| data[key.to_s || key] = data.delete(key) } | ||
end | ||
|
||
stringify_keys.call(notification.data) | ||
|
||
if is_raw_notification.call(notification) | ||
RawRequest.create(notification, access_token) | ||
else | ||
ToastRequest.create(notification, access_token) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Rpush | ||
module Daemon | ||
module Wns | ||
class RawRequest | ||
def self.create(notification, access_token) | ||
body = notification.data.to_json | ||
uri = URI.parse(notification.uri) | ||
post = Net::HTTP::Post.new( | ||
uri.request_uri, | ||
"Content-Length" => body.length.to_s, | ||
"Content-Type" => "application/octet-stream", | ||
"X-WNS-Type" => "wns/raw", | ||
"X-WNS-RequestForStatus" => "true", | ||
"Authorization" => "Bearer #{access_token}" | ||
) | ||
post.body = body | ||
post | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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'] || '' | ||
end | ||
|
||
def to_xml | ||
"<toast> | ||
<visual version='1' lang='en-US'> | ||
<binding template='ToastText02'> | ||
<text id='1'>#{CleanParamString.clean(@title)}</text> | ||
<text id='2'>#{CleanParamString.clean(@body)}</text> | ||
</binding> | ||
</visual> | ||
</toast>" | ||
end | ||
end | ||
|
||
class CleanParamString | ||
def self.clean(string) | ||
string.gsub(/&/, "&").gsub(/</, "<") \ | ||
.gsub(/>/, ">").gsub(/'/, "'").gsub(/"/, """) | ||
end | ||
end | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
spec/unit/client/active_record/wns/raw_notification_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Rpush::Client::ActiveRecord::Wns::RawNotification do | ||
let(:notification) do | ||
notif = Rpush::Client::ActiveRecord::Wns::RawNotification.new | ||
notif.app = Rpush::Client::ActiveRecord::Wns::App.new(name: "aname") | ||
notif.uri = 'https://db5.notify.windows.com/?token=TOKEN' | ||
notif.data = { foo: 'foo', bar: 'bar' } | ||
notif | ||
end | ||
|
||
it 'does not allow the size of payload over 5 KB' do | ||
allow(notification).to receive(:payload_data_size) { 5121 } | ||
expect(notification.valid?).to be(false) | ||
end | ||
|
||
it 'allows exact payload of 5 KB' do | ||
allow(notification).to receive(:payload_data_size) { 5120 } | ||
expect(notification.valid?).to be(true) | ||
end | ||
|
||
it 'allows the size of payload under 5 KB' do | ||
allow(notification).to receive(:payload_data_size) { 5119 } | ||
expect(notification.valid?).to be(true) | ||
end | ||
end if active_record? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'unit_spec_helper' | ||
|
||
describe Rpush::Daemon::Wns::PostRequest do | ||
let(:app) do | ||
Rpush::Wns::App.create!( | ||
name: "MyApp", | ||
client_id: "someclient", | ||
client_secret: "somesecret", | ||
access_token: "access_token", | ||
access_token_expiration: Time.now + (60 * 10) | ||
) | ||
end | ||
|
||
context 'Notification' do | ||
let(:notification) do | ||
Rpush::Wns::Notification.create!( | ||
app: app, | ||
data: { | ||
title: "MyApp", | ||
body: "Example notification" | ||
}, | ||
uri: "http://some.example/" | ||
) | ||
end | ||
|
||
it 'creates a request characteristic for toast notification' do | ||
request = Rpush::Daemon::Wns::PostRequest.create(notification, 'token') | ||
expect(request['X-WNS-Type']).to eq('wns/toast') | ||
expect(request['Content-Type']).to eq('text/xml') | ||
expect(request.body).to include('<toast>') | ||
expect(request.body).to include('MyApp') | ||
expect(request.body).to include('Example notification') | ||
end | ||
end | ||
|
||
context 'RawNotification' do | ||
let(:notification) do | ||
Rpush::Wns::RawNotification.create!( | ||
app: app, | ||
data: { foo: 'foo', bar: 'bar' }, | ||
uri: "http://some.example/" | ||
) | ||
end | ||
|
||
it 'creates a request characteristic for raw notification' do | ||
request = Rpush::Daemon::Wns::PostRequest.create(notification, 'token') | ||
expect(request['X-WNS-Type']).to eq('wns/raw') | ||
expect(request['Content-Type']).to eq('application/octet-stream') | ||
expect(request.body).to eq("{\"foo\":\"foo\",\"bar\":\"bar\"}") | ||
end | ||
end | ||
end |