class ApnDevice < ActiveRecord::Base
  belongs_to :user
  has_many :apn_notifications, foreign_key: "device_id", dependent: :destroy

  scope :by_id, -> { order(:id) }
  
  validates_uniqueness_of :token
  validates_format_of :token, :with => /\A[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\s[a-z0-9]{8}\z/
  
  before_create :set_last_registered_at
  
  # The <tt>feedback_at</tt> accessor is set when the 
  # device is marked as potentially disconnected from your
  # application by Apple.
  attr_accessor :feedback_at
  
  # Stores the token (Apple's device ID) of the iPhone (device).
  # 
  # If the token comes in like this:
  #  '<5gxadhy6 6zmtxfl6 5zpbcxmw ez3w7ksf qscpr55t trknkzap 7yyt45sc g6jrw7qz>'
  # Then the '<' and '>' will be stripped off.
  def token=(token)
    res = token.scan(/\<(.+)\>/).first
    unless res.nil? || res.empty?
      token = res.first
    end
    write_attribute('token', token)
  end
  
  # Returns the hexadecimal representation of the device's token.
  def to_hexa
    [self.token.delete(' ')].pack('H*')
  end
  
  def set_last_registered_at
    self.last_registered_at = Time.now #if self.last_registered_at.nil?
  end
end

# == Schema Information
#
# Table name: apn_devices
#
#  id                 :integer         not null, primary key
#  token              :string(255)     not null
#  last_registered_at :datetime
#  integer            :integer
#  user_id            :integer         not null
#  created_at         :datetime
#  updated_at         :datetime
#

