Permalink
Browse files

Merge branch 'master' into backends

Conflicts:
	lib/rapns.rb
  • Loading branch information...
2 parents ad0bdb0 + 8c69166 commit 2e39804dc042978bf054399242759df7c0d92f39 @ileitch committed Jan 26, 2013
View
4 lib/generators/templates/rapns_add_gcm_support.rb
@@ -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
View
3 lib/rapns.rb
@@ -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'
View
10 lib/rapns/apns/notification.rb
@@ -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
@@ -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
View
2 lib/rapns/app.rb
@@ -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
View
7 lib/rapns/gcm/notification.rb
@@ -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))
@@ -25,6 +26,10 @@ def as_json
json
end
+
+ def payload_data_size
+ multi_json_dump(as_json['data']).bytesize
+ end
end
end
end
View
13 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
View
13 lib/rapns/gcm/payload_size_validator.rb
@@ -1,13 +0,0 @@
-module Rapns
- module Gcm
- class PayloadSizeValidator < ActiveModel::Validator
- LIMIT = 4096
-
- def validate(record)
- if record.payload_size > LIMIT
- record.errors[:base] << "GCM notification payload cannot be larger than #{LIMIT} bytes."
- end
- end
- end
- end
-end
View
13 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
View
17 spec/unit/apns/notification_spec.rb
@@ -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
View
10 spec/unit/gcm/notification_spec.rb
@@ -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
@@ -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

0 comments on commit 2e39804

Please sign in to comment.