apnd 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 (APNs) to iPhones.
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
@@ -8,14 +8,14 @@ module APND
8
8
  autoload :Notification, 'apnd/notification'
9
9
 
10
10
  #
11
- # APND Settings
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 Settings
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
- def ohai(message)
26
+
27
+ def ohai(message) #:nodoc:
27
28
  puts "[%s] %s" % [Time.now.strftime("%Y-%m-%d %H:%M:%S"), message]
28
29
  end
@@ -40,21 +40,30 @@ module APND
40
40
  end
41
41
 
42
42
  EventMachine::PeriodicTimer.new(@timer) do
43
- count = @queue.size
44
- if count > 0
45
- ohai "Queue has #{count} item#{count == 1 ? '' : 's'}"
46
- count.times do
47
- @queue.pop do |notification|
48
- begin
49
- ohai "Sending notification"
50
- @apple.write(notification.to_bytes)
51
- rescue Errno::EPIPE, OpenSSL::SSL::SSLError
52
- ohai "Error, notification has been added back to the queue"
53
- @queue.push(notification)
54
- rescue RuntimeError => error
55
- ohai "Error: #{error}"
56
- end
57
- end
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module APND
2
- module Errors
2
+ module Errors #:nodoc: all
3
3
 
4
4
  #
5
5
  # Raised if APN payload is larger than 256 bytes
@@ -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
- attr_accessor :token, :alert, :badge, :sound, :custom
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
- socket = TCPSocket.new(self.class.upstream_host, self.class.upstream_port)
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
  #
@@ -1,8 +1,8 @@
1
1
  module APND
2
- class Version
2
+ class Version #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
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-09-29 00:00:00 -04:00
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
- version: "0"
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
- - 0
41
- version: "0"
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: "0"
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 (APNs) to iPhones.\n"
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