Sha256: efd3ee05f4d59381380c0b4dfd757ccfa492cbc697baa3c2916b91c4367d1c78

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'openssl'
require 'socket'

module ApnServer
  class Client
    
    attr_accessor :pem, :host, :port, :password
    
    def initialize(pem, host = 'gateway.push.apple.com', port = 2195, pass = nil)
      @pem, @host, @port, @password = pem, host, port, pass
    end
    
    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
    
    def disconnect!
      @ssl.close
      @sock.close
    end
    
    def write(notification)
      puts "#{Time.now} [#{host}:#{port}] sending #{notification.alert}"
      @ssl.write(notification.to_bytes)
    end
    
    def connected?
      @ssl
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bpoweski-apnserver-0.0.10 lib/apnserver/client.rb
bpoweski-apnserver-0.0.12 lib/apnserver/client.rb
bpoweski-apnserver-0.0.8 lib/apnserver/client.rb
bpoweski-apnserver-0.0.9 lib/apnserver/client.rb