domain_switcher 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,26 @@ module DomainSwitcher
12
12
  end
13
13
 
14
14
  def scan_domain(host)
15
- @@domain_cache[host]
15
+ @@domain_cache[host] || deep_scan(host)
16
+ end
17
+
18
+ # Will not match exactly the domain but will try to match the closest one
19
+ # For example => fr.staging.domain.com will match staging.domain.com if declared
20
+ def deep_scan(host)
21
+ return nil if !host
22
+
23
+ dom = nil
24
+ nbdot = nil
25
+ @domains.each do |domain|
26
+ if dot = host.rindex(domain.domain)
27
+ if !nbdot or dot < nbdot
28
+ nbdot = dot
29
+ dom = domain
30
+ end
31
+ end
32
+ end
33
+ @@domain_cache[host] = dom if dom
34
+ return dom
16
35
  end
17
36
 
18
37
  end
@@ -23,6 +23,7 @@ website2:
23
23
  - www.website2.com:
24
24
  default: true
25
25
  - fr.website2.com
26
+ - staging.website2.com
26
27
  lang: fr
27
28
  false_meth: false
28
29
  env_var: 'test'
@@ -46,8 +46,8 @@ describe :config_loader do
46
46
  end
47
47
 
48
48
  it "load correctly all domains" do
49
- @config.domains.size.should == 6
50
- @config.domains.collect(&:domain).uniq.size.should == 6
49
+ @config.domains.size.should == 7
50
+ @config.domains.collect(&:domain).uniq.size.should == 7
51
51
  @config.domains.first.domain.should == 'www.website1.com'
52
52
  end
53
53
 
@@ -11,6 +11,11 @@ describe :scanner do
11
11
  @scanner.scan_domain(nil).should == nil
12
12
  @scanner.scan_domain('something_not_existing').should == nil
13
13
  end
14
+
15
+ it "handle deep scanning" do
16
+ @scanner.scan_domain('staging.website2.com').domain.should == 'staging.website2.com'
17
+ @scanner.scan_domain('fr.staging.website2.com').domain.should == 'staging.website2.com'
18
+ end
14
19
 
15
20
  it "return correct domain when existing" do
16
21
  @scanner.scan_domain('www.website2.com').should_not be_nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domain_switcher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 5
10
- version: 0.1.5
9
+ - 6
10
+ version: 0.1.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Guillaume Luccisano