banjo-apn_sender 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/apn_sender.gemspec CHANGED
@@ -5,28 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{banjo-apn_sender}
8
- s.version = "2.0.0"
8
+ s.version = "2.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Kali Donovan}, %q{KW Justin Leung}]
12
12
  s.date = %q{2012-06-19}
13
13
  s.description = %q{Based on Kali Donovan's APN sender 1.x. 2.0 keep things lean - we removed the resque layer, and make APN connection pluggable to multithreaded background worker (like SideKiq) to send Apple Push Notifications over a persistent TCP socket.}
14
14
  s.email = %q{kali.donovan@gmail.com justin@teambanjo.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- "LICENSE",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "apn_sender.gemspec",
25
- "lib/apn.rb",
26
- "lib/apn/base.rb",
27
- "lib/apn/feedback.rb",
28
- "lib/apn/notification.rb"
29
- ]
15
+ s.extra_rdoc_files = [ "LICENSE", "README.rdoc" ]
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
30
20
  s.homepage = %q{http://github.com/BanjoInc/apn_sender}
31
21
  s.require_paths = [%q{lib}]
32
22
  s.rubygems_version = %q{1.8.6}
@@ -0,0 +1,67 @@
1
+ class APN::Connection
2
+ include APN::Base
3
+ TIMES_TO_RETRY_SOCKET_ERROR = 2
4
+
5
+ @production_senders = Hash.new
6
+ @sandbox_senders = Hash.new
7
+ @enterprise_semaphore = Mutex.new
8
+
9
+ def send_to_apple(notification)
10
+ retries = 0
11
+
12
+ begin
13
+ self.socket.write( notification.to_s )
14
+ rescue => e
15
+ Rails.logger.error("Try #{retries}: APNConnection to #{apn_host} error with #{e}")
16
+
17
+ # Try reestablishing the connection
18
+ if (retries += 1) <= TIMES_TO_RETRY_SOCKET_ERROR
19
+ teardown_connection
20
+ setup_connection
21
+ retry
22
+ end
23
+
24
+ Rails.logger.error("APNConnection gave up send_to_apple after #{retries} failures")
25
+ raise e
26
+ end
27
+ end
28
+
29
+ def self.send(token, options, sandbox = false, enterprise = false)
30
+ msg = APN::Notification.new(token, options)
31
+ raise "Invalid notification options (did you provide :alert, :badge, or :sound?): #{options.inspect}" unless msg.valid?
32
+
33
+ thread_id = Thread.current.object_id
34
+
35
+ # Use only 1 single thread for internal enterprise cert
36
+ sender = if enterprise
37
+ if sandbox
38
+ @sandbox_enterprise_sender ||= new(worker_count: 1, sandbox: 1, verbose: 1, enterprise: 1)
39
+ else
40
+ @production_enterprise_sender ||= new(worker_count: 1, verbose: 1, enterprise: 1)
41
+ end
42
+ else
43
+ if sandbox
44
+ @sandbox_senders[thread_id] ||= new(worker_count: 1, sandbox: 1, verbose: 1)
45
+ else
46
+ @production_senders[thread_id] ||= new(worker_count: 1, verbose: 1)
47
+ end
48
+ end
49
+
50
+ Rails.logger.info "[APNConnection:#{sender.object_id} #{sandbox ? 'sandbox' : 'production'}#{enterprise ? ' enterprise' : ''}] token: #{token} message: #{options}"
51
+ if enterprise
52
+ @enterprise_semaphore.synchronize { sender.send_to_apple(msg) }
53
+ else
54
+ sender.send_to_apple(msg)
55
+ end
56
+ end
57
+
58
+ protected
59
+ def apn_host
60
+ @apn_host ||= apn_sandbox? ? "gateway.sandbox.push.apple.com" : "gateway.push.apple.com"
61
+ end
62
+
63
+ def apn_port
64
+ 2195
65
+ end
66
+ end
67
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banjo-apn_sender
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -54,6 +54,8 @@ extra_rdoc_files:
54
54
  - LICENSE
55
55
  - README.rdoc
56
56
  files:
57
+ - .gitignore
58
+ - Gemfile
57
59
  - LICENSE
58
60
  - README.rdoc
59
61
  - Rakefile
@@ -61,6 +63,7 @@ files:
61
63
  - apn_sender.gemspec
62
64
  - lib/apn.rb
63
65
  - lib/apn/base.rb
66
+ - lib/apn/connection.rb
64
67
  - lib/apn/feedback.rb
65
68
  - lib/apn/notification.rb
66
69
  homepage: http://github.com/BanjoInc/apn_sender