Class: DomainSwitcher::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/domain_switcher/scanner.rb

Constant Summary collapse

@@domain_cache =
{}

Instance Method Summary collapse

Constructor Details

#initialize(domains) ⇒ Scanner

Returns a new instance of Scanner.



6
7
8
9
10
11
12
# File 'lib/domain_switcher/scanner.rb', line 6

def initialize(domains)
  @domains = domains

  domains.each do |d|
    @@domain_cache[d.domain] = d
  end
end

Instance Method Details

#deep_scan(host) ⇒ Object

Will not match exactly the domain but will try to match the closest one For example => fr.staging.domain.com will match staging.domain.com if declared



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/domain_switcher/scanner.rb', line 20

def deep_scan(host)
  return nil if !host

  dom = nil
  nbdot = nil
  @domains.each do |domain|
    if dot = host.rindex(domain.domain)
      if !nbdot or dot < nbdot
        nbdot = dot
        dom = domain
      end
    end
  end
  @@domain_cache[host] = dom if dom
  return dom
end

#scan_domain(host) ⇒ Object



14
15
16
# File 'lib/domain_switcher/scanner.rb', line 14

def scan_domain(host)
  @@domain_cache[host] || deep_scan(host)
end