Class: EmailAddress::DomainMatcher

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_nameObject (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_nameObject (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_addressObject (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

#partsObject (readonly)

Returns the value of attribute parts.



12
13
14
# File 'lib/email_address/domain_matcher.rb', line 12

def parts
  @parts
end

#registration_nameObject (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

#subdomainsObject (readonly)

Returns the value of attribute subdomains.



12
13
14
# File 'lib/email_address/domain_matcher.rb', line 12

def subdomains
  @subdomains
end

#tldObject (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

Returns:

  • (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

Returns:

  • (Boolean)


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”?

Returns:

  • (Boolean)


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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


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

Returns:

  • (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?

Returns:

  • (Boolean)


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