Class: Racoon::Notification
- Inherits:
-
Object
- Object
- Racoon::Notification
- Includes:
- Payload
- Defined in:
- lib/racoon/notification.rb
Constant Summary
Constants included from Payload
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#custom ⇒ Object
Returns the value of attribute custom.
-
#device_token ⇒ Object
Returns the value of attribute device_token.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#send_at ⇒ Object
Returns the value of attribute send_at.
-
#sound ⇒ Object
Returns the value of attribute sound.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Notification
constructor
A new instance of Notification.
- #json_payload ⇒ Object
- #payload ⇒ Object
- #to_bytes ⇒ Object
Methods included from Payload
#create_payload, #create_payload_from_hash
Constructor Details
#initialize ⇒ Notification
Returns a new instance of Notification.
11 12 13 14 |
# File 'lib/racoon/notification.rb', line 11 def initialize @expiry = 0 @send_at = Time.now end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def badge @badge end |
#custom ⇒ Object
Returns the value of attribute custom.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def custom @custom end |
#device_token ⇒ Object
Returns the value of attribute device_token.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def device_token @device_token end |
#expiry ⇒ Object
Returns the value of attribute expiry.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def expiry @expiry end |
#identifier ⇒ Object
Returns the value of attribute identifier.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def identifier @identifier end |
#send_at ⇒ Object
Returns the value of attribute send_at.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def send_at @send_at end |
#sound ⇒ Object
Returns the value of attribute sound.
9 10 11 |
# File 'lib/racoon/notification.rb', line 9 def sound @sound end |
Class Method Details
.parse(p) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/racoon/notification.rb', line 51 def self.parse(p) buffer = p.dup notification = Notification.new header = buffer.slice!(0, 11).unpack("cNNn") raise RuntimeError.new("Header of notification is invalid: #{header.inspect}") if header[0] != 1 # identifier notification.identifier = header[1] notification.expiry = header[2] # device token notification.device_token = buffer.slice!(0, 32).unpack("a*").first # JSON payload payload_len = buffer.slice!(0, 2).unpack("n") j = buffer.slice!(0, payload_len.last) result = Yajl::Parser.parse(j) ['alert', 'badge', 'sound'].each do |k| notification.send("#{k}=", result['aps'][k]) if result['aps'] && result['aps'][k] end result.delete("aps") notification.custom = result notification end |
.valid?(p) ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/racoon/notification.rb', line 36 def self.valid?(p) begin Notification.parse(p) rescue PayloadInvalid => p Config.logger.error "PayloadInvalid: #{p}" false rescue RuntimeError => r Config.logger.error "Runtime error: #{r}" false rescue Exception => e Config.logger.error "Unknown error: #{e}" false end end |
Instance Method Details
#json_payload ⇒ Object
25 26 27 28 29 |
# File 'lib/racoon/notification.rb', line 25 def json_payload j = Yajl::Encoder.encode(payload) raise PayloadInvalid.new("The payload is larger than allowed: #{j.length}") if j.size > 256 j end |
#payload ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/racoon/notification.rb', line 16 def payload p = Hash.new [:badge, :alert, :sound, :custom].each do |k| r = send(k) p[k] = r if r end create_payload(p) end |
#to_bytes ⇒ Object
31 32 33 34 |
# File 'lib/racoon/notification.rb', line 31 def to_bytes j = json_payload [1, identifier, expiry.to_i, device_token.size, device_token, j.size, j].pack("cNNna*na*") end |