Class: Racoon::Notification

Inherits:
Object
  • Object
show all
Includes:
Payload
Defined in:
lib/racoon/notification.rb

Constant Summary

Constants included from Payload

Payload::PayloadInvalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Payload

#create_payload, #create_payload_from_hash

Constructor Details

#initializeNotification

Returns a new instance of Notification.



16
17
18
# File 'lib/racoon/notification.rb', line 16

def initialize
  @expiry = 0
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def badge
  @badge
end

#customObject

Returns the value of attribute custom.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def custom
  @custom
end

#device_tokenObject

Returns the value of attribute device_token.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def device_token
  @device_token
end

#expiryObject

Returns the value of attribute expiry.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def expiry
  @expiry
end

#identifierObject

Returns the value of attribute identifier.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def identifier
  @identifier
end

#soundObject

Returns the value of attribute sound.



14
15
16
# File 'lib/racoon/notification.rb', line 14

def sound
  @sound
end

Class Method Details

.create_from_packet(packet) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/racoon/notification.rb', line 83

def self.create_from_packet(packet)
  aps = packet[:notification][:aps]

  notification = Notification.new
  notification.identifier = packet[:identifier]
  notification.expiry = packet[:expiry] || 0
  notification.device_token = packet[:device_token]
  notification.badge = aps[:badge] if aps.has_key? :badge
  notification.alert = aps[:alert] if aps.has_key? :alert
  notification.sound = aps[:sound] if aps.has_key? :sound
  notification.custom = aps[:custom] if aps.has_key? :custom

  notification
end

.parse(p) ⇒ Object

Raises:

  • (RuntimeError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/racoon/notification.rb', line 55

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

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/racoon/notification.rb', line 40

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_payloadObject

Raises:



29
30
31
32
33
# File 'lib/racoon/notification.rb', line 29

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

#payloadObject



20
21
22
23
24
25
26
27
# File 'lib/racoon/notification.rb', line 20

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_bytesObject



35
36
37
38
# File 'lib/racoon/notification.rb', line 35

def to_bytes
  j = json_payload
  [1, identifier, expiry.to_i, device_token.size, device_token, j.size, j].pack("cNNna*na*")
end