Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement uniqueness (hash equality) #129

Merged
merged 4 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/twingly/url.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "digest"

require "addressable/idna/pure"
require "addressable/uri"
require "public_suffix"
Expand All @@ -23,6 +25,11 @@ class URL
PublicSuffix::DomainInvalid,
].freeze

# Instantiate digest classes in a thread-safe manner
# This is important since we don't know how people will
# use this gem (if they require it in a thread safe way)
SHA256_DIGEST = Digest(:SHA256)

private_constant :ACCEPTED_SCHEMES
private_constant :CUSTOM_PSL
private_constant :STARTS_WITH_WWW
Expand Down Expand Up @@ -193,6 +200,14 @@ def <=>(other)
self.to_s <=> other.to_s
end

def eql?(other)
self.hash == other.hash
walro marked this conversation as resolved.
Show resolved Hide resolved
end

def hash
SHA256_DIGEST.digest(self.to_s).unpack1("q")
walro marked this conversation as resolved.
Show resolved Hide resolved
end

def to_s
addressable_uri.to_s
end
Expand Down
8 changes: 8 additions & 0 deletions lib/twingly/url/null_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def <=>(other)
self.to_s <=> other.to_s
end

def eql?(other)
self.hash == other.hash
end

def hash
"".hash
end

def to_s
""
end
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/twingly/url/null_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@
expect { url.method_does_not_exist }.to raise_error(NoMethodError)
end
end

describe "uniqueness" do
context "a list with multiple NullURLs should only have one unique item" do
subject(:list) { 10.times.map { described_class.new }.uniq }

it { is_expected.to eq([described_class.new]) }
end
end
end
30 changes: 30 additions & 0 deletions spec/lib/twingly/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,36 @@ def valid_urls
end
end

describe "uniqueness" do
context do "with the same URL twice"
let(:a) { described_class.parse("https://www.twingly.com/") }
let(:b) { described_class.parse("https://www.google.com/") }
let(:c) { described_class.parse("https://www.twingly.com/") }

it "should give only unique URLs" do
expect([a,b,c].uniq).to eq([a,b])
end
end

context do "two similar URLs, but not exactly the same"
let(:a) { described_class.parse("https://www.twingly.com") }
let(:b) { described_class.parse("https://www.twingly.com/") }

it "should be two unique URLs" do
expect([a,b].uniq).to eq([a,b])
end
end

context do "the same URL but with some whitespace should be the same"
let(:a) { described_class.parse(" https://www.twingly.com/") }
let(:b) { described_class.parse("https://www.twingly.com/ ") }

it "should be one unique URL" do
expect([a,b].uniq).to eq([a])
end
end
end

describe "#inspect" do
let(:url_object) { described_class.parse(url) }
subject { url_object.inspect }
Expand Down