Class: TinyAPNS::Message
- Inherits:
-
Object
- Object
- TinyAPNS::Message
- Defined in:
- lib/tiny_apns.rb
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#params ⇒ Object
Returns the value of attribute params.
-
#sound ⇒ Object
Returns the value of attribute sound.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #apple_hash ⇒ Object
- #get_hex_token ⇒ Object
- #send(connection) ⇒ Object
- #to_apple_json ⇒ Object
- #to_post_string ⇒ Object
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
4 5 6 |
# File 'lib/tiny_apns.rb', line 4 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
4 5 6 |
# File 'lib/tiny_apns.rb', line 4 def badge @badge end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/tiny_apns.rb', line 4 def params @params end |
#sound ⇒ Object
Returns the value of attribute sound.
4 5 6 |
# File 'lib/tiny_apns.rb', line 4 def sound @sound end |
#token ⇒ Object
Returns the value of attribute token.
4 5 6 |
# File 'lib/tiny_apns.rb', line 4 def token @token end |
Instance Method Details
#apple_hash ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tiny_apns.rb', line 6 def apple_hash result = {} result['aps'] = {} result['aps']['alert'] = self.alert if self.alert result['aps']['badge'] = self.badge.to_i if self.badge if self.sound result['aps']['sound'] = self.sound if self.sound.is_a? String result['aps']['sound'] = "1.aiff" if self.sound.is_a?(TrueClass) end if self.params self.params.each do |key,value| result[key.to_s] = value.to_s end end result end |
#get_hex_token ⇒ Object
27 28 29 |
# File 'lib/tiny_apns.rb', line 27 def get_hex_token [self.token.delete(' ')].pack('H*') end |
#send(connection) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/tiny_apns.rb', line 38 def send(connection) raise ArgumentError.new("Missing token") if self.token.blank? connection.open_for_delivery do |ssl| ssl.write(self.to_post_string) end end |
#to_apple_json ⇒ Object
23 24 25 |
# File 'lib/tiny_apns.rb', line 23 def to_apple_json self.apple_hash.to_json end |
#to_post_string ⇒ Object
31 32 33 34 35 36 |
# File 'lib/tiny_apns.rb', line 31 def to_post_string json = self.to_apple_json = "\0\0 #{get_hex_token}\0#{json.length.chr}#{json}" raise ArgumentError.new("Total message size too long: #{}") if .size.to_i > 256 end |