Class: PushNotifier::APNS::Notification
- Inherits:
-
Object
- Object
- PushNotifier::APNS::Notification
- Defined in:
- lib/push_notifier/apns/notification.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #allowed_attrs ⇒ Object
- #clean_token(token) ⇒ Object
- #data_message ⇒ Object
- #default_options ⇒ Object
- #dynamic_accessor(option) ⇒ Object
-
#initialize(token, message, options = {}) ⇒ Notification
constructor
A new instance of Notification.
- #payload ⇒ Object
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, , ={}) dynamic_accessor @token = clean_token token @alert = @custom_data = .reject { |x,y| allowed_attrs.include? x } end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/push_notifier/apns/notification.rb', line 6 def @options end |
#token ⇒ Object
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_attrs ⇒ Object
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_message ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/push_notifier/apns/notification.rb', line 23 def 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_options ⇒ Object
15 16 17 |
# File 'lib/push_notifier/apns/notification.rb', line 15 def @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 |
#payload ⇒ Object
43 44 45 46 |
# File 'lib/push_notifier/apns/notification.rb', line 43 def payload dm = [0, 0, 32, @token, 0, dm.bytesize, dm].pack("ccca*cca*") end |