<commit_before># Clip app

Camping.goes :Clip

module Clip
  set :views, File.dirname(__FILE__) + '/views'
end

module Clip::Controllers
  class Index < R '/'
    def get
      @clips = Clip.order('created_at desc').all
      
      render :index
    end
  end
  
  class Clips < R '/clips'
    def post
      @headers['Access-Control-Allow-Origin'] = '*'
      
      @url_regex = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/?)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s\`!()\[\]{};:\'\".,<>?]))/i
      
      @result = {}
      
      if @input.url and @url_regex.match(@input.url) and not Clip.find_by_url(@input.url)
        Clip.create(:url => @input.url)
        
        @result[:success] = true
      else
        @result[:success] = false
      end
      
      @result = @result.to_json
      
      render :clips, :layout => false
    end
  end
end


module Clip::Models
  class Clip < Base
  end
  
  class BasicFields < V 1.0
    def self.up
      create_table Clip.table_name do |t|
        t.text :url
        t.timestamps
      end
    end
    
    def self.down
      drop_table Clip.table_name
    end
  end
end


def Clip.create
  Clip::Models.create_schema
end
<commit_msg>Move Clip validation into model<commit_after># Clip app

Camping.goes :Clip

module Clip
  set :views, File.dirname(__FILE__) + '/views'
end

module Clip::Controllers
  class Index < R '/'
    def get
      @clips = Clip.order('created_at desc').all
      
      render :index
    end
  end
  
  class Clips < R '/clips'
    def post
      @headers['Access-Control-Allow-Origin'] = '*'
      
      @result = {}
      
      clip = Clip.new(:url => @input.url)
      
      if clip.save
        @result[:success] = true
      else
        @result[:success] = false
      end
      
      @result = @result.to_json
      
      render :clips, :layout => false
    end
  end
end


module Clip::Models
  class Clip < Base
    validates :url, :presence => true
    validates :url, :uniqueness => true
    validates :url, :format => { :with => /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/?)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s\`!()\[\]{};:\'\".,<>?]))/i, :message => 'Requires a valid URL' }
    
    def smart_add_url_protocol
      unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
        self.url = 'http://' + self.url
      end
    end
    
    def save
      self.smart_add_url_protocol
      super
    end
  end
  
  class BasicFields < V 1.0
    def self.up
      create_table Clip.table_name do |t|
        t.text :url
        t.timestamps
      end
    end
    
    def self.down
      drop_table Clip.table_name
    end
  end
end


def Clip.create
  Clip::Models.create_schema
end

{'fraction_non_alphanumeric': 0.20346779900920028, 'fraction_numerical': 0.005661712668082095, 'mean_word_length': 2.0202991452991452, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6727399', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1067, 'n_words': 215}