Skip to content

Instantly share code, notes, and snippets.

@mipearson
Created December 13, 2011 06:32
Show Gist options
  • Save mipearson/1470906 to your computer and use it in GitHub Desktop.
Save mipearson/1470906 to your computer and use it in GitHub Desktop.
def subdomain
@subdomain ||= begin
parts = begin
if @request.host.ends_with?('.au')
@request.subdomains(2)
else
@request.subdomains(1)
end
end
parts.first == 'www' ? parts[1] : parts[0]
end
end
@mipearson
Copy link
Author

(yes, it has unit tests)

@deepfryed
Copy link

use a return-if construct, it's not as bad as it sounds

def subdomain
  return @subdomain if @subdomain
  parts = @request.subdomains(@request.host[-3, 3] == '.au' ? 2 : 1)
  @subdomain = parts.first == 'www' ? parts[1] : parts[0]
end

or use 'public_suffix_service' gem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment