apns_simple 0.1.0 → 0.1.2

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: 7449bfb406d6984c0ed037b8db4ca415fda8ee52
4
- data.tar.gz: 838af8c0d804d0272c65cda919dffaa21c3e94da
3
+ metadata.gz: e8d3ce67428d291516b75022a6ee5529c8b0b2fb
4
+ data.tar.gz: cf94350fa0378fbb269a430fd26f225294e34741
5
5
  SHA512:
6
- metadata.gz: 49aa36731d9913f063c624eef4ae6d9dff19aef6ebfd7a31ca1b02ec6eb62d7787d375d7b640407ebd0d7f6a20df028b1a52b2da3a57ca19f6c4cffba36bff1e
7
- data.tar.gz: a937c2fb8b54289ca4c65401b064a54918415c586bd348b39054d420fb9de39dc16446bd3df844c3d52b0d2511cc1b27c5e794a7639223efba19682e5ff96444
6
+ metadata.gz: ddcc8b6ec117379829ad469bfac0f41a452c2474ba88ea45d73633ae262d9537b21af78287625212c0d22469795d040d545ca9fbf65d09d64b1fa0d17c0079cd
7
+ data.tar.gz: 805088137b1bec19c635668d1bb3795c6c9aab16b80fd65ebd920c38ef6243b8fee5c1262dfaa65562c8f0ecf2f234d9bd2a862075884306d682463bfff29b1d
data/lib/apns_simple.rb CHANGED
@@ -1,76 +1,6 @@
1
- require 'openssl'
2
- require 'socket'
3
- require 'json'
4
-
1
+ require_relative 'apns_simple/client'
2
+ require_relative 'apns_simple/notification'
5
3
  require_relative 'apns_simple/version'
6
4
 
7
5
  module ApnsSimple
8
- class Client
9
-
10
- attr_reader :certificate, :passphrase, :host, :port
11
-
12
- def initialize(options)
13
- @certificate = options.fetch(:certificate)
14
- @passphrase = options[:passphrase] || ''
15
- gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
16
- @host, @port = parse_gateway_uri(gateway_uri)
17
- end
18
-
19
- def push(notification)
20
- begin
21
- ctx = OpenSSL::SSL::SSLContext.new
22
- ctx.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
23
- ctx.cert = OpenSSL::X509::Certificate.new(certificate)
24
-
25
- sock = TCPSocket.new(host, port)
26
- ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
27
- ssl.connect
28
- ssl.write(notification.payload)
29
- ssl.flush
30
-
31
- if IO.select([ssl], nil, nil, 1) && error = ssl.read(6)
32
- command, status, index = error.unpack("ccN")
33
- notification.error = status
34
- end
35
- ensure
36
- ssl.close if ssl
37
- sock.close if sock
38
- end
39
- end
40
-
41
- private
42
-
43
- def parse_gateway_uri(uri)
44
- first, last = uri.sub('apn://', '').split(':')
45
- [first, last.to_i]
46
- end
47
-
48
- end
49
-
50
- class Notification
51
-
52
- attr_reader :token, :alert, :badge, :sound, :content_available
53
- attr_accessor :error
54
-
55
- def initialize(options)
56
- @token = options.fetch(:token)
57
- @alert = options[:alert]
58
- @badge = options[:badge]
59
- @sound = options[:sound] || 'default'
60
- @content_available = options[:content_available]
61
- end
62
-
63
- def payload
64
- payload = { aps: {} }
65
- payload[:aps][:alert] = alert if alert
66
- payload[:aps][:badge] = badge if badge
67
- payload[:aps][:sound] = sound if sound
68
- payload[:aps][:content_available] = 1 if content_available
69
-
70
- packed_message = payload.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
71
- packed_token = [token.gsub(/[\s|<|>]/,'')].pack('H*')
72
- [0, 0, 32, packed_token, 0, packed_message.bytesize, packed_message].pack("ccca*cca*")
73
- end
74
-
75
- end
76
6
  end
@@ -0,0 +1,46 @@
1
+ require 'openssl'
2
+ require 'socket'
3
+
4
+ module ApnsSimple
5
+ class Client
6
+
7
+ attr_reader :certificate, :passphrase, :host, :port
8
+
9
+ def initialize(options)
10
+ @certificate = options.fetch(:certificate)
11
+ @passphrase = options[:passphrase] || ''
12
+ gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
13
+ @host, @port = parse_gateway_uri(gateway_uri)
14
+ end
15
+
16
+ def push(notification)
17
+ begin
18
+ ctx = OpenSSL::SSL::SSLContext.new
19
+ ctx.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
20
+ ctx.cert = OpenSSL::X509::Certificate.new(certificate)
21
+
22
+ sock = TCPSocket.new(host, port)
23
+ ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
24
+ ssl.connect
25
+ ssl.write(notification.payload)
26
+ ssl.flush
27
+
28
+ if IO.select([ssl], nil, nil, 1) && error = ssl.read(6)
29
+ _command, status, _index = error.unpack("ccN")
30
+ notification.error = status
31
+ end
32
+ ensure
33
+ ssl.close if ssl
34
+ sock.close if sock
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def parse_gateway_uri(uri)
41
+ first, last = uri.sub('apn://', '').split(':')
42
+ [first, last.to_i]
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+
3
+ module ApnsSimple
4
+ class Notification
5
+
6
+ attr_reader :token, :alert, :badge, :sound, :content_available
7
+ attr_accessor :error
8
+
9
+ def initialize(options)
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
+ end
16
+
17
+ def payload
18
+ payload = { aps: {} }
19
+ payload[:aps][:alert] = alert if alert
20
+ payload[:aps][:badge] = badge if badge
21
+ payload[:aps][:sound] = sound if sound
22
+ payload[:aps][:content_available] = 1 if content_available
23
+
24
+ packed_message = payload.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
25
+ packed_token = [token.gsub(/[\s|<|>]/,'')].pack('H*')
26
+ [0, 0, 32, packed_token, 0, packed_message.bytesize, packed_message].pack("ccca*cca*")
27
+ end
28
+
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module ApnsSimple
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
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: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Voronkov
@@ -57,6 +57,8 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/apns_simple.rb
60
+ - lib/apns_simple/client.rb
61
+ - lib/apns_simple/notification.rb
60
62
  - lib/apns_simple/version.rb
61
63
  homepage: https://github.com/Antiarchitect/apns_simple
62
64
  licenses: