Class: Racoon::APNS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/racoon/apns/connection.rb

Direct Known Subclasses

FeedbackConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pem, host = 'gateway.push.apple.com', port = 2195, pass = nil) ⇒ Connection

Returns a new instance of Connection.



14
15
16
# File 'lib/racoon/apns/connection.rb', line 14

def initialize(pem, host = 'gateway.push.apple.com', port = 2195, pass = nil)
  @pem, @host, @port, @password = pem, host, port, pass
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/racoon/apns/connection.rb', line 12

def host
  @host
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/racoon/apns/connection.rb', line 12

def password
  @password
end

#pemObject

Returns the value of attribute pem.



12
13
14
# File 'lib/racoon/apns/connection.rb', line 12

def pem
  @pem
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/racoon/apns/connection.rb', line 12

def port
  @port
end

Instance Method Details

#connect!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/racoon/apns/connection.rb', line 18

def connect!
  raise "Your certificate is not set." unless self.pem

  @context      = OpenSSL::SSL::SSLContext.new
  @context.cert = OpenSSL::X509::Certificate.new(self.pem)
  @context.key  = OpenSSL::PKey::RSA.new(self.pem, self.password)

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

  return @sock, @ssl
end

#connected?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/racoon/apns/connection.rb', line 55

def connected?
  @ssl
end

#disconnect!Object



32
33
34
35
36
37
# File 'lib/racoon/apns/connection.rb', line 32

def disconnect!
  @ssl.close
  @sock.close
  @ssl = nil
  @sock = nil
end

#readObject



39
40
41
42
43
44
45
# File 'lib/racoon/apns/connection.rb', line 39

def read
  errors ||= []
  while error = @ssl.read(6)
    errors << parse_tuple(error)
  end
  errors
end

#write(bytes) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/racoon/apns/connection.rb', line 47

def write(bytes)
  if host.include? "sandbox"
    notification = Notification.parse(bytes)
    Config.logger.debug "#{Time.now} [#{host}:#{port}] Device: #{notification.device_token.unpack('H*')} sending #{notification.json_payload}"
  end
  @ssl.write(notification.to_bytes)
end