Permalink
Browse files

Added tests for message_for_sending and truncated alerts over 150

  • Loading branch information...
1 parent 9813635 commit a740b4568efee98317b369dcb07f98210728fe72 @markbates markbates committed Jul 23, 2009
@@ -1,8 +1,16 @@
class APN::Notification < ActiveRecord::Base
+ include ::ActionView::Helpers::TextHelper
set_table_name 'apn_notifications'
belongs_to :device, :class_name => 'APN::Device'
+ def alert=(message)
+ if !message.blank? && message.size > 150
+ message = truncate(message, :length => 150)
+ end
+ write_attribute('alert', message)
+ end
+
# Creates a Hash that will be the payload of an APN.
#
# Example:
@@ -35,9 +43,9 @@ def to_apple_json
self.apple_hash.to_json
end
- def apn_message_for_sending
+ def message_for_sending
json = self.to_apple_json
- message = "\0\0 #{self.device_token_hexa}\0#{json.length.chr}#{json}"
+ message = "\0\0 #{self.device.to_hexa}\0#{json.length.chr}#{json}"
raise APN::Errors::ExceededMessageSizeError.new(message) if message.size.to_i > 256
message
end
@@ -2,6 +2,16 @@
describe APN::Notification do
+ describe 'alert' do
+
+ it 'should trim the message to 150 characters' do
+ noty = APN::Notification.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
@@ -28,4 +38,22 @@
end
+ describe 'message_for_sending' do
+
+ it 'should create a binary message to be sent to Apple' do
+ noty = APN::Notification.first
+ noty.device = DeviceFactory.new(:token => '5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz')
+ noty.message_for_sending.should == fixture_value('message_for_sending.bin')
+ end
+
+ it 'should raise an APN::Errors::ExceededMessageSizeError if the message is too big' do
+ noty = NotificationFactory.new(:device_id => DeviceFactory.create, :sound => true, :badge => nil)
+ noty.send(:write_attribute, 'alert', 'a' * 183)
+ lambda {
+ noty.message_for_sending
+ }.should raise_error(APN::Errors::ExceededMessageSizeError)
+ end
+
+ end
+
end
Binary file not shown.
View
@@ -1,5 +1,6 @@
require 'rubygems'
require 'spec'
+require 'action_view'
Dir.glob(File.join(File.dirname(__FILE__), 'extensions', '*.rb')).sort.each do |f|
require f

0 comments on commit a740b45

Please sign in to comment.