Class: Rack::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/magis/base.rb

Instance Method Summary collapse

Instance Method Details

#subdomains(tld_len = 1) ⇒ Object

we set tld_len to 1, use 2 for co.uk or similar



3
4
5
6
7
8
9
10
11
# File 'lib/magis/base.rb', line 3

def subdomains(tld_len=1) # we set tld_len to 1, use 2 for co.uk or similar
  # cache the result so we only compute it once.
  @env['rack.env.subdomains'] ||= lambda {
    # check if the current host is an IP address, if so return an empty array
    return [] if (host.nil? ||
                  /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
    host.split('.')[0...(1 - tld_len - 2)] # pull everything except the TLD
  }.call
end