Skip to content

Instantly share code, notes, and snippets.

@sinatra
Created February 1, 2009 16:47
Show Gist options
  • Save sinatra/55898 to your computer and use it in GitHub Desktop.
Save sinatra/55898 to your computer and use it in GitHub Desktop.
# This method is heavily adapted from the Rails method of determining the subdomain.
require 'rubygems'
require 'sinatra'
helpers do
def subdomains(tld_len=1) # we set tld_len to 1, use 2 for co.uk or similar
@subdomains ||= if (request.host.nil? ||
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(request.host))
request.host.split('.')[0...(1 - tld_len - 2)]
else
[]
end
end
end
get '/' do
subdomains.join("<br/>")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment