Class: EventMachine::ApnManager::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/em_apn_manager/notification.rb

Defined Under Namespace

Classes: PayloadTooLarge

Constant Summary collapse

DATA_MAX_BYTES =
256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, aps = {}, custom = {}, options = {}) ⇒ Notification

Returns a new instance of Notification.



13
14
15
16
17
18
19
20
21
22
# File 'lib/em_apn_manager/notification.rb', line 13

def initialize(token, aps = {}, custom = {}, options = {})
  raise "Bad push token: #{token}" if token.nil? || (token.length != 64)

  @token  = token
  @aps    = aps
  @custom = custom

  self.identifier = options[:identifier] if options[:identifier]
  self.expiry = options[:expiry] if options[:expiry]
end

Instance Attribute Details

#expiryObject

Returns the value of attribute expiry.



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

def expiry
  @expiry
end

#identifierObject

Returns the value of attribute identifier.



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

def identifier
  @identifier
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/em_apn_manager/notification.rb', line 10

def token
  @token
end

Instance Method Details

#dataObject



30
31
32
33
34
35
36
# File 'lib/em_apn_manager/notification.rb', line 30

def data
  identifier = @identifier || 0
  expiry     = @expiry || 0
  size = [payload].pack("a*").size
  data_array = [1, identifier, expiry, 32, token, size, payload]
  data_array.pack("cNNnH*na*")
end

#payloadObject



24
25
26
# File 'lib/em_apn_manager/notification.rb', line 24

def payload
  Yajl::Encoder.encode(@custom.merge(:aps => @aps))
end

#truncate_alert!Object



51
52
53
54
55
# File 'lib/em_apn_manager/notification.rb', line 51

def truncate_alert!
  while data.size > DATA_MAX_BYTES && !@aps["alert"].nil? && @aps["alert"].size > 0
    @aps["alert"] = @aps["alert"][0..-2]
  end
end

#validate!Object



38
39
40
41
42
43
44
45
# File 'lib/em_apn_manager/notification.rb', line 38

def validate!
  if data.size > DATA_MAX_BYTES
    error = "max is #{DATA_MAX_BYTES} bytes, but got #{data.size}: #{payload.inspect}"
    raise PayloadTooLarge.new(error)
  else
    true
  end
end