class Apartment::Elevators::Subdomain

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

def call(env)

def call(env)
  host = Rack::Request.new(env).host
  database = subdomain(host)
  Apartment::Database.switch database if database
  @app.call(env)
end

def initialize(app)

def initialize(app)
  @app = app
end

def named_host?(host)

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

def subdomain(host)

Only care about the first subdomain for the database name
def subdomain(host)
  subdomains(host).first
end

def subdomains(host, tld_length = 1)

Assuming tld_length of 1, might need to make this configurable in Apartment in the future for things like .co.uk
def subdomains(host, tld_length = 1)
  return [] unless named_host?(host)
  host.split('.')[0..-(tld_length + 2)]
end