Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…sts updated
  • Loading branch information...
commit 352ed470706e148a9a29e0cb78684bdf7dd1d5eb 1 parent 0de7aa7
Thierry Passeron authored

Showing 2 changed files with 16 additions and 0 deletions. Show diff stats Hide diff stats

  1. +2 0  lib/apns/payload.rb
  2. +14 0 spec/apns/payload_spec.rb
2  lib/apns/payload.rb
@@ -67,6 +67,7 @@ def apn_message
67 67
68 68 # Returns a new payload with the alert truncated to fit in the payload size requirement (PAYLOAD_MAX_SIZE)
69 69 # Rem: It's a best effort since the alert may not be the one string responsible for the oversized payload
  70 + # 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
70 71 def payload_with_truncated_alert
71 72 payload_with_truncated_string_at_keypath([:alert])
72 73 end
@@ -78,6 +79,7 @@ def payload_with_truncated_alert
78 79 # Rem: Truncation only works on String values...
79 80 def payload_with_truncated_string_at_keypath(array_or_dotted_string)
80 81 return self if valid? # You can safely call it on a valid payload
  82 + return unless message.at_key_path(array_or_dotted_string).is_a?(String)
81 83
82 84 # 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
83 85 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] = ""})
14 spec/apns/payload_spec.rb
@@ -72,6 +72,20 @@ def payload(message, token = "a-device-token")
72 72 pn.message[:custom].should == p.message[:custom]
73 73 end
74 74
  75 + it "should not change the alert when truncating alert and alert is not a String" do
  76 + p = payload({
  77 + :alert => {'foo' => 'bar'*80, 'baz' => 'blah'},
  78 + :badge => 2,
  79 + :sound => "sound.aiff",
  80 + :custom => "my custom data"
  81 + })
  82 + p.should_not be_valid
  83 +
  84 + pn = p.payload_with_truncated_alert
  85 + pn.should be_nil
  86 + end
  87 +
  88 +
75 89 it "should allow truncating a custom field" do
76 90 p = payload({
77 91 :alert => "my alert",

0 comments on commit 352ed47

Please sign in to comment.
Something went wrong with that request. Please try again.