Class: ApnsPolite::Feedback

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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Feedback

初期化



10
11
12
13
14
15
16
17
18
19
# File 'lib/apns_polite/feedback.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

切断



50
51
52
53
# File 'lib/apns_polite/feedback.rb', line 50

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

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

接続



21
22
23
24
25
26
27
28
# File 'lib/apns_polite/feedback.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

#reconnectionObject

再接続



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

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

#stream(&block) ⇒ Object

取得



55
56
57
58
59
60
61
62
63
64
# File 'lib/apns_polite/feedback.rb', line 55

def stream(&block)

	while line = @sock.gets
		payload = line.strip.unpack('N1n1H140')
		if payload and payload.length >= 3
			yield payload[2], payload[0]
		end
	end

end