<commit_before>require 'uri'

URI_LENGTH = 7

class Url < ActiveRecord::Base
  before_save :generate_short_uri
  validates :redirect_url, presence: true, format: URI::regexp(%w(http https))

  def url_visit
    self.click_count += 1
    self.save
  end

  private

  def generate_short_uri
    unless self.short_uri
      while self.short_uri.nil? || Url.where(short_uri: self.short_uri).count == 1
        self.short_uri = [*'0'..'9',*'A'..'Z',*'a'..'z'].sample(URI_LENGTH).join
      end
    end
  end
end<commit_msg>Only unique urls are accepted<commit_after>require 'uri'

URI_LENGTH = 7

class Url < ActiveRecord::Base
  before_save :generate_short_uri
  validates :redirect_url, uniqueness: true, presence: true, format: URI::regexp(%w(http https))

  def url_visit
    self.click_count += 1
    self.save
  end

  private

  def generate_short_uri
    unless self.short_uri
      while self.short_uri.nil? || Url.where(short_uri: self.short_uri).count == 1
        self.short_uri = [*'0'..'9',*'A'..'Z',*'a'..'z'].sample(URI_LENGTH).join
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.13432835820895522, 'fraction_numerical': 0.009328358208955223, 'mean_word_length': 3.5274261603375527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6767921', 'n_tokens_mistral': 399, 'n_tokens_neox': 383, 'n_words': 81}