Class: Houston::Client
- Inherits:
-
Object
- Object
- Houston::Client
- Defined in:
- lib/houston/client.rb
Instance Attribute Summary collapse
-
#certificate ⇒ Object
Returns the value of attribute certificate.
-
#feedback_uri ⇒ Object
Returns the value of attribute feedback_uri.
-
#gateway_uri ⇒ Object
Returns the value of attribute gateway_uri.
-
#passphrase ⇒ Object
Returns the value of attribute passphrase.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #devices ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #push(*notifications) ⇒ Object
- #unregistered_devices ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
27 28 29 30 31 32 33 |
# File 'lib/houston/client.rb', line 27 def initialize @gateway_uri = ENV['APN_GATEWAY_URI'] @feedback_uri = ENV['APN_FEEDBACK_URI'] @certificate = ENV['APN_CERTIFICATE'] @passphrase = ENV['APN_CERTIFICATE_PASSPHRASE'] @timeout = Float(ENV['APN_TIMEOUT'] || 0.5) end |
Instance Attribute Details
#certificate ⇒ Object
Returns the value of attribute certificate.
9 10 11 |
# File 'lib/houston/client.rb', line 9 def certificate @certificate end |
#feedback_uri ⇒ Object
Returns the value of attribute feedback_uri.
9 10 11 |
# File 'lib/houston/client.rb', line 9 def feedback_uri @feedback_uri end |
#gateway_uri ⇒ Object
Returns the value of attribute gateway_uri.
9 10 11 |
# File 'lib/houston/client.rb', line 9 def gateway_uri @gateway_uri end |
#passphrase ⇒ Object
Returns the value of attribute passphrase.
9 10 11 |
# File 'lib/houston/client.rb', line 9 def passphrase @passphrase end |
#timeout ⇒ Object
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/houston/client.rb', line 9 def timeout @timeout end |
Class Method Details
.development ⇒ Object
12 13 14 15 16 17 |
# File 'lib/houston/client.rb', line 12 def development client = self.new client.gateway_uri = APPLE_DEVELOPMENT_GATEWAY_URI client.feedback_uri = APPLE_DEVELOPMENT_FEEDBACK_URI client end |
.production ⇒ Object
19 20 21 22 23 24 |
# File 'lib/houston/client.rb', line 19 def production client = self.new client.gateway_uri = APPLE_PRODUCTION_GATEWAY_URI client.feedback_uri = APPLE_PRODUCTION_FEEDBACK_URI client end |
Instance Method Details
#devices ⇒ Object
97 98 99 |
# File 'lib/houston/client.rb', line 97 def devices unregistered_devices.collect{|device| device[:token]} end |
#push(*notifications) ⇒ Object
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 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/houston/client.rb', line 35 def push(*notifications) return if notifications.empty? notifications.flatten! error = nil Connection.open(@gateway_uri, @certificate, @passphrase) do |connection| ssl = connection.ssl notifications.each_with_index do |notification, index| next unless notification.kind_of?(Notification) next if notification.sent? next unless notification.valid? notification.id = index connection.write(notification.) notification.mark_as_sent! break if notifications.count == 1 || notification == notifications.last read_socket, write_socket = IO.select([ssl], [ssl], [ssl], nil) if (read_socket && read_socket[0]) error = connection.read(6) break end end return if notifications.count == 1 unless error read_socket, write_socket = IO.select([ssl], nil, [ssl], timeout) if (read_socket && read_socket[0]) error = connection.read(6) end end end if error command, status, index = error.unpack("ccN") notifications[index].apns_error_code = status notifications.slice!(0..index) notifications.each(&:mark_as_unsent!) push(*notifications) end end |
#unregistered_devices ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/houston/client.rb', line 82 def unregistered_devices devices = [] Connection.open(@feedback_uri, @certificate, @passphrase) do |connection| while line = connection.read(38) feedback = line.unpack('N1n1H140') = feedback[0] token = feedback[2].scan(/.{0,8}/).join(' ').strip devices << {token: token, timestamp: } if token && end end devices end |