class Houston::Client

Attributes

certificate[RW]
feedback_uri[RW]
gateway_uri[RW]
passphrase[RW]
timeout[RW]

Public Class Methods

development() click to toggle source
# 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
new() click to toggle source
# File lib/houston/client.rb, line 27
def initialize
  @gateway_uri = ENV['APN_GATEWAY_URI']
  @feedback_uri = ENV['APN_FEEDBACK_URI']
  @certificate = certificate_data
  @passphrase = ENV['APN_CERTIFICATE_PASSPHRASE']
  @timeout = Float(ENV['APN_TIMEOUT'] || 0.5)
end
production() click to toggle source
# 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

Public Instance Methods

certificate_data() click to toggle source
# File lib/houston/client.rb, line 84
def certificate_data
  if ENV['APN_CERTIFICATE']
    File.read(ENV['APN_CERTIFICATE'])
  elsif ENV['APN_CERTIFICATE_DATA']
    ENV['APN_CERTIFICATE_DATA']
  end
end
devices() click to toggle source
# File lib/houston/client.rb, line 80
def devices
  unregistered_devices.collect { |device| device[:token] }
end
push(*notifications) click to toggle source
# File lib/houston/client.rb, line 35
def push(*notifications)
  return if notifications.empty?

  notifications.flatten!

  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.message)
      notification.mark_as_sent!

      read_socket, _write_socket = IO.select([ssl], [ssl], [ssl], nil)
      if (read_socket && read_socket[0])
        if error = connection.read(6)
          _command, status, index = error.unpack('ccN')
          notification.apns_error_code = status
          notification.mark_as_unsent!
        end
      end
    end
  end
end
unregistered_devices() click to toggle source
# File lib/houston/client.rb, line 65
def unregistered_devices
  devices = []

  Connection.open(@feedback_uri, @certificate, @passphrase) do |connection|
    while line = connection.read(38)
      feedback = line.unpack('N1n1H140')
      timestamp = feedback[0]
      token = feedback[2].scan(/.{0,8}/).join(' ').strip
      devices << { token: token, timestamp: timestamp } if token && timestamp
    end
  end

  devices
end