apnd 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -1
- data/Rakefile +16 -0
- data/lib/apnd.rb +4 -3
- data/lib/apnd/daemon.rb +24 -15
- data/lib/apnd/daemon/protocol.rb +10 -0
- data/lib/apnd/errors.rb +1 -1
- data/lib/apnd/notification.rb +52 -4
- data/lib/apnd/version.rb +2 -2
- metadata +17 -11
data/README.markdown
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# APND
|
2
2
|
|
3
3
|
APND (Apple Push Notification Daemon) is a ruby library to send Apple Push
|
4
|
-
Notifications
|
4
|
+
Notifications to iPhones.
|
5
5
|
|
6
6
|
Apple recommends application developers create one connection to their
|
7
7
|
upstream push notification server, rather than creating one per notification.
|
data/Rakefile
CHANGED
@@ -11,3 +11,19 @@ desc "Open an irb session preloaded with this library"
|
|
11
11
|
task :console do
|
12
12
|
sh "irb -rubygems -r ./lib/apnd.rb -I ./lib"
|
13
13
|
end
|
14
|
+
|
15
|
+
require 'sdoc_helpers'
|
16
|
+
desc "Push a new version to Gemcutter"
|
17
|
+
task :publish do
|
18
|
+
require 'apnd/version'
|
19
|
+
|
20
|
+
ver = APND::Version
|
21
|
+
|
22
|
+
sh "gem build apnd.gemspec"
|
23
|
+
sh "gem push apnd-#{ver}.gem"
|
24
|
+
sh "git tag -a -m 'APND v#{ver}' v#{ver}"
|
25
|
+
sh "git push origin v#{ver}"
|
26
|
+
sh "git push origin master"
|
27
|
+
sh "git clean -fd"
|
28
|
+
exec "rake pages"
|
29
|
+
end
|
data/lib/apnd.rb
CHANGED
@@ -8,14 +8,14 @@ module APND
|
|
8
8
|
autoload :Notification, 'apnd/notification'
|
9
9
|
|
10
10
|
#
|
11
|
-
# APND
|
11
|
+
# Returns APND::Settings
|
12
12
|
#
|
13
13
|
def self.settings
|
14
14
|
@@settings ||= Settings.new
|
15
15
|
end
|
16
16
|
|
17
17
|
#
|
18
|
-
# Yields APND
|
18
|
+
# Yields APND::Settings
|
19
19
|
#
|
20
20
|
def self.configure
|
21
21
|
yield settings
|
@@ -23,6 +23,7 @@ module APND
|
|
23
23
|
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
|
27
|
+
def ohai(message) #:nodoc:
|
27
28
|
puts "[%s] %s" % [Time.now.strftime("%Y-%m-%d %H:%M:%S"), message]
|
28
29
|
end
|
data/lib/apnd/daemon.rb
CHANGED
@@ -40,21 +40,30 @@ module APND
|
|
40
40
|
end
|
41
41
|
|
42
42
|
EventMachine::PeriodicTimer.new(@timer) do
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
43
|
+
process_notifications!
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
#
|
51
|
+
# Sends each notification in the queue upstream to Apple
|
52
|
+
#
|
53
|
+
def process_notifications!
|
54
|
+
count = @queue.size
|
55
|
+
if count > 0
|
56
|
+
ohai "Queue has #{count} item#{count == 1 ? '' : 's'}"
|
57
|
+
count.times do
|
58
|
+
@queue.pop do |notification|
|
59
|
+
begin
|
60
|
+
ohai "Sending notification"
|
61
|
+
@apple.write(notification.to_bytes)
|
62
|
+
rescue Errno::EPIPE, OpenSSL::SSL::SSLError
|
63
|
+
ohai "Error, notification has been added back to the queue"
|
64
|
+
@queue.push(notification)
|
65
|
+
rescue RuntimeError => error
|
66
|
+
ohai "Error: #{error}"
|
58
67
|
end
|
59
68
|
end
|
60
69
|
end
|
data/lib/apnd/daemon/protocol.rb
CHANGED
@@ -3,13 +3,23 @@ module APND
|
|
3
3
|
# Daemon::Protocol handles incoming APNs
|
4
4
|
#
|
5
5
|
class Daemon::Protocol < ::EventMachine::Connection
|
6
|
+
|
7
|
+
#
|
8
|
+
# Queue should be the EventMachine queue, see APND::Daemon
|
9
|
+
#
|
6
10
|
attr_accessor :queue
|
7
11
|
|
12
|
+
#
|
13
|
+
# Called when a client connection is opened
|
14
|
+
#
|
8
15
|
def post_init
|
9
16
|
@address = Socket.unpack_sockaddr_in(self.get_peername)
|
10
17
|
ohai "#{@address.last}:#{@address.first} opened connection"
|
11
18
|
end
|
12
19
|
|
20
|
+
#
|
21
|
+
# Called when a client connection is closed
|
22
|
+
#
|
13
23
|
def unbind
|
14
24
|
ohai "#{@address.last}:#{@address.first} closed connection"
|
15
25
|
end
|
data/lib/apnd/errors.rb
CHANGED
data/lib/apnd/notification.rb
CHANGED
@@ -7,14 +7,64 @@ module APND
|
|
7
7
|
class Notification
|
8
8
|
|
9
9
|
class << self
|
10
|
+
#
|
11
|
+
# The host notifications will be written to, usually one
|
12
|
+
# running APND
|
13
|
+
#
|
10
14
|
attr_accessor :upstream_host
|
15
|
+
|
16
|
+
#
|
17
|
+
# The port to connect to upstream_host on
|
18
|
+
#
|
11
19
|
attr_accessor :upstream_port
|
12
20
|
end
|
13
21
|
|
22
|
+
#
|
23
|
+
# Set upstream host/port to default values
|
24
|
+
#
|
14
25
|
self.upstream_host = APND.settings.notification.host
|
15
26
|
self.upstream_port = APND.settings.notification.port.to_i
|
16
27
|
|
17
|
-
|
28
|
+
#
|
29
|
+
# The device token from Apple
|
30
|
+
#
|
31
|
+
attr_accessor :token
|
32
|
+
|
33
|
+
#
|
34
|
+
# The alert to send
|
35
|
+
#
|
36
|
+
attr_accessor :alert
|
37
|
+
|
38
|
+
#
|
39
|
+
# The badge number to set
|
40
|
+
#
|
41
|
+
attr_accessor :badge
|
42
|
+
|
43
|
+
#
|
44
|
+
# The sound to play
|
45
|
+
#
|
46
|
+
attr_accessor :sound
|
47
|
+
|
48
|
+
#
|
49
|
+
# Custom data to send
|
50
|
+
#
|
51
|
+
attr_accessor :custom
|
52
|
+
|
53
|
+
#
|
54
|
+
# Creates a new socket to upstream_host:upstream_port
|
55
|
+
#
|
56
|
+
def self.upstream_socket
|
57
|
+
@socket ||= TCPSocket.new(upstream_host, upstream_port)
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# Opens a new socket upstream, yields it, and closes it
|
62
|
+
#
|
63
|
+
def self.open_upstream_socket(&block)
|
64
|
+
socket = upstream_socket
|
65
|
+
yield socket
|
66
|
+
socket.close
|
67
|
+
end
|
18
68
|
|
19
69
|
#
|
20
70
|
# Create a new APN
|
@@ -112,9 +162,7 @@ module APND
|
|
112
162
|
# Pushes notification to upstream host:port (default is localhost:22195)
|
113
163
|
#
|
114
164
|
def push!
|
115
|
-
|
116
|
-
socket.write(to_bytes)
|
117
|
-
socket.close
|
165
|
+
self.class.open_upstream_socket { |sock| sock.write(to_bytes) }
|
118
166
|
end
|
119
167
|
|
120
168
|
#
|
data/lib/apnd/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joshua Priddle
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-10-02 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -22,11 +22,13 @@ dependencies:
|
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - "="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
28
|
- 0
|
29
|
-
|
29
|
+
- 12
|
30
|
+
- 10
|
31
|
+
version: 0.12.10
|
30
32
|
type: :runtime
|
31
33
|
version_requirements: *id001
|
32
34
|
- !ruby/object:Gem::Dependency
|
@@ -34,11 +36,13 @@ dependencies:
|
|
34
36
|
prerelease: false
|
35
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
36
38
|
requirements:
|
37
|
-
- - "
|
39
|
+
- - "="
|
38
40
|
- !ruby/object:Gem::Version
|
39
41
|
segments:
|
40
|
-
-
|
41
|
-
|
42
|
+
- 1
|
43
|
+
- 4
|
44
|
+
- 6
|
45
|
+
version: 1.4.6
|
42
46
|
type: :runtime
|
43
47
|
version_requirements: *id002
|
44
48
|
- !ruby/object:Gem::Dependency
|
@@ -46,14 +50,16 @@ dependencies:
|
|
46
50
|
prerelease: false
|
47
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
48
52
|
requirements:
|
49
|
-
- - "
|
53
|
+
- - "="
|
50
54
|
- !ruby/object:Gem::Version
|
51
55
|
segments:
|
56
|
+
- 1
|
57
|
+
- 1
|
52
58
|
- 0
|
53
|
-
version:
|
59
|
+
version: 1.1.0
|
54
60
|
type: :runtime
|
55
61
|
version_requirements: *id003
|
56
|
-
description: " APND (Apple Push Notification Daemon) is a ruby library to send Apple Push\n Notifications
|
62
|
+
description: " APND (Apple Push Notification Daemon) is a ruby library to send Apple Push\n Notifications to iPhones.\n"
|
57
63
|
email: jpriddle@nevercraft.net
|
58
64
|
executables:
|
59
65
|
- apnd
|