-
-
Notifications
You must be signed in to change notification settings - Fork 902
/
gem_typo.rb
40 lines (30 loc) · 982 Bytes
/
gem_typo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class GemTypo
DOWNLOADS_THRESHOLD = 10_000
LAST_RELEASE_TIME = 5.years.ago
attr_reader :protected_gem
def initialize(rubygem_name)
@rubygem_name = rubygem_name
end
def protected_typo?
return false if GemTypoException.where("upper(name) = upper(?)", @rubygem_name).any?
return false if published_exact_name_matches.any?
match = matched_protected_gem_name
return false if not_protected?(match)
@protected_gem = match.name
true
end
private
def published_exact_name_matches
Rubygem.with_versions.where("upper(name) = upper(?)", @rubygem_name)
end
def matched_protected_gem_name
Rubygem.with_versions.find_by(
"regexp_replace(upper(name), '[_-]', '', 'g') = regexp_replace(upper(?), '[_-]', '', 'g')",
@rubygem_name
)
end
def not_protected?(rubygem)
return true unless rubygem
rubygem.downloads < DOWNLOADS_THRESHOLD && rubygem.most_recent_version.created_at < LAST_RELEASE_TIME
end
end