Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Truncation returns nil when trying to truncate a non String value. Te…

…sts updated
  • Loading branch information...
commit 352ed470706e148a9a29e0cb78684bdf7dd1d5eb 1 parent 0de7aa7
@Orion98MC authored
Showing with 16 additions and 0 deletions.
  1. +2 −0  lib/apns/payload.rb
  2. +14 −0 spec/apns/payload_spec.rb
View
2  lib/apns/payload.rb
@@ -67,6 +67,7 @@ def apn_message
# Returns a new payload with the alert truncated to fit in the payload size requirement (PAYLOAD_MAX_SIZE)
# Rem: It's a best effort since the alert may not be the one string responsible for the oversized payload
+ # also, if your alert is a Hash containing loc-* keys it won't work, in this case you should use the #payload_with_truncated_string_at_keypath
def payload_with_truncated_alert
payload_with_truncated_string_at_keypath([:alert])
end
@@ -78,6 +79,7 @@ def payload_with_truncated_alert
# Rem: Truncation only works on String values...
def payload_with_truncated_string_at_keypath(array_or_dotted_string)
return self if valid? # You can safely call it on a valid payload
+ return unless message.at_key_path(array_or_dotted_string).is_a?(String)
# Rem: I'm using Marshall to make a deep copy of the message hash. Of course this would only work with "standard" values like Hash/String/Array
payload_with_empty_string = APNS::Payload.new(device.token, Marshal.load(Marshal.dump(message)).at_key_path(array_or_dotted_string){|obj, key| obj[key] = ""})
View
14 spec/apns/payload_spec.rb
@@ -72,6 +72,20 @@ def payload(message, token = "a-device-token")
pn.message[:custom].should == p.message[:custom]
end
+ it "should not change the alert when truncating alert and alert is not a String" do
+ p = payload({
+ :alert => {'foo' => 'bar'*80, 'baz' => 'blah'},
+ :badge => 2,
+ :sound => "sound.aiff",
+ :custom => "my custom data"
+ })
+ p.should_not be_valid
+
+ pn = p.payload_with_truncated_alert
+ pn.should be_nil
+ end
+
+
it "should allow truncating a custom field" do
p = payload({
:alert => "my alert",
Please sign in to comment.
Something went wrong with that request. Please try again.