Class: EmailAddress::DomainMatcher
- Inherits:
-
Object
- Object
- EmailAddress::DomainMatcher
- Defined in:
- lib/email_address/domain_matcher.rb
Overview
DomainMatcher - Matches a domain to a set of patterns
Match Patterns
hostname sub.domain.tld
domain domain.tld
registration domain
tld .tld, .domain.tld
Instance Attribute Summary collapse
-
#domain_name ⇒ Object
readonly
Returns the value of attribute domain_name.
-
#host_name ⇒ Object
readonly
Returns the value of attribute host_name.
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#registration_name ⇒ Object
readonly
Returns the value of attribute registration_name.
-
#subdomains ⇒ Object
readonly
Returns the value of attribute subdomains.
-
#tld ⇒ Object
readonly
Returns the value of attribute tld.
Class Method Summary collapse
Instance Method Summary collapse
-
#domain_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “example.com” domain name.
-
#initialize(host_name, rule = nil) ⇒ DomainMatcher
constructor
A new instance of DomainMatcher.
-
#ip_cidr_matches?(rule) ⇒ Boolean
Does an IP of mail exchanger for “sub.example.com” match “xxx.xx.xx.xx/xx”?.
- #list_matches?(list) ⇒ Boolean
- #matches?(rule = nil) ⇒ Boolean
-
#registration_name_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “example” registration name.
- #rule_matches?(rule) ⇒ Boolean
-
#tld_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “.com” and “.example.com” top level names?.
Constructor Details
#initialize(host_name, rule = nil) ⇒ DomainMatcher
Returns a new instance of DomainMatcher.
19 20 21 22 23 24 |
# File 'lib/email_address/domain_matcher.rb', line 19 def initialize(host_name, rule=nil) @host_name = host_name.downcase @host = EmailAddress::Host.new(@host_name) @rule = rule matches? end |
Instance Attribute Details
#domain_name ⇒ Object (readonly)
Returns the value of attribute domain_name.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def domain_name @domain_name end |
#host_name ⇒ Object (readonly)
Returns the value of attribute host_name.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def host_name @host_name end |
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def ip_address @ip_address end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def parts @parts end |
#registration_name ⇒ Object (readonly)
Returns the value of attribute registration_name.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def registration_name @registration_name end |
#subdomains ⇒ Object (readonly)
Returns the value of attribute subdomains.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def subdomains @subdomains end |
#tld ⇒ Object (readonly)
Returns the value of attribute tld.
12 13 14 |
# File 'lib/email_address/domain_matcher.rb', line 12 def tld @tld end |
Class Method Details
.matches?(domain, rule) ⇒ Boolean
15 16 17 |
# File 'lib/email_address/domain_matcher.rb', line 15 def self.matches?(domain, rule) DomainMatcher.new(domain, rule).matches? end |
Instance Method Details
#domain_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “example.com” domain name
55 56 57 |
# File 'lib/email_address/domain_matcher.rb', line 55 def domain_matches?(rule) rule.match(/\A[^\.]+\.[^\.]+\z/) && @host.domain_name == rule.downcase ? true : false end |
#ip_cidr_matches?(rule) ⇒ Boolean
Does an IP of mail exchanger for “sub.example.com” match “xxx.xx.xx.xx/xx”?
67 68 69 70 |
# File 'lib/email_address/domain_matcher.rb', line 67 def ip_cidr_matches?(rule) return false unless rule.match(/\A\d.+\/\d+\z/) && @host.exchanger @host.exchanger.in_cidr?(r) ? true : false end |
#list_matches?(list) ⇒ Boolean
44 45 46 47 |
# File 'lib/email_address/domain_matcher.rb', line 44 def list_matches?(list) list.each {|rule| return true if rule_matches?(rule) } false end |
#matches?(rule = nil) ⇒ Boolean
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/email_address/domain_matcher.rb', line 26 def matches?(rule=nil) rule ||= @rule case rule when String rule_matches?(rule) when Array list_matches?(rule) else false end end |
#registration_name_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “example” registration name
50 51 52 |
# File 'lib/email_address/domain_matcher.rb', line 50 def registration_name_matches?(rule) rule.match(/\A(\w+)\z/) && @host.registration_name == rule.downcase ? true : false end |
#rule_matches?(rule) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/email_address/domain_matcher.rb', line 38 def rule_matches?(rule) rule.downcase! @host_name == rule || registration_name_matches?(rule) || domain_matches?(rule) || tld_matches?(rule) end |
#tld_matches?(rule) ⇒ Boolean
Does “sub.example.com” match “.com” and “.example.com” top level names?
60 61 62 63 64 |
# File 'lib/email_address/domain_matcher.rb', line 60 def tld_matches?(rule) rule.match(/\A\..+\z/) && ( @host_name[-rule.size, rule.size] == rule.downcase || ".#{@host_name}" == rule) \ ? true : false end |