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.



11
12
13
14
# File 'lib/racoon/notification.rb', line 11

def initialize
  @expiry = 0
  @send_at = Time.now
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def badge
  @badge
end

#customObject

Returns the value of attribute custom.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def custom
  @custom
end

#device_tokenObject

Returns the value of attribute device_token.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def device_token
  @device_token
end

#expiryObject

Returns the value of attribute expiry.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def expiry
  @expiry
end

#identifierObject

Returns the value of attribute identifier.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def identifier
  @identifier
end

#send_atObject

Returns the value of attribute send_at.



9
10
11
# File 'lib/racoon/notification.rb', line 9

def send_at
  @send_at
end

#soundObject

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

Raises:

  • (RuntimeError)


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

Returns:

  • (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_payloadObject

Raises:



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

#payloadObject



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_bytesObject



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