Browse Source

tld validation

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
62c3a0fed5
  1. 10
      app/models/domain.rb
  2. 26
      app/models/tld.rb
  3. 5189
      db/data/tlds.txt
  4. 8
      spec/models/domain_spec.rb
  5. 22
      spec/models/tld_spec.rb

10
app/models/domain.rb

@ -45,15 +45,9 @@ class Domain < ActiveRecord::Base
end
end
validate :domain_ownership, :on => :create
validate :domain_ownership
def domain_ownership # at least one NS is among ours
ns = Settings.resolv.sample
Resolv::DNS.open(:nameserver => ns) do |dns|
ress = dns.getresources name, Resolv::DNS::Resource::IN::NS
if (Settings.ns & ress.map{|r| r.name.to_s}).blank?
errors.add :base, "You must delegate #{name} to one of our NS servers before adding it"
end
end
errors[:name] = "cannot be a TLD or a reserved domain" if Tld.include?(name)
end
def slave?; self.type == 'SLAVE' end

26
app/models/tld.rb

@ -0,0 +1,26 @@
class Tld
@@lines = []
cattr_accessor :lines
File.open(Rails.root.join('db', 'data', 'tlds.txt').to_s) do |fd|
fd.each do |line|
unless line[0..1] == '//' || line =~ /^\s$/ # neither comment neither blank
@@lines << case line[0]
when '*' then /^[\w-]+#{Regexp.escape(line[1..-1].chomp)}$/i # wildcard
when '!' then /^#{Regexp.escape(line[1..-1].chomp)}$/i # domain
else /^#{Regexp.escape(line[0..-1].chomp)}$/i # tld
end
end
end
end
def self.include?(name)
for line in @@lines
return true if name =~ line
end
return false
end
end

5189
db/data/tlds.txt

File diff suppressed because it is too large Load Diff

8
spec/models/domain_spec.rb

@ -46,4 +46,12 @@ describe Domain do
domain.should be_valid
end
it "validates ownership" do
domain.name = 'co.uk'
domain.should have(1).errors_on(:name)
domain.name = 'clyfe.ro'
domain.should be_valid
end
end

22
spec/models/tld_spec.rb

@ -0,0 +1,22 @@
require 'spec_helper'
describe Tld do
its ".lines" do
Tld.lines.should be_present
end
its ".include" do
Tld.include?('ro').should be_true
Tld.include?('lt').should be_true
Tld.include?('co.uk').should be_true
Tld.include?('com.au').should be_true
Tld.include?('ANYTHING.ar').should be_true
Tld.include?('pref.fukuoka.jp').should be_true
Tld.include?('any.toyama.jp').should be_true
Tld.include?('clyfe.ro').should be_false
Tld.include?('clyfe.zooz.lt').should be_false
end
end
Loading…
Cancel
Save