Class: Locomotive::Routing::DefaultConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/routing/default_constraint.rb

Class Method Summary collapse

Class Method Details

.domain_and_subdomain(request) ⇒ Object

see actionpack/lib/action_dispatch/http/url.rb for more information



17
18
19
# File 'lib/locomotive/routing/default_constraint.rb', line 17

def self.domain_and_subdomain(request)
  [extract_domain(request), extract_subdomain(request)]
end

.extract_domain(request, tld_length = 1) ⇒ Object



21
22
23
24
# File 'lib/locomotive/routing/default_constraint.rb', line 21

def self.extract_domain(request, tld_length = 1)
  return nil unless named_host?(request.host)
  request.host.split('.').last(1 + tld_length).join('.')
end

.extract_subdomain(request, tld_length = 1) ⇒ Object



26
27
28
# File 'lib/locomotive/routing/default_constraint.rb', line 26

def self.extract_subdomain(request, tld_length = 1)
  subdomains(request, tld_length).join('.')
end

.matches?(request) ⇒ Boolean

Returns:



5
6
7
8
9
10
11
12
13
14
# File 'lib/locomotive/routing/default_constraint.rb', line 5

def self.matches?(request)
  if Locomotive.config.multi_sites?
    domain, subdomain = domain_and_subdomain(request)
    subdomain = 'www' if subdomain.blank?

    domain == Locomotive.config.domain && Locomotive.config.reserved_subdomains.include?(subdomain)
  else
    false
  end
end

.named_host?(host) ⇒ Boolean

Returns:



30
31
32
# File 'lib/locomotive/routing/default_constraint.rb', line 30

def self.named_host?(host)
  !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end

.subdomains(request, tld_length = 1) ⇒ Object



34
35
36
37
38
# File 'lib/locomotive/routing/default_constraint.rb', line 34

def self.subdomains(request, tld_length = 1)
  return [] unless named_host?(request.host)
  parts = request.host.split('.')
  parts[0..-(tld_length+2)]
end