apns_simple 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 885ffeb9c1c47bc2dbdce4e9544583333805c6eb
4
- data.tar.gz: 224b07b0dc020a6f6728713018f0c771f2ec5710
3
+ metadata.gz: c1df2a3e93672f23f6ba436092935d6c98a92216
4
+ data.tar.gz: b930413426fa2205811741e42c08d644e7b2c24d
5
5
  SHA512:
6
- metadata.gz: 87420a1bcd041e9edb43b3ae76a89a5a328cb36f56f19326bdf0b1fa01e15229e5e4fe9261e34d2d3e795f4763c010b3162362d48ea73972087bd00eaad3eb8b
7
- data.tar.gz: c96326ca6e2dac6a58a24bd583a2d6fab6b99922318382d32108350beadd6ffab934e5cfa72f4e5d6af305cb2216ca3e09fc818b2f1788ebebd8a99217c28786
6
+ metadata.gz: 910cc4063645c9bde49f41ea840e775299ded66d502c4f9deadc807307b68f2f21f362a703bd6119968de3c3aed20fe6d901c2c3de2bf915c772530ad52b58ec
7
+ data.tar.gz: b269f36995255422b1c1df4852b3da6044af36500b02c82a6996b45ac6e19b6c53d66273d2fdbc88421eab032b77200537835bae654c7e8b9ff350f1c18edf1d
@@ -46,29 +46,32 @@ module ApnsSimple
46
46
  end
47
47
 
48
48
  def push(notification)
49
- sock = TCPSocket.new(host, port)
50
- ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
51
- ssl.sync = true
52
- ssl.connect
53
- ssl.write(notification.payload)
49
+ unless notification.error
50
+ begin
51
+ sock = TCPSocket.new(host, port)
52
+ ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context)
53
+ ssl.sync = true
54
+ ssl.connect
55
+ ssl.write(notification.payload)
54
56
 
55
- if (ready = IO.select([ssl], [], [], TIMEOUT))
56
- readable_ssl_socket = ready.first.first
57
- if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT))
58
- command, code, _index = error.unpack('ccN')
59
- notification.error = true
60
- if command == COMMAND
61
- notification.error_code = code
62
- notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}"
63
- else
64
- notification.error_message = "Unknown command received from APNS server: #{command}"
57
+ if (ready = IO.select([ssl], [], [], TIMEOUT))
58
+ readable_ssl_socket = ready.first.first
59
+ if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT))
60
+ command, code, _index = error.unpack('ccN')
61
+ notification.error = true
62
+ if command == COMMAND
63
+ notification.error_code = code
64
+ notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}"
65
+ else
66
+ notification.error_message = "Unknown command received from APNS server: #{command}"
67
+ end
68
+ end
65
69
  end
70
+ ensure
71
+ ssl.close if ssl
72
+ sock.close if sock
66
73
  end
67
74
  end
68
-
69
- ensure
70
- ssl.close if ssl
71
- sock.close if sock
72
75
  end
73
76
 
74
77
  private
@@ -3,29 +3,35 @@ require 'json'
3
3
  module ApnsSimple
4
4
  class Notification
5
5
 
6
- attr_reader :token, :alert, :badge, :sound, :content_available, :custom_payload
6
+ PAYLOAD_MAX_BYTESIZE = 2048
7
+
8
+ attr_reader :payload
7
9
  attr_accessor :error, :error_message, :error_code
8
10
 
9
11
  def initialize(options, custom_payload = {})
10
- @token = options.fetch(:token)
11
- @alert = options[:alert]
12
- @badge = options[:badge]
13
- @sound = options[:sound] || 'default'
14
- @content_available = options[:content_available]
15
- @custom_payload = custom_payload
16
- end
12
+ token = options.fetch(:token)
13
+ alert = options[:alert]
14
+ badge = options[:badge]
15
+ sound = options[:sound] || 'default'
16
+ content_available = options[:content_available]
17
+
18
+ payload_hash = { aps: {} }
19
+ payload_hash[:aps][:alert] = alert if alert
20
+ payload_hash[:aps][:badge] = badge if badge
21
+ payload_hash[:aps][:sound] = sound if sound
22
+ payload_hash[:aps]['content-available'] = 1 if content_available
23
+ payload_hash.merge! custom_payload
24
+
25
+ packed_token = [token.gsub(/[<\s>]/,'')].pack('H*')
26
+ packed_message = payload_hash.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
27
+ payload_size = packed_message.bytesize
28
+
29
+ if payload_size > PAYLOAD_MAX_BYTESIZE
30
+ self.error = true
31
+ self.error_message = "Payload size is #{payload_size} bytes but maximum #{PAYLOAD_MAX_BYTESIZE} bytes allowed."
32
+ end
17
33
 
18
- def payload
19
- payload = { aps: {} }
20
- payload[:aps][:alert] = alert if alert
21
- payload[:aps][:badge] = badge if badge
22
- payload[:aps][:sound] = sound if sound
23
- payload[:aps]['content-available'] = 1 if content_available
24
- payload.merge! custom_payload
25
-
26
- packed_message = payload.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
27
- packed_token = [token.gsub(/[\s|<|>]/,'')].pack('H*')
28
- [0, 0, 32, packed_token, 0, packed_message.bytesize, packed_message].pack("ccca*cca*")
34
+ self.payload = [0, 0, 32, packed_token, 0, payload_size, packed_message].pack("ccca*cca*")
29
35
  end
30
36
 
31
37
  end
@@ -1,3 +1,3 @@
1
1
  module ApnsSimple
2
- VERSION = '1.1.1'
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apns_simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Voronkov