Skip to content

Commit

Permalink
[BREAKING] truncable_field options is now truncable_alert
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinelyset committed Mar 8, 2016
1 parent f60915d commit 003568e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/bmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module BMO
#
# @param device_token [String]
# @param data [Hash] The data you want to send
# @option options :truncable_field If the payload is too large
# this field will be truncated, it must be in an aps hash
# @option options :truncable_alert If the payload is too large
# this alert field will be truncated
# @option options :omission ('...') The omission in truncate
# @option options :separator The separator use in truncation
#
Expand Down
17 changes: 9 additions & 8 deletions lib/bmo/apns/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ class PayloadTooLarge < Exception; end
# Define ==
include Equalizer.new(:data)

attr_reader :data, :truncable_field, :options
attr_reader :data, :truncable_alert, :custom_data, :options

def initialize(data, options = {})
@data = data
@truncable_field = options[:truncable_field]
@truncable_alert = options[:truncable_alert]
@options = options
end

def to_package
truncate_field! if truncable_field && !valid?
truncate_alert! if truncable_alert && !valid?
validate!
package
end
Expand All @@ -62,13 +62,14 @@ def package
[2, json.bytes.count, json].pack('cna*')
end

def truncate_field!
return unless data[:aps] && data[:aps][truncable_field]
string = data[:aps][truncable_field]
def truncate_alert!
return unless data[:aps] && data[:aps][:alert]
string = data[:aps][:alert]
diff_bytesize = package.bytesize - MAX_BYTE_SIZE
desirable_bytesize = (string.bytesize - diff_bytesize) - 1
data[:aps][truncable_field] = Utils
.bytesize_force_truncate(string, desirable_bytesize, options)
data[:aps][:alert] = Utils.bytesize_force_truncate(string,
desirable_bytesize,
options)
end

def valid?
Expand Down
41 changes: 24 additions & 17 deletions spec/bmo/apns/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
describe BMO::APNS::Notification::Payload do
let(:payload_max_size) { BMO::APNS::Notification::Payload::MAX_BYTE_SIZE }

describe 'custom_data field' do
it 'truncates if there is a truncable field and this is not valid' do
payload = described_class.new(aps: { alert: 'a' }, custom_data: { hello: 'world' })
expect(payload.to_package).to eq("\x02\x005{\"aps\":{\"alert\":\"a\"},\"custom_data\":{\"hello\":\"world\"}}")
end
end

describe '#validate!' do
it 'returns true if the payload is valid' do
payload = described_class.new(aps: 'a' * 200)
Expand All @@ -45,39 +52,39 @@

describe '#to_package' do
it 'truncates if there is a truncable field and this is not valid' do
options = { truncable_field: :message }
payload = described_class.new({ aps: { message: 'a' * payload_max_size } }, options)
options = { truncable_alert: true }
payload = described_class.new({ aps: { alert: 'a' * payload_max_size } }, options)
# I'm not able to write the hex literal for the beginning
expect(payload.to_package).to end_with("{\"aps\":{\"message\":\"#{'a' * (payload_max_size - 29)}...\"}}")
expect(payload.to_package).to end_with("{\"aps\":{\"alert\":\"#{'a' * (payload_max_size - 27)}...\"}}")
end

it 'truncates to respect the MAX_BYTE_SIZE' do
options = { truncable_field: :message }
payload = described_class.new({ aps: { message: 'a' * payload_max_size } }, options)
options = { truncable_alert: true }
payload = described_class.new({ aps: { alert: 'a' * payload_max_size } }, options)
expect(payload.to_package.bytesize).to be < BMO::APNS::Notification::Payload::MAX_BYTE_SIZE
end
end

describe '#truncable_field!' do
it 'truncates the corresponding field in aps' do
options = { truncable_field: :message }
payload = described_class.new({ aps: { message: 'a' * payload_max_size } }, options)
payload.truncate_field!
expect(payload.data[:aps][:message]).to eq(('a' * (payload_max_size - 29)) + '...')
options = { truncable_alert: true }
payload = described_class.new({ aps: { alert: 'a' * payload_max_size } }, options)
payload.truncate_alert!
expect(payload.data[:aps][:alert]).to eq(('a' * (payload_max_size - 27)) + '...')
end

it 'truncates with omission' do
options = { truncable_field: :message, omission: '[more]' }
payload = described_class.new({ aps: { message: 'a' * payload_max_size } }, options)
payload.truncate_field!
expect(payload.data[:aps][:message]).to eq(('a' * (payload_max_size - 32)) + '[more]')
options = { truncable_field: :true, omission: '[more]' }
payload = described_class.new({ aps: { alert: 'a' * payload_max_size } }, options)
payload.truncate_alert!
expect(payload.data[:aps][:alert]).to eq(('a' * (payload_max_size - 30)) + '[more]')
end

it 'truncates with separator' do
options = { truncable_field: :message, separator: ' ' }
payload = described_class.new({ aps: { message: 'test ' + ('a' * (payload_max_size - 1)) } }, options)
payload.truncate_field!
expect(payload.data[:aps][:message]).to eq('test...')
options = { truncable_field: true, separator: ' ' }
payload = described_class.new({ aps: { alert: 'test ' + ('a' * (payload_max_size - 1)) } }, options)
payload.truncate_alert!
expect(payload.data[:aps][:alert]).to eq('test...')
end
end
end

0 comments on commit 003568e

Please sign in to comment.