require "uri"
 
url = "http://foo.example.com:8080/foo/bar?x=y"
baseurl = URI.join(url, "/").to_s
# http://foo.example.com:8080/