Skip to content

Commit

Permalink
added syntax sugar to check if URI is absolute or relative
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Jul 2, 2018
1 parent ad56b14 commit 331fa44
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/std/uri_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe "URI" do
end
end

describe "#absolute?" do
it { URI.parse("http://www.example.com/foo").absolute?.should eq(true) }
it { URI.parse("http://www.example.com").absolute?.should eq(true) }
it { URI.parse("file://").absolute?.should eq(true) }
it { URI.parse("foo").absolute?.should eq(false) }
it { URI.parse("foo").absolute?.should eq(false) }
end

describe "#relative?" do
it { URI.parse("/foo").relative?.should eq(true) }
end

describe "normalize" do
it "removes dot notation from path" do
cases = {
Expand Down
10 changes: 10 additions & 0 deletions src/uri.cr
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ class URI
end
end

# Returns `true` if URI has a *scheme* specified.
def absolute?
@scheme ? true : false
end

# Returns `true` if URI does not have a *scheme* specified.
def relative?
!absolute?
end

def to_s(io : IO)
if scheme
io << scheme
Expand Down

0 comments on commit 331fa44

Please sign in to comment.