public
Description: Ruby Apple Push Feedback Service
Homepage:
Clone URL: git://github.com/jdg/Feedback.git
Feedback / feedback.rb
100644 68 lines (53 sloc) 1.523 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## Feedback
##
## Requires json (sudo gem install json)
## Usage:
## devices = Feedback.receive
##
## Copyright (c) 2009 Jonathan George (jonathan@jdg.net)
##
## MIT Licensed
 
#require 'rubygems'
 
require 'socket'
require 'openssl'
require 'json'
 
class Feedback
  HOST = 'feedback.sandbox.push.apple.com'
  PATH = '/'
  PORT = 2196
  CERT = File.read(File.join(RAILS_ROOT, "config", "apple_push_notification.pem")) if File.exists?(File.join(RAILS_ROOT, "config", "apple_push_notification.pem"))
  PASSPHRASE = ''
  USERAGENT = 'Ruby/Feedback.rb'
 
  attr_accessor :sound, :badge, :alert, :app_data
  attr_reader :device_token
 
  def initialize
 
  end
 
  def self.receive
    socket, ssl = ssl_connection
 
    devices = []
 
    while data = socket.read(76)
      next if data.size < 76
      timestamp, token_length, device_token = data.unpack('N1n1H140')
      devices << { :timestamp => timestamp, :device_token => device_token}
    end
 
    puts "Devices: " if devices.any?
    devices.each do |device|
      puts "\t#{device[:device_token]}"
    end
 
    ssl.close
    socket.close
    return devices
  rescue SocketError => error
    raise "Error while receieving feedback: #{error}"
 
  end
 
  def self.ssl_connection
    ctx = OpenSSL::SSL::SSLContext.new
    ctx.key = OpenSSL::PKey::RSA.new(CERT, PASSPHRASE)
    ctx.cert = OpenSSL::X509::Certificate.new(CERT)
 
    sock = TCPSocket.new(HOST, PORT)
    ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
    ssl.sync = true
    ssl.connect
 
    return sock, ssl
  end
end