Skip to content

Instantly share code, notes, and snippets.

@jcotzin
Created June 26, 2016 19:08
Show Gist options
  • Save jcotzin/ad4142baba49a8c14bb803827a96dd45 to your computer and use it in GitHub Desktop.
Save jcotzin/ad4142baba49a8c14bb803827a96dd45 to your computer and use it in GitHub Desktop.
class UrlParser
def initialize(new_url)
@new_url=new_url
end
def scheme
@new_url.split(':')[0]
end
def domain
@new_url.split('/')[2].split(':')[0]
end
def port
port = @new_url.split('/')[2].split(':')[1]
if !port && scheme == "http"
"80"
elsif !port && scheme == "https"
"443"
else
port
end
end
def path
path = @new_url.split('/')[3].split('?')[0]
if path == ""
nil
else
path
end
end
def query_string
query_string = @new_url.split('?')[1].split('#')[0].split('&')
query_string.map!{|i| i.split('=')}
query_string.flatten!
query_string = Hash[*query_string]
end
def fragment_id
@new_url.split('#').last
end
end
@new_url = UrlParser.new "http://www.google.com:60/search?q=cat&name=Tim#img=FunnyCat"
@jciancio
Copy link

Hey Great job! I like the indentation and the methods seem pretty concise. I would recommend commenting some of the methods, like query_string because it's hard to follow what's going on. Also, most importantly, we don't want to name variables the same as a method, like path in the path method. It can get really confusing to know if you're calling the method or the variable, and you might end up in an infinite loop without realizing it.

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