-
-
Notifications
You must be signed in to change notification settings - Fork 902
/
links.rb
73 lines (63 loc) · 2.03 KB
/
links.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Links
# Links available for indexed gems
LINKS = {
"home" => "homepage_uri",
"changelog" => "changelog_uri",
"code" => "source_code_uri",
"docs" => "documentation_uri",
"wiki" => "wiki_uri",
"mail" => "mailing_list_uri",
"bugs" => "bug_tracker_uri",
"download" => "download_uri",
"funding" => "funding_uri"
}.freeze
# Links available for non-indexed gems
NON_INDEXED_LINKS = {
"docs" => "documentation_uri"
}.freeze
attr_accessor :rubygem, :version, :linkset
def initialize(rubygem, version)
self.rubygem = rubygem
self.version = version
self.linkset = rubygem.linkset
end
def links
version.indexed ? LINKS : NON_INDEXED_LINKS
end
delegate :keys, to: :links
def each
return enum_for(:each) unless block_given?
links.each do |short, long|
value = send(long)
yield short, value if value
end
end
# documentation uri:
# if metadata has it defined, use that
# or if linksets has it defined, use that
# else, generate one from gem name and version number
def documentation_uri
return version.metadata["documentation_uri"].presence if version.metadata_uri_set?
linkset&.docs&.presence || "https://www.rubydoc.info/gems/#{rubygem.name}/#{version.number}"
end
# technically this is a path
def download_uri
"/downloads/#{version.full_name}.gem" if version.indexed
end
# excluded from metadata_uri_set? check
def homepage_uri
version.metadata["homepage_uri"].presence || linkset&.home
end
# define getters for each of the uris (both short `home` or long `homepage_uri` versions)
# don't define for download_uri since it has special logic and is already defined
# using a try because linkset does not define all the uri attributes
LINKS.each do |short, long|
unless method_defined?(long)
define_method(long) do
return version.metadata[long].presence if version.metadata_uri_set?
linkset.try(short)
end
end
alias_method short, long
end
end