Class: Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/dark_domains/models/domain.rb

Constant Summary collapse

DOMAIN_URI =
/^\w[\w\.\-\_]+\.[\w\.]{2,7}$/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain) ⇒ Domain

Create a domain instance



42
43
44
# File 'lib/dark_domains/models/domain.rb', line 42

def initialize(domain)
  self.domain = domain
end

Instance Attribute Details

#domainObject

Keep it simple



10
11
12
# File 'lib/dark_domains/models/domain.rb', line 10

def domain
  @domain
end

Class Method Details

.ban!(domain) ⇒ Object

Ban a domain



26
27
28
# File 'lib/dark_domains/models/domain.rb', line 26

def self.ban!(domain)
  banned_domains[normalized_domain(domain)] = true
end

.banned?(suspect_domain) ⇒ Boolean

Return true if this is a known spam domain, false otherwise

Returns:

  • (Boolean)


21
22
23
# File 'lib/dark_domains/models/domain.rb', line 21

def self.banned?(suspect_domain)
  banned_domains[normalized_domain(suspect_domain)].eql?(true)
end

.banned_domainsObject

Return all of the banned domains



16
17
18
# File 'lib/dark_domains/models/domain.rb', line 16

def self.banned_domains
  @@banned_domains ||= {}
end

.default_blacklist_pathObject

Path to the default blacklist



79
80
81
# File 'lib/dark_domains/models/domain.rb', line 79

def self.default_blacklist_path
  File.expand_path(File.join(__FILE__, "..", "..", "data", "blacklist.txt"))
end

.load_blacklist(absolute_path = nil) ⇒ Object

TODO:

make this pull from a dynamic source on the net

Load existing known bad spammer domains, returns number of domains loaded



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dark_domains/models/domain.rb', line 61

def self.load_blacklist(absolute_path = nil)
  absolute_path = default_blacklist_path if !absolute_path
  
  raise "unable to find blacklist data file: #{ absolute_path }" \
    unless File.exists?(absolute_path)
  
  # keep for the total we'll return
  existing_size = banned_domains.size
  
  # Grab the domains, squash the newlines, ignore comments and blank lines
  File.readlines(absolute_path).each{ |d| Domain.ban!(d.chomp) unless d =~ /(^\#|^\s+$)/ }

  puts "loaded #{ banned_domains.size } banned domains." if $DEBUG
  
  return banned_domains.size - existing_size
end

.normalized_domain(domain) ⇒ Object

Return a normalized URL



31
32
33
34
35
36
# File 'lib/dark_domains/models/domain.rb', line 31

def self.normalized_domain(domain)
  # Strip www subdomains, but leave others
  domain = domain.gsub(/www\./, '')
  # Support protocols/ports/etc
  domain =~ DOMAIN_URI ? domain : URI.parse(domain).host
end

Instance Method Details

#ban!Object

Ban this specific domain instance



52
53
54
# File 'lib/dark_domains/models/domain.rb', line 52

def ban!
  Domain.ban!(domain)
end

#banned?Boolean

Return true if we are a banned domain

Returns:

  • (Boolean)


47
48
49
# File 'lib/dark_domains/models/domain.rb', line 47

def banned?
  Domain.banned?(domain)
end