Nicolae Claudius
13 years ago
5 changed files with 5247 additions and 8 deletions
@ -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 |
||||
|
@ -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…
Reference in new issue