Sha256: 75a5186ba0df7b9d8f4ec6a7c0b568a32810589828f21ed9aae7e4404e47173f

Contents?: true

Size: 1.76 KB

Versions: 16

Compression:

Stored size: 1.76 KB

Contents

require 'socket'
require 'openssl'
module Rhoconnect
  class Apple
    def self.ping(params)
      settings = get_config(Rhoconnect.base_directory)[Rhoconnect.environment]
      cert_file = File.join(Rhoconnect.base_directory,settings[:iphonecertfile])
      cert = File.read(cert_file) if File.exists?(cert_file)
    	passphrase = settings[:iphonepassphrase]
    	host = settings[:iphoneserver]
    	port = settings[:iphoneport]
    	if(cert and host and port) 
        begin
          ssl_ctx = OpenSSL::SSL::SSLContext.new
      		ssl_ctx.key = OpenSSL::PKey::RSA.new(cert, passphrase)
      		ssl_ctx.cert = OpenSSL::X509::Certificate.new(cert)

      		socket = TCPSocket.new(host, port)
      		ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_ctx)
      		ssl_socket.sync = true
      		ssl_socket.connect

      		ssl_socket.write(apn_message(params))
      		ssl_socket.close
      		socket.close
    		rescue SocketError => error
    		  log "Error while sending ping: #{error}"
    		  raise error
    		end
		  end
    end

    # Generates APNS package
  	def self.apn_message(params)
  		data = {}
  		data['aps'] = {}
  		data['aps']['alert'] = params['message'] if params['message'] 
  		data['aps']['badge'] = params['badge'].to_i if params['badge']
  		data['aps']['sound'] = params['sound'] if params['sound']
  		data['aps']['vibrate'] = params['vibrate'] if params['vibrate']
  		data['do_sync'] = params['sources'] if params['sources']
  		json = data.to_json
  		"\0\0 #{[params['device_pin'].delete(' ')].pack('H*')}\0#{json.length.chr}#{json}"
  	end
  end
  
  # Deprecated - use Apple instead
  class Iphone < Apple    
    def self.ping(params)
      log "DEPRECATION WARNING: 'iphone' is a deprecated device_type, use 'apple' instead"
      super(params)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rhoconnect-4.0.0.beta.24 lib/rhoconnect/ping/apple.rb
rhoconnect-3.4.5 lib/rhoconnect/ping/apple.rb
rhoconnect-4.0.0.beta.10 lib/rhoconnect/ping/apple.rb
rhoconnect-4.0.0.beta.12 lib/rhoconnect/ping/apple.rb
rhoconnect-3.4.4 lib/rhoconnect/ping/apple.rb
rhoconnect-3.4.3 lib/rhoconnect/ping/apple.rb
rhoconnect-3.4.2 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.6 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.5 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.4 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.3 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.2 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.1 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.1.beta4 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.1.beta3 lib/rhoconnect/ping/apple.rb
rhoconnect-3.3.1.beta2 lib/rhoconnect/ping/apple.rb