Sha256: cc4f42e2bfff9cad271bae518790091b6d89ad8bcdf79e51256501c807fe21d0

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'resolv'
require 'uri'
class AccountDomain < ActiveRecord::Base
  belongs_to :account
  validates :url, :account_id, :presence => true
  validate :validate_pattern, :validate_blacklist

  scope :verified, where(:verified => true)

  def normalize_host
    begin
      normalized_url = url
      normalized_url = normalized_url.gsub('www.','')
      normalized_url = normalized_url.gsub('http://','')
      normalized_url = normalized_url.gsub('https://','')
      normalized_url = 'http://' + normalized_url
      URI.parse(normalized_url).host
    rescue
      nil
    end
  end

  def calculate_token
    url = normalize_host
    Digest::SHA1.hexdigest( "#{id}#{url}" )[8..24]
  end

  def verify
    url = normalize_host
    ref_id = self.calculate_token
    checked=false
    begin
      Socket.gethostbyname( "#{ref_id}.#{url}" )
      checked=true
    rescue SocketError
    end
    begin
      response = Net::HTTP.start(url, 80) {|http| http.head("/#{ref_id}.html") }
      checked = true if response.code == "200"
    rescue
    end
    AccountDomain.where(:url => self.url).update_all(:verified => false) if checked == true
    update_attribute(:verified, checked)
    checked
  end

  def set_primary
    AccountDomain.where(:account_id => account_id).update_all(:primary => false)
    update_attribute(:primary, true)
  end

  private

  def validate_pattern
    if url
      errors.add(:url, "Invalid Pattern") unless url.match /^([a-z0-9]*\.)*[a-z0-9]*$/
    end
  end

  def validate_blacklist
    if url
      IuguSDK::custom_domain_invalid_hosts.each do |invalid_host|
        errors.add(:url, "Domain on Blacklist") if url == invalid_host
      end
    end
  end
  
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iugusdk-1.0.0.alpha.1 app/models/account_domain.rb
iugusdk-1.0.0.alpha.0 app/models/account_domain.rb