Class: BMO::APNS::Notification::Payload
- Inherits:
-
Object
- Object
- BMO::APNS::Notification::Payload
- Defined in:
- lib/bmo/apns/notification.rb
Overview
The Payload contains the data sent to Apple
Defined Under Namespace
Classes: PayloadTooLarge
Constant Summary collapse
- MAX_BYTE_SIZE =
2048
Instance Attribute Summary collapse
-
#custom_data ⇒ Object
readonly
Returns the value of attribute custom_data.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#truncable_alert ⇒ Object
readonly
Returns the value of attribute truncable_alert.
Instance Method Summary collapse
-
#initialize(data, options = {}) ⇒ Payload
constructor
A new instance of Payload.
- #package ⇒ Object
- #to_package ⇒ Object
- #truncate_alert! ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ Payload
Returns a new instance of Payload.
41 42 43 44 45 |
# File 'lib/bmo/apns/notification.rb', line 41 def initialize(data, = {}) @data = data @truncable_alert = [:truncable_alert] @options = end |
Instance Attribute Details
#custom_data ⇒ Object (readonly)
Returns the value of attribute custom_data.
39 40 41 |
# File 'lib/bmo/apns/notification.rb', line 39 def custom_data @custom_data end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
39 40 41 |
# File 'lib/bmo/apns/notification.rb', line 39 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
39 40 41 |
# File 'lib/bmo/apns/notification.rb', line 39 def @options end |
#truncable_alert ⇒ Object (readonly)
Returns the value of attribute truncable_alert.
39 40 41 |
# File 'lib/bmo/apns/notification.rb', line 39 def truncable_alert @truncable_alert end |
Instance Method Details
#package ⇒ Object
60 61 62 63 |
# File 'lib/bmo/apns/notification.rb', line 60 def package json = data.to_json [2, json.bytes.count, json].pack('cna*') end |
#to_package ⇒ Object
47 48 49 50 51 |
# File 'lib/bmo/apns/notification.rb', line 47 def to_package truncate_alert! if truncable_alert && !valid? validate! package end |
#truncate_alert! ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/bmo/apns/notification.rb', line 65 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][:alert] = Utils.bytesize_force_truncate(string, desirable_bytesize, ) end |
#valid? ⇒ Boolean
75 76 77 |
# File 'lib/bmo/apns/notification.rb', line 75 def valid? package.bytesize < MAX_BYTE_SIZE end |
#validate! ⇒ Object
53 54 55 56 57 58 |
# File 'lib/bmo/apns/notification.rb', line 53 def validate! return true if valid? str = "Payload byte size (#{package.bytesize})" \ " should be less than #{Payload::MAX_BYTE_SIZE} bytes" fail PayloadTooLarge, str end |