Skip to content

Instantly share code, notes, and snippets.

@leopolicastro
Created December 2, 2018 16:39
Show Gist options
  • Save leopolicastro/eeac2c91428ebecc3368b2b156c5ee0b to your computer and use it in GitHub Desktop.
Save leopolicastro/eeac2c91428ebecc3368b2b156c5ee0b to your computer and use it in GitHub Desktop.
url_parser
class UrlParser
def initialize(new_url)
@new_url = new_url
end
def scheme
scheme = @new_url.split('://')[0]
end
def domain
# @domain = @new_url.split('//')[1].split('/')[0].split(":")[0]
@new_url.split(/[\/:]/)[3]
end
def port
# @port = @new_url.split(':')[2].split('/')[0]
scheme = @new_url.split('://')[0]
port = @new_url.split(/[\/:]/)[4]
if port.to_i == 0 && scheme == "http"
port = "80"
elsif port.to_i == 0 && scheme == "https"
port = "443"
end
p port
end
# if @port = @new_url.split(/[\/:]/)[4]
# return @port
# else @scheme == 'http' && @port == nil
# @port = @insecure_url.gsub('com/', 'com/:80/')
# end
# end
def path
@path = @new_url.split('//')[1].split('/')[1].split("?")[0]
@path == "" ? nil : @path
end
def query_string
query_string = @new_url.split('?')[1].split('=').join(" ").split('#')[0].split('&').join(" ").split(" ")
Hash[*query_string]
end
def fragment_id
fragment_id = @new_url.split('#')[1]
end
end
new_url = UrlParser.new("http://www.google.com:60/search?q=cat&name=Tim#img=FunnyCat")
new_url.port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment