Sha256: df09b2284ff6f5f4903042196fc43593e4f711b40877859dcd9e0a21646a5e28

Contents?: true

Size: 1.68 KB

Versions: 22

Compression:

Stored size: 1.68 KB

Contents

require 'socket'
require 'openssl'
module Rhosync
  class Apple
    def self.ping(params)
      settings = get_config(Rhosync.base_directory)[Rhosync.environment]
      cert_file = File.join(Rhosync.base_directory,settings[:iphonecertfile])
      cert = File.read(cert_file) if File.exists?(cert_file)
    	passphrase = settings[:iphonepassphrase]
    	host = settings[:iphoneserver]
    	port = settings[:iphoneport] 
      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

    # 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

22 entries across 22 versions & 1 rubygems

Version Path
rhosync-2.1.17.beta3 lib/rhosync/ping/apple.rb
rhosync-2.1.17.beta2 lib/rhosync/ping/apple.rb
rhosync-2.1.17.beta1 lib/rhosync/ping/apple.rb
rhosync-2.1.16 lib/rhosync/ping/apple.rb
rhosync-2.1.15 lib/rhosync/ping/apple.rb
rhosync-2.1.14 lib/rhosync/ping/apple.rb
rhosync-2.1.13 lib/rhosync/ping/apple.rb
rhosync-2.1.12 lib/rhosync/ping/apple.rb
rhosync-2.1.11 lib/rhosync/ping/apple.rb
rhosync-2.1.10 lib/rhosync/ping/apple.rb
rhosync-2.1.7 lib/rhosync/ping/apple.rb
rhosync-2.1.6 lib/rhosync/ping/apple.rb
rhosync-2.1.3 lib/rhosync/ping/apple.rb
rhosync-2.1.2 lib/rhosync/ping/apple.rb
rhosync-2.1.1 lib/rhosync/ping/apple.rb
rhosync-2.1.0 lib/rhosync/ping/apple.rb
rhosync-2.1.0.beta.2 lib/rhosync/ping/apple.rb
rhosync-2.1.0.beta.1 lib/rhosync/ping/apple.rb
rhosync-2.0.9 lib/rhosync/ping/apple.rb
rhosync-2.0.8 lib/rhosync/ping/apple.rb