Sha256: 39b9cbb30036a8e305556bdac497a033d10fad822b33b2c7faec4b86cb0284f9

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

module Locomotive
  module Routing
    class DefaultConstraint
   
      def self.matches?(request)
        domain, subdomain = domain_and_subdomain(request)
        subdomain = 'www' if subdomain.blank?
        
        domain == Locomotive.config.default_domain && Locomotive.config.reserved_subdomains.include?(subdomain)
      end
      
      # see actionpack/lib/action_dispatch/http/url.rb for more information              
      def self.domain_and_subdomain(request)
        subdomain = extract_subdomain(request)
        [extract_domain(request), extract_subdomain(request)]
      end
      
      def self.extract_domain(request, tld_length = 1)
        return nil unless named_host?(request.host)
        request.host.split('.').last(1 + tld_length).join('.')
      end
      
      def self.extract_subdomain(request, tld_length = 1)
        subdomains(request, tld_length).join('.')
      end
      
      def self.named_host?(host)
        !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
      end
      
      def self.subdomains(request, tld_length = 1)
        return [] unless named_host?(request.host)
        parts = request.host.split('.')
        parts[0..-(tld_length+2)]
      end
      
    end    
  end  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
locomotive_cms-0.0.3.3 lib/locomotive/routing/default_constraint.rb
locomotive_cms-0.0.3.1 lib/locomotive/routing/default_constraint.rb
locomotive_cms-0.0.2.9 lib/locomotive/routing/default_constraint.rb
locomotive_cms-0.0.2.8 lib/locomotive/routing/default_constraint.rb
locomotive_cms-0.0.2.7 lib/locomotive/routing/default_constraint.rb
locomotive_cms-0.0.2.6 lib/locomotive/routing/default_constraint.rb