class Apartment::Elevators::Subdomain

Provides a rack based tenant switching solution based on subdomains Assumes that tenant name should match subdomain

Public Class Methods

excluded_subdomains() click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 10
def self.excluded_subdomains
  @excluded_subdomains ||= []
end
excluded_subdomains=(arg) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 14
def self.excluded_subdomains=(arg)
  @excluded_subdomains = arg
end

Public Instance Methods

parse_tenant_name(request) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 18
def parse_tenant_name(request)
  request_subdomain = subdomain(request.host)

  # If the domain acquired is set to be excluded, set the tenant to whatever is currently
  # next in line in the schema search path.
  tenant = if self.class.excluded_subdomains.include?(request_subdomain)
    nil
  else
    request_subdomain
  end

  tenant.presence
end

Protected Instance Methods

domain_valid?(host) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 53
def domain_valid?(host)
  PublicSuffix.valid?(host, ignore_private: true)
end
host_valid?(host) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 45
def host_valid?(host)
  !ip_host?(host) && domain_valid?(host)
end
ip_host?(host) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 49
def ip_host?(host)
  !/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/.match(host).nil?
end
parse_host(host) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 57
def parse_host(host)
  (PublicSuffix.parse(host, ignore_private: true).trd || '').split('.')
end
subdomain(host) click to toggle source

Only care about the first subdomain for the database name

# File lib/apartment/elevators/subdomain.rb, line 37
def subdomain(host)
  subdomains(host).first
end
subdomains(host) click to toggle source
# File lib/apartment/elevators/subdomain.rb, line 41
def subdomains(host)
  host_valid?(host) ? parse_host(host) : []
end