Skip to content

Instantly share code, notes, and snippets.

@TXDynamics
Created August 12, 2013 19:40
Show Gist options
  • Save TXDynamics/6214363 to your computer and use it in GitHub Desktop.
Save TXDynamics/6214363 to your computer and use it in GitHub Desktop.
Rails helper that extracts the domain from a URL submitted to the function.
# Use this helping to display the base domain name of a url
# I am using it when displaying web results that are loaded from Social Search
def get_host_without_www(url)
require 'uri'
uri = URI.parse(url)
uri = URI.parse("http://#{url}") if uri.scheme.nil?
host = uri.host.downcase
host.start_with?('www.') ? host[4..-1] : host
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment