Skip to content

Commit 9720270

Browse files
authored
Use Set where it makes sense (#35)
A bunch of this array-generating code calls `uniq` which is a great case for using `Set`. Also, assigning variables isn't the end of the world and doing so avoids chained allocations and lets us use bang methods like `reject!`.
1 parent 6157a0a commit 9720270

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

lib/webmention/client.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ def inspect
6363
def mentioned_urls
6464
response = source_url.response
6565

66-
self.class
67-
.registered_parsers[response.mime_type]
68-
.new(response.body, response.uri)
69-
.results
70-
.uniq
71-
.reject { |url| url.match(/^#{response.uri}(?:#.*)?$/) }
72-
.sort
66+
urls = Set.new(
67+
self.class
68+
.registered_parsers[response.mime_type]
69+
.new(response.body, response.uri)
70+
.results
71+
)
72+
73+
urls.reject! { |url| url.match(/^#{response.uri}(?:#.*)?$/) }
74+
75+
urls.to_a.sort
7376
end
7477

7578
# Send a webmention from this client's source URL to a target URL.

lib/webmention/verification.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def vouch_mentions_source?
6161
#
6262
# @return [Array<String>]
6363
def mentioned_domains(response)
64-
mentioned_urls(response).map { |url| HTTP::URI.parse(url).host }.uniq
64+
Set.new(mentioned_urls(response).map { |url| HTTP::URI.parse(url).host }).to_a
6565
end
6666

6767
# @param response [Webmention::Response]
@@ -70,10 +70,11 @@ def mentioned_domains(response)
7070
#
7171
# @return [Array<String>]
7272
def mentioned_urls(response)
73-
Client.registered_parsers[response.mime_type]
74-
.new(response.body, response.uri)
75-
.results
76-
.uniq
73+
Set.new(
74+
Client.registered_parsers[response.mime_type]
75+
.new(response.body, response.uri)
76+
.results
77+
).to_a
7778
end
7879
end
7980
end

0 commit comments

Comments
 (0)