forked from PRX/apn_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on tests, renaming in app notifications to pull notifications
- Loading branch information
Showing
13 changed files
with
233 additions
and
13 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
generators/templates/apn_migrations/009_create_pull_notifications.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,16 @@ | ||
class CreatePullNotifications < ActiveRecord::Migration | ||
def self.up | ||
create_table :apn_pull_notifications do |t| | ||
t.integer :app_id | ||
t.string :title | ||
t.string :content | ||
t.string :link | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :apn_pull_notifications | ||
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
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,15 @@ | ||
class APN::PullNotification < APN::Base | ||
belongs_to :app, :class_name => 'APN::App' | ||
|
||
validates_presence_of :app_id | ||
|
||
def self.latest_since(app_id, since_date=nil) | ||
conditions = if since_date | ||
["app_id = ? AND created_at > ?", app_id, since_date] | ||
else | ||
["app_id = ?", app_id] | ||
end | ||
|
||
first(:order => "created_at DESC", :conditions => conditions) | ||
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
66 changes: 66 additions & 0 deletions
66
spec/apn_on_rails/app/models/apn/group_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,66 @@ | ||
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper.rb') | ||
|
||
describe APN::GroupNotification do | ||
|
||
describe 'alert' do | ||
|
||
it 'should trim the message to 150 characters' do | ||
noty = APN::GroupNotification.new | ||
noty.alert = 'a' * 200 | ||
noty.alert.should == ('a' * 147) + '...' | ||
end | ||
|
||
end | ||
|
||
describe 'apple_hash' do | ||
|
||
it 'should return a hash of the appropriate params for Apple' do | ||
noty = APN::GroupNotification.first | ||
noty.apple_hash.should == {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"},"typ" => "1"} | ||
noty.custom_properties = nil | ||
noty.apple_hash.should == {"aps" => {"badge" => 5, "sound" => "my_sound.aiff", "alert" => "Hello!"}} | ||
noty.badge = nil | ||
noty.apple_hash.should == {"aps" => {"sound" => "my_sound.aiff", "alert" => "Hello!"}} | ||
noty.alert = nil | ||
noty.apple_hash.should == {"aps" => {"sound" => "my_sound.aiff"}} | ||
noty.sound = nil | ||
noty.apple_hash.should == {"aps" => {}} | ||
noty.sound = true | ||
noty.apple_hash.should == {"aps" => {"sound" => "1.aiff"}} | ||
end | ||
|
||
end | ||
|
||
describe 'to_apple_json' do | ||
|
||
it 'should return the necessary JSON for Apple' do | ||
noty = APN::GroupNotification.first | ||
noty.to_apple_json.should == %{{"typ":"1","aps":{"badge":5,"sound":"my_sound.aiff","alert":"Hello!"}}} | ||
end | ||
|
||
end | ||
|
||
describe 'message_for_sending' do | ||
|
||
it 'should create a binary message to be sent to Apple' do | ||
noty = APN::GroupNotification.first | ||
noty.custom_properties = nil | ||
device = DeviceFactory.new(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz') | ||
noty.message_for_sending(device).should == fixture_value('message_for_sending.bin') | ||
end | ||
|
||
it 'should raise an APN::Errors::ExceededMessageSizeError if the message is too big' do | ||
app = AppFactory.create | ||
device = DeviceFactory.create({:app_id => app.id}) | ||
group = GroupFactory.create({:app_id => app.id}) | ||
device_grouping = DeviceGroupingFactory.create({:group_id => group.id,:device_id => device.id}) | ||
noty = GroupNotificationFactory.new(:group_id => group.id, :sound => true, :badge => nil) | ||
noty.send(:write_attribute, 'alert', 'a' * 183) | ||
lambda { | ||
noty.message_for_sending(device) | ||
}.should raise_error(APN::Errors::ExceededMessageSizeError) | ||
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
37 changes: 37 additions & 0 deletions
37
spec/apn_on_rails/app/models/apn/pull_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,37 @@ | ||
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper.rb') | ||
|
||
describe APN::PullNotification do | ||
|
||
describe 'latest_since_when_already_seen_latest' do | ||
|
||
it 'should return nothing because since date is after the latest pull notification' do | ||
app = APN::App.first | ||
noty1 = PullNotificationFactory.create({:app_id => app.id}) | ||
noty1.created_at = Time.now - 1.week | ||
noty1.save | ||
APN::PullNotification.latest_since(app.id,Time.now).should == nil | ||
end | ||
|
||
end | ||
|
||
describe 'latest_since_when_have_not_seen_latest' do | ||
|
||
it 'should return the most recent pull notification because it has not yet been seen' do | ||
app = APN::App.first | ||
noty1 = PullNotificationFactory.create({:app_id => app.id}) | ||
noty1.created_at = Time.now + 1.week | ||
noty1.save | ||
APN::PullNotification.latest_since(app.id,Time.now - 1.week).should == noty1 | ||
end | ||
|
||
end | ||
|
||
describe 'latest_since_with_no_date' do | ||
it 'should return the most recent pull notification because no date is given' do | ||
app = APN::App.first | ||
noty1 = APN::PullNotification.find(:first, :order => "created_at DESC") | ||
APN::PullNotification.latest_since(app.id).should == noty1 | ||
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
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 PullNotificationFactory | ||
|
||
class << self | ||
|
||
def new(options = {}) | ||
app = APN::App.first | ||
options = {:app_id => app.id, :title => 'Pull Notification Title', | ||
:content => 'blah blah blah', :link => 'http://www.prx.org'}.merge(options) | ||
return APN::PullNotification.new(options) | ||
end | ||
|
||
def create(options = {}) | ||
noty = PullNotificationFactory.new(options) | ||
noty.save | ||
return noty | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
PullNotificationFactory.create |