Class: TinyAPNS::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



4
5
6
# File 'lib/tiny_apns.rb', line 4

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



4
5
6
# File 'lib/tiny_apns.rb', line 4

def badge
  @badge
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/tiny_apns.rb', line 4

def params
  @params
end

#soundObject

Returns the value of attribute sound.



4
5
6
# File 'lib/tiny_apns.rb', line 4

def sound
  @sound
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/tiny_apns.rb', line 4

def token
  @token
end

Instance Method Details

#apple_hashObject



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_tokenObject



27
28
29
# File 'lib/tiny_apns.rb', line 27

def get_hex_token
  [self.token.delete(' ')].pack('H*')
end

#send(connection) ⇒ Object

Raises:

  • (ArgumentError)


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_jsonObject



23
24
25
# File 'lib/tiny_apns.rb', line 23

def to_apple_json
  self.apple_hash.to_json
end

#to_post_stringObject

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/tiny_apns.rb', line 31

def to_post_string
  json = self.to_apple_json
  message = "\0\0 #{get_hex_token}\0#{json.length.chr}#{json}"
  raise ArgumentError.new("Total message size too long: #{message}") if message.size.to_i > 256
  message
end