Class: Domain
- Inherits:
-
Object
- Object
- Domain
- Defined in:
- lib/dark_domains/models/domain.rb
Constant Summary collapse
- DOMAIN_URI =
/^\w[\w\.\-\_]+\.[\w\.]{2,7}$/i.freeze
Instance Attribute Summary collapse
-
#domain ⇒ Object
Keep it simple.
Class Method Summary collapse
-
.ban!(domain) ⇒ Object
Ban a domain.
-
.banned?(suspect_domain) ⇒ Boolean
Return true if this is a known spam domain, false otherwise.
-
.banned_domains ⇒ Object
Return all of the banned domains.
-
.default_blacklist_path ⇒ Object
Path to the default blacklist.
-
.load_blacklist(absolute_path = nil) ⇒ Object
Load existing known bad spammer domains, returns number of domains loaded.
-
.normalized_domain(domain) ⇒ Object
Return a normalized URL.
Instance Method Summary collapse
-
#ban! ⇒ Object
Ban this specific domain instance.
-
#banned? ⇒ Boolean
Return true if we are a banned domain.
-
#initialize(domain) ⇒ Domain
constructor
Create a domain instance.
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
#domain ⇒ Object
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
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_domains ⇒ Object
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_path ⇒ Object
Path to the default blacklist
79 80 81 |
# File 'lib/dark_domains/models/domain.rb', line 79 def self.default_blacklist_path File.(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 |