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.



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

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.



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

def host
  @host
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#pemObject

Returns the value of attribute pem.



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

def pem
  @pem
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#connect!Object



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

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)


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

def connected?
  @ssl
end

#disconnect!Object



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

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

#write(notification) ⇒ Object



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

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