forked from PRX/apn_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broke out connections into a new module that opens and closes connect…
…ions easily.
- Loading branch information
Showing
7 changed files
with
129 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module APN | ||
module Connection | ||
|
||
class << self | ||
|
||
# Yields up an SSL socket to write notifications to. | ||
# The connections are close automatically. | ||
# | ||
# Example: | ||
# APN::Configuration.open_for_delivery do |conn| | ||
# conn.write('my cool notification') | ||
# end | ||
# | ||
# Configuration parameters are: | ||
# | ||
# configatron.apn.passphrase = '' | ||
# configatron.apn.port = 2195 | ||
# configatron.apn.host = 'gateway.sandbox.push.apple.com' # Development | ||
# configatron.apn.host = 'gateway.push.apple.com' # Production | ||
# configatron.apn.cert = File.join(rails_root, 'config', 'apple_push_notification_development.pem')) # Development | ||
# configatron.apn.cert = File.join(rails_root, 'config', 'apple_push_notification_production.pem')) # Production | ||
def open_for_delivery(options = {}, &block) | ||
open(options, &block) | ||
end | ||
|
||
# Yields up an SSL socket to receive feedback from. | ||
# The connections are close automatically. | ||
# Configuration parameters are: | ||
# | ||
# configatron.apn.feedback.passphrase = '' | ||
# configatron.apn.feedback.port = 2196 | ||
# configatron.apn.feedback.host = 'feedback.sandbox.push.apple.com' # Development | ||
# configatron.apn.feedback.host = 'feedback.push.apple.com' # Production | ||
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_development.pem')) # Development | ||
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_production.pem')) # Production | ||
def open_for_feedback(options = {}, &block) # :nodoc: | ||
|
||
end | ||
|
||
private | ||
def open(options = {}, &block) # :nodoc: | ||
options = {:cert => configatron.apn.cert, | ||
:passphrase => configatron.apn.passphrase, | ||
:host => configatron.apn.host, | ||
:port => configatron.apn.port}.merge(options) | ||
cert = File.read(options[:cert]) | ||
ctx = OpenSSL::SSL::SSLContext.new | ||
ctx.key = OpenSSL::PKey::RSA.new(cert, options[:passphrase]) | ||
ctx.cert = OpenSSL::X509::Certificate.new(cert) | ||
|
||
s = TCPSocket.new(options[:host], options[:port]) | ||
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx) | ||
ssl.sync = true | ||
ssl.connect | ||
|
||
yield ssl if block_given? | ||
|
||
ssl.close | ||
s.close | ||
end | ||
|
||
end | ||
|
||
end # Connection | ||
end # APN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper.rb') | ||
|
||
describe APN::Connection do | ||
|
||
describe 'open_for_delivery' do | ||
|
||
it 'should create a connection to Apple, yield it, and then close' do | ||
rsa_mock = mock('rsa_mock') | ||
OpenSSL::PKey::RSA.should_receive(:new).with(apn_cert, '').and_return(rsa_mock) | ||
|
||
cert_mock = mock('cert_mock') | ||
OpenSSL::X509::Certificate.should_receive(:new).and_return(cert_mock) | ||
|
||
ctx_mock = mock('ctx_mock') | ||
ctx_mock.should_receive(:key=).with(rsa_mock) | ||
ctx_mock.should_receive(:cert=).with(cert_mock) | ||
OpenSSL::SSL::SSLContext.should_receive(:new).and_return(ctx_mock) | ||
|
||
tcp_mock = mock('tcp_mock') | ||
tcp_mock.should_receive(:close) | ||
TCPSocket.should_receive(:new).with('gateway.sandbox.push.apple.com', 2195).and_return(tcp_mock) | ||
|
||
ssl_mock = mock('ssl_mock') | ||
ssl_mock.should_receive(:sync=).with(true) | ||
ssl_mock.should_receive(:connect) | ||
ssl_mock.should_receive(:write).with('message-0') | ||
ssl_mock.should_receive(:write).with('message-1') | ||
ssl_mock.should_receive(:close) | ||
OpenSSL::SSL::SSLSocket.should_receive(:new).with(tcp_mock, ctx_mock).and_return(ssl_mock) | ||
|
||
APN::Connection.open_for_delivery do |conn| | ||
conn.write('message-0') | ||
conn.write('message-1') | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters