Skip to content

Instantly share code, notes, and snippets.

@scottymac
Forked from sinatra/gist:55898
Created August 16, 2009 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottymac/168741 to your computer and use it in GitHub Desktop.
Save scottymac/168741 to your computer and use it in GitHub Desktop.
#forked to fix error in the original
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))
[]
else
request.host.split('.')[0...(1 - tld_len - 2)]
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