Class: ApnServer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apnserver/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



8
9
10
# File 'lib/apnserver/client.rb', line 8

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.



6
7
8
# File 'lib/apnserver/client.rb', line 6

def host
  @host
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/apnserver/client.rb', line 6

def password
  @password
end

#pemObject

Returns the value of attribute pem.



6
7
8
# File 'lib/apnserver/client.rb', line 6

def pem
  @pem
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/apnserver/client.rb', line 6

def port
  @port
end

Instance Method Details

#connect!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/apnserver/client.rb', line 12

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

  @context      = OpenSSL::SSL::SSLContext.new
  @context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
  @context.key  = OpenSSL::PKey::RSA.new(File.read(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)


39
40
41
# File 'lib/apnserver/client.rb', line 39

def connected?
  @ssl
end

#disconnect!Object



27
28
29
30
31
32
# File 'lib/apnserver/client.rb', line 27

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

#write(notification) ⇒ Object



34
35
36
37
# File 'lib/apnserver/client.rb', line 34

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