Class: ApnsPolite::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/apns_polite/notification.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Notification

初期化



10
11
12
13
14
15
16
17
18
19
# File 'lib/apns_polite/notification.rb', line 10

def initialize(options = nil)
	if options
		host = options[:host]
		pem = options[:pem]
		port = options[:port]
		password = options[:password]

		connection host, port, pem, password
	end
end

Instance Method Details

#closeObject

切断



51
52
53
54
55
56
57
# File 'lib/apns_polite/notification.rb', line 51

def close
	@ssl.close if @ssl
	@sock.close if @sock

	@ssl = nil
	@sock = nil
end

#connection(host, port, pem, password = nil) ⇒ Object

接続



21
22
23
24
25
26
27
28
29
# File 'lib/apns_polite/notification.rb', line 21

def connection(host, port, pem, password=nil)

	@host = host || 'gateway.sandbox.push.apple.com'
	@port = port || 2195
	@pem = pem
	@password = password

	reconnection
end

#push(message) ⇒ Object

通知



59
60
61
62
# File 'lib/apns_polite/notification.rb', line 59

def push(message)
	raise "No connected." unless @ssl
	@ssl.write message.pack
end

#reconnectionObject

再接続



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apns_polite/notification.rb', line 31

def reconnection
	close

	raise "The path to your pem file is not set." unless @pem
	raise "The path to your pem file does not exist!" unless File.exist?(@pem)

	pemText = File.read @pem

	context = OpenSSL::SSL::SSLContext.new
	context.cert = OpenSSL::X509::Certificate.new pemText
	context.key = OpenSSL::PKey::RSA.new pemText, @password

	sock = TCPSocket.new @host, @port
	ssl = OpenSSL::SSL::SSLSocket.new sock, context
	ssl.connect

	@sock = sock
	@ssl = ssl
end