Skip to content

Commit

Permalink
ruby gems api uses metadata or linkset uris
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Dec 5, 2016
1 parent eefe047 commit 37059a4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def each
end

def documentation_uri
(linkset && linkset.docs.presence) || "http://www.rubydoc.info/gems/#{rubygem.name}/#{version.number}"
(version.metadata && version.metadata["documentation_uri"].presence) ||
(linkset && linkset.docs.presence) ||
"http://www.rubydoc.info/gems/#{rubygem.name}/#{version.number}"
end

# technically this is a path
Expand All @@ -52,7 +54,7 @@ def badge_uri
LINKS.each do |short, long|
unless method_defined?(long)
define_method(long) do
linkset && linkset.public_send(short)
(version.metadata && version.metadata[long.to_s].presence) || (linkset && linkset.public_send(short))
end
end
alias_method short, long
Expand Down
38 changes: 38 additions & 0 deletions test/unit/rubygem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,44 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal run_dep.name, doc.at_css("dependencies runtime dependency name").content
end

context "with metadata" do
setup do
@version = create(:version, rubygem: @rubygem)
@rubygem = build(:rubygem, versions: [@version])
end

should "prefer metadata over links in JSON" do
@version.update_attributes!(
metadata: {
"homepage_uri" => "http://example.com/home",
"wiki_uri" => "http://example.com/wiki",
"documentation_uri" => "http://example.com/docs",
"mailing_list_uri" => "http://example.com/mail",
"source_code_uri" => "http://example.com/code",
"bug_tracker_uri" => "http://example.com/bugs"
}
)

hash = MultiJson.load(@rubygem.to_json)

assert_equal "http://example.com/home", hash["homepage_uri"]
assert_equal "http://example.com/wiki", hash["wiki_uri"]
assert_equal "http://example.com/docs", hash["documentation_uri"]
assert_equal "http://example.com/mail", hash["mailing_list_uri"]
assert_equal "http://example.com/code", hash["source_code_uri"]
assert_equal "http://example.com/bugs", hash["bug_tracker_uri"]
end

should "return version documentation url if metadata and linkset docs is empty" do
@version.update_attributes!(metadata: {})
@rubygem.linkset.update_attributes(:docs, "") if @rubygem.linkset
@rubygem.reload
hash = JSON.load(@rubygem.to_json)

assert_equal "http://www.rubydoc.info/gems/#{@rubygem.name}/#{@version.number}", hash["documentation_uri"]
end
end

context "with a linkset" do
setup do
@rubygem = build(:rubygem)
Expand Down

0 comments on commit 37059a4

Please sign in to comment.