Class: PushNotifier::APNS::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



7
8
9
10
11
12
13
# File 'lib/push_notifier/apns/notification.rb', line 7

def initialize(token, message, options={})
  default_options
  dynamic_accessor options
  @token = clean_token token
  @alert = message
  @custom_data = options.reject { |x,y| allowed_attrs.include? x }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/push_notifier/apns/notification.rb', line 6

def options
  @options
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/push_notifier/apns/notification.rb', line 6

def token
  @token
end

Instance Method Details

#allowed_attrsObject



33
34
35
# File 'lib/push_notifier/apns/notification.rb', line 33

def allowed_attrs
  [:alert, :badge, :category, :content_available, :sound, :custom_data, :priority]
end

#clean_token(token) ⇒ Object



19
20
21
# File 'lib/push_notifier/apns/notification.rb', line 19

def clean_token(token)
  [token.gsub!(/[<|>|\s]/,'')].pack('H*')
end

#data_messageObject



23
24
25
26
27
28
29
30
31
# File 'lib/push_notifier/apns/notification.rb', line 23

def data_message
  aps = allowed_attrs.inject({}) do |acc,x|
    acc[x.to_s] = self.send(x) unless self.send(x).nil?
    acc
  end
  custom_data = aps.delete('custom_data')
  custom_data[:aps] = aps
  custom_data.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
end

#default_optionsObject



15
16
17
# File 'lib/push_notifier/apns/notification.rb', line 15

def default_options
  @alert = ''
end

#dynamic_accessor(option) ⇒ Object



37
38
39
40
41
42
# File 'lib/push_notifier/apns/notification.rb', line 37

def dynamic_accessor(option)
  allowed_attrs.each do |x|
    singleton_class.class_eval do; attr_accessor "#{x}"; end
    self.send("#{x}=",option[x])
  end
end

#payloadObject



43
44
45
46
# File 'lib/push_notifier/apns/notification.rb', line 43

def payload
  dm = data_message
  [0, 0, 32, @token, 0, dm.bytesize, dm].pack("ccca*cca*")
end