Instead of rolling your own, you should be able to use URI to parse this automatically.  Check out: http://www.ruby-doc.org/stdlib-2.0/libdoc/uri/rdoc/URI.html