Class: BMO::APNS::Notification::Payload

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

Overview

The Payload contains the data sent to Apple

Defined Under Namespace

Classes: PayloadTooLarge

Constant Summary collapse

MAX_BYTE_SIZE =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Payload

Returns a new instance of Payload.



41
42
43
44
45
# File 'lib/bmo/apns/notification.rb', line 41

def initialize(data, options = {})
  @data            = data
  @truncable_alert = options[:truncable_alert]
  @options         = options
end

Instance Attribute Details

#custom_dataObject (readonly)

Returns the value of attribute custom_data.



39
40
41
# File 'lib/bmo/apns/notification.rb', line 39

def custom_data
  @custom_data
end

#dataObject (readonly)

Returns the value of attribute data.



39
40
41
# File 'lib/bmo/apns/notification.rb', line 39

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



39
40
41
# File 'lib/bmo/apns/notification.rb', line 39

def options
  @options
end

#truncable_alertObject (readonly)

Returns the value of attribute truncable_alert.



39
40
41
# File 'lib/bmo/apns/notification.rb', line 39

def truncable_alert
  @truncable_alert
end

Instance Method Details

#packageObject



60
61
62
63
# File 'lib/bmo/apns/notification.rb', line 60

def package
  json = data.to_json
  [2, json.bytes.count, json].pack('cna*')
end

#to_packageObject



47
48
49
50
51
# File 'lib/bmo/apns/notification.rb', line 47

def to_package
  truncate_alert! if truncable_alert && !valid?
  validate!
  package
end

#truncate_alert!Object



65
66
67
68
69
70
71
72
73
# File 'lib/bmo/apns/notification.rb', line 65

def truncate_alert!
  return unless data[:aps] && data[:aps][:alert]
  string                = data[:aps][:alert]
  diff_bytesize         = package.bytesize - MAX_BYTE_SIZE
  desirable_bytesize    = (string.bytesize - diff_bytesize) - 1
  data[:aps][:alert] = Utils.bytesize_force_truncate(string,
                                                     desirable_bytesize,
                                                     options)
end

#valid?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/bmo/apns/notification.rb', line 75

def valid?
  package.bytesize < MAX_BYTE_SIZE
end

#validate!Object



53
54
55
56
57
58
# File 'lib/bmo/apns/notification.rb', line 53

def validate!
  return true if valid?
  str = "Payload byte size (#{package.bytesize})" \
    " should be less than #{Payload::MAX_BYTE_SIZE} bytes"
  fail PayloadTooLarge, str
end