Last active
July 18, 2017 01:13
-
-
Save waldothedeveloper/1b19bd5114798d66e4d421e558bcb4c5 to your computer and use it in GitHub Desktop.
URL_PARSER homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UrlParser | |
def initialize(new_url) | |
@new_url = new_url | |
end | |
def scheme | |
@new_url.split("://")[0] | |
end | |
def domain | |
@new_url.split("://")[1].split(":")[0] | |
end | |
def port | |
ok_port = @new_url.split("/")[0] + @new_url.split("/")[2] | |
if ok_port.include? "60" | |
return ok_port.split(":")[2] | |
elsif ok_port.split(":")[1] == "60" | |
ok_port.split(":")[1] | |
elsif ok_port.include? "https" | |
return "443" | |
else | |
return "80" | |
end | |
end | |
def path | |
the_path = @new_url.split("?")[0].split("/")[3] | |
case the_path | |
when "search" | |
then the_path | |
when ("?") | |
then NIL | |
end | |
end | |
def query_string | |
h = @new_url.split("?")[1].split("#")[0].split(/[=&]/) | |
Hash[*h] | |
end | |
def fragment_id | |
frag_id = @new_url.split("#")[1] | |
end | |
end | |
github_url = UrlParser.new "http://www.apple.com:60/search?q=cat&name=Tim#img=FunnyCat" | |
p github_url.scheme | |
p github_url.domain | |
p github_url.port | |
p github_url.path | |
p github_url.query_string | |
p github_url.fragment_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './url_parser.rb' | |
describe UrlParser do | |
context 'with all parts' do | |
before(:each) do | |
@new_url = UrlParser.new "http://www.google.com:60/search?q=cat&name=Tim#img=FunnyCat" | |
end | |
it 'when instantiated should be a member of the UrlParser class' do | |
expect(@new_url).to be_a UrlParser | |
end | |
it 'should have a scheme attribute' do | |
expect(@new_url.scheme).to eq("http") | |
end | |
it 'should have a domain attribute' do | |
expect(@new_url.domain).to eq("www.google.com") | |
end | |
it 'should have a port attribute with the given port number' do | |
expect(@new_url.port).to eq("60") | |
end | |
it 'should have a path attribute' do | |
expect(@new_url.path).to eq("search") | |
end | |
it 'should have a query string attribute that should return a hash of query params' do | |
expect(@new_url.query_string).to be_a(Hash) | |
expect(@new_url.query_string).to eq({"q" => "cat", "name" => "Tim"}) | |
end | |
it 'should have a fragment id attribute' do | |
expect(@new_url.fragment_id).to eq("img=FunnyCat") | |
end | |
end | |
context 'with no path' do | |
before(:each) do | |
@new_url = UrlParser.new "https://www.google.com/?q=cat#img=FunnyCat" | |
end | |
it 'should return a nil path' do | |
expect(@new_url.path).to be(NIL) | |
end | |
it 'should be able to have a query string at the root path' do | |
expect(@new_url.query_string).to eq({"q" => "cat"}) | |
end | |
it 'should be able to have a fragment id at the root path' do | |
expect(@new_url.fragment_id).to eq("img=FunnyCat") | |
end | |
end | |
context 'with a special case' do | |
it 'with no port number and a http scheme should default to port 80' do | |
insecure_url = UrlParser.new "http://www.google.com/search" | |
expect(insecure_url.port).to eq("80") | |
end | |
it 'with no port number and a https scheme should default to port 443' do | |
secure_url = UrlParser.new "https://www.google.com/search" | |
expect(secure_url.port).to eq("443") | |
end | |
it 'a query string with duplicate params should only return one key value pair' do | |
duplicate_param = UrlParser.new "http://www.google.com:60/search?q=cat&q=overwrite#img=FunnyCat" | |
expect(duplicate_param.query_string).to eq({"q"=> "overwrite"}) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment