Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/core.rb
Constant Summary collapse
- MAX_PAYLOAD_LEN =
256
Instance Method Summary collapse
-
#apn_payload_length ⇒ Fixnum?
Invokes #to_payload and returns it’s length.
-
#to_apn_payload ⇒ String?
Converts hash into JSON String.
Instance Method Details
#apn_payload_length ⇒ Fixnum?
Invokes #to_payload and returns it’s length
35 36 37 38 |
# File 'lib/core.rb', line 35 def apn_payload_length p = to_apn_payload p ? p.length : nil end |
#to_apn_payload ⇒ String?
Converts hash into JSON String. When payload is too long but can be chopped, tries to cut self.[:alert]. If payload still don’t fit Apple’s restrictions, returns nil
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/core.rb', line 15 def to_apn_payload # Payload too long if (to_json.length > MAX_PAYLOAD_LEN) alert = self[:aps][:alert] self[:aps][:alert] = '' # can be chopped? if (to_json.length > MAX_PAYLOAD_LEN) return nil else # inefficient way, but payload may be full of unicode-escaped chars, so... self[:aps][:alert] = alert while (self.to_json.length > MAX_PAYLOAD_LEN) self[:aps][:alert].chop! end end end to_json end |