Skip to content
This repository has been archived by the owner on Mar 9, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' into backends
Browse files Browse the repository at this point in the history
Conflicts:
	lib/rapns.rb
  • Loading branch information
ileitch committed Jan 26, 2013
2 parents ad0bdb0 + 8c69166 commit 2e39804
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 22 deletions.
4 changes: 3 additions & 1 deletion lib/generators/templates/rapns_add_gcm_support.rb
Expand Up @@ -34,7 +34,9 @@ def self.up

add_column :rapns_notifications, :collapse_key, :string, :null => true
add_column :rapns_notifications, :delay_while_idle, :boolean, :null => false, :default => false
add_column :rapns_notifications, :registration_ids, :text, :null => true

reg_ids_type = ActiveRecord::Base.connection.adapter_name.include?('Mysql') ? :mediumtext : :text
add_column :rapns_notifications, :registration_ids, reg_ids_type, :null => true
add_column :rapns_notifications, :app_id, :integer, :null => true
add_column :rapns_notifications, :retries, :integer, :null => true, :default => 0

Expand Down
3 changes: 2 additions & 1 deletion lib/rapns.rb
Expand Up @@ -36,7 +36,8 @@ def self.require_for_daemon
require 'rapns/apns/required_fields_validator'

require 'rapns/gcm/expiry_collapse_key_mutual_inclusion_validator'
require 'rapns/gcm/payload_size_validator'
require 'rapns/gcm/payload_data_size_validator'
require 'rapns/gcm/registration_ids_count_validator'

if defined? ActiveRecord
require 'rapns/notification'
Expand Down
10 changes: 8 additions & 2 deletions lib/rapns/apns/notification.rb
Expand Up @@ -41,13 +41,13 @@ def alert

MDM_KEY = '__rapns_mdm__'
def mdm=(magic)
self.attributes_for_device = { MDM_KEY => magic }
self.attributes_for_device = (attributes_for_device || {}).merge({ MDM_KEY => magic })
end

CONTENT_AVAILABLE_KEY = '__rapns_content_available__'
def content_available=(bool)
return unless bool
self.attributes_for_device = { CONTENT_AVAILABLE_KEY => true }
self.attributes_for_device = (attributes_for_device || {}).merge({ CONTENT_AVAILABLE_KEY => true })
end

def as_json
Expand Down Expand Up @@ -79,6 +79,12 @@ def to_binary(options = {})
[1, id_for_pack, expiry, 0, 32, device_token, payload_size, payload].pack("cNNccH*na*")
end

def data=(attrs)
return unless attrs
raise ArgumentError, "must be a Hash" if !attrs.is_a?(Hash)
super attrs.merge(data || {})
end

end
end
end
2 changes: 1 addition & 1 deletion lib/rapns/app.rb
Expand Up @@ -4,7 +4,7 @@ class App < ActiveRecord::Base

attr_accessible :name, :environment, :certificate, :password, :connections, :auth_key

has_many :notifications
has_many :notifications, :class_name => 'Rapns::Notification'

validates :name, :presence => true, :uniqueness => { :scope => [:type, :environment] }
validates_numericality_of :connections, :greater_than => 0, :only_integer => true
Expand Down
7 changes: 6 additions & 1 deletion lib/rapns/gcm/notification.rb
Expand Up @@ -3,7 +3,8 @@ module Gcm
class Notification < Rapns::Notification
validates :registration_ids, :presence => true
validates_with Rapns::Gcm::ExpiryCollapseKeyMutualInclusionValidator
validates_with Rapns::Gcm::PayloadSizeValidator
validates_with Rapns::Gcm::PayloadDataSizeValidator
validates_with Rapns::Gcm::RegistrationIdsCountValidator

def registration_ids=(ids)
super(Array(ids))
Expand All @@ -25,6 +26,10 @@ def as_json

json
end

def payload_data_size
multi_json_dump(as_json['data']).bytesize
end
end
end
end
13 changes: 13 additions & 0 deletions lib/rapns/gcm/payload_data_size_validator.rb
@@ -0,0 +1,13 @@
module Rapns
module Gcm
class PayloadDataSizeValidator < ActiveModel::Validator
LIMIT = 4096

def validate(record)
if record.payload_data_size > LIMIT
record.errors[:base] << "GCM notification payload data cannot be larger than #{LIMIT} bytes."
end
end
end
end
end
13 changes: 0 additions & 13 deletions lib/rapns/gcm/payload_size_validator.rb

This file was deleted.

13 changes: 13 additions & 0 deletions lib/rapns/gcm/registration_ids_count_validator.rb
@@ -0,0 +1,13 @@
module Rapns
module Gcm
class RegistrationIdsCountValidator < ActiveModel::Validator
LIMIT = 1000

def validate(record)
if record.registration_ids && record.registration_ids.size > LIMIT
record.errors[:base] << "GCM notification num of registration_ids cannot be larger than #{LIMIT}."
end
end
end
end
end
17 changes: 16 additions & 1 deletion spec/unit/apns/notification_spec.rb
Expand Up @@ -128,10 +128,25 @@
notification.as_json['aps'].key?('content-available').should be_false
end

it 'does not include convert-available as a non-aps attribute' do
it 'does not include content-available as a non-aps attribute' do
notification.content_available = true
notification.as_json.key?('content-available').should be_false
end

it 'does not overwrite existing attributes for the device' do
notification.data = {:hi => :mom}
notification.content_available = true
notification.as_json['aps']['content-available'].should == 1
notification.as_json['hi'].should == 'mom'
end

it 'does not overwrite the content-available flag when setting attributes for the device' do
notification.content_available = true
notification.data = {:hi => :mom}
notification.as_json['aps']['content-available'].should == 1
notification.as_json['hi'].should == 'mom'
end

end

describe Rapns::Apns::Notification, "to_binary" do
Expand Down
10 changes: 8 additions & 2 deletions spec/unit/gcm/notification_spec.rb
Expand Up @@ -12,10 +12,10 @@

it { should validate_presence_of :registration_ids }

it 'has a payload limit of 4096 bytes' do
it "has a 'data' payload limit of 4096 bytes" do
notification.data = { :key => "a" * 4096 }
notification.valid?.should be_false
notification.errors[:base].should == ["GCM notification payload cannot be larger than 4096 bytes."]
notification.errors[:base].should == ["GCM notification payload data cannot be larger than 4096 bytes."]
end

it 'allows assignment of many registration IDs' do
Expand All @@ -25,6 +25,12 @@
reloaded_notification = notification_class.find(notification.id)
reloaded_notification.registration_ids.should == ['a', 'b']
end

it 'num of registration Ids limit of 1000' do
notification.registration_ids = ['a']*(1000+1)
notification.valid?.should be_false
notification.errors[:base].should == ["GCM notification num of registration_ids cannot be larger than 1000."]
end

it 'allows assignment of a single registration ID' do
notification.app = app
Expand Down

0 comments on commit 2e39804

Please sign in to comment.