class Payload
Public Class Methods
new(notification_hash, max_bytes)
click to toggle source
# File lib/apn/payload.rb, line 3 def initialize(notification_hash, max_bytes) @notification_hash = notification_hash @max_bytes = max_bytes end
Public Instance Methods
package()
click to toggle source
# File lib/apn/payload.rb, line 8 def package str = encode(@notification_hash) if APN.truncate_alert && str.bytesize > @max_bytes max_bytesize = @max_bytes - (str.bytesize - alert.bytesize) if max_bytesize <= 0 escaped_max_bytesize = @max_bytes - (str.bytesize - encode(alert).bytesize) raise "Even truncating the alert won't be enough to have a #{@max_bytes} message" if escaped_max_bytesize <= 0 truncate_escaped!(escaped_max_bytesize) else truncate_alert!(max_bytesize) end str = encode(@notification_hash) end str end
Private Instance Methods
alert()
click to toggle source
# File lib/apn/payload.rb, line 28 def alert @alert ||= if hash_alert? @notification_hash['aps']['alert']['loc-args'][0] else @notification_hash['aps']['alert'] end end
alert=(value)
click to toggle source
# File lib/apn/payload.rb, line 37 def alert=(value) if hash_alert? @notification_hash['aps']['alert']['loc-args'][0] = value else @notification_hash['aps']['alert'] = value end end
encode(obj)
click to toggle source
# File lib/apn/payload.rb, line 63 def encode(obj) ActiveSupport::JSON.encode(obj) end
encoded_size(str)
click to toggle source
# File lib/apn/payload.rb, line 67 def encoded_size(str) encode(str).bytesize - 2 end
hash_alert?()
click to toggle source
# File lib/apn/payload.rb, line 45 def hash_alert? @hash_alert ||= @notification_hash['aps']['alert'].is_a?(Hash) end
truncate_alert!(max_size)
click to toggle source
# File lib/apn/payload.rb, line 49 def truncate_alert!(max_size) self.alert = alert.mb_chars.limit(max_size).to_s end
truncate_escaped!(max_size)
click to toggle source
# File lib/apn/payload.rb, line 53 def truncate_escaped!(max_size) self.alert = alert.each_char.each_with_object('') do |char, result| if encoded_size(result) + encoded_size(char) > max_size break result else result << char end end end