Class: APN::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apn/client.rb

Constant Summary collapse

DEFAULTS =
{port: 2195, host: "gateway.push.apple.com"}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
# File 'lib/apn/client.rb', line 6

def initialize(options = {})
  options = DEFAULTS.merge options.reject{|k,v| v.nil?}
  @apn_cert, @cert_pass = options[:certificate], options[:password]
  @host, @port = options[:host], options[:port]
  self
end

Instance Method Details

#feedbackObject



32
33
34
35
36
37
# File 'lib/apn/client.rb', line 32

def feedback
  if bunch = socket.read(38)
    f = bunch.strip.unpack('N1n1H140')
    APN::FeedbackItem.new(Time.at(f[0]), f[2])
  end
end

#push(message) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/apn/client.rb', line 13

def push(message)
  socket.write(message.to_s)
  socket.flush

  if IO.select([socket], nil, nil, 1) && error = socket.read(6)
    error = error.unpack("ccN")
    APN.log(:error, "Error on message: #{error}")
    return false
  end

  APN.log(:debug, "Message sent.")
  true
rescue OpenSSL::SSL::SSLError, Errno::EPIPE, Errno::ETIMEDOUT => e
  APN.log(:error, "[##{self.object_id}] Exception occurred: #{e.inspect}, socket state: #{socket.inspect}")
  reset_socket
  APN.log(:debug, "[##{self.object_id}] Socket reestablished, socket state: #{socket.inspect}")
  retry
end

#socketObject



39
40
41
# File 'lib/apn/client.rb', line 39

def socket
  @socket ||= setup_socket
end