Skip to content

Commit

Permalink
Remove API call from web command
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusRich committed Aug 3, 2019
1 parent 22f959d commit 3982cd6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 111 deletions.
65 changes: 20 additions & 45 deletions lib/rubygems/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,40 @@ module Web
class Executor

def open_page(gem, options)
spec = Gem::Specification.find_by_name(gem)

if options[:sourcecode]
find_page(gem, "source_code_uri")
source_code_uri = spec.metadata["source_code_uri"]
if source_code_uri
open_default_browser(source_code_uri)
else
puts("This gem has no info about its source code.")
end
elsif options[:doc]
find_page(gem, "documentation_uri")
documentation_uri = spec.metadata["documentation_uri"]
if documentation_uri
open_default_browser(documentation_uri)
else
puts("This gem has no info about its documentation.")
end
elsif options[:webpage]
find_page(gem, "homepage_uri")
open_default_browser(spec.homepage)
elsif options[:rubygems]
open_rubygems(gem)
elsif options[:rubytoolbox]
open_rubytoolbox(gem)
else
find_github(gem)
end
end

def find_page(gem, page)
meta = get_api_metadata(gem)
launch_browser(gem, meta[page]) unless meta.nil?
end

def find_github(gem)
unless (meta = get_api_metadata(gem)).nil?
links = [meta["source_code_uri"], meta["documentation_uri"], meta["homepage_uri"]]
uri = links.find do |link|
!link.nil? && link.match(/http(s?):\/\/(www\.)?github.com\/.*/i)
end
launch_browser(gem, uri)
else # The default option is homepage
open_default_browser(spec.homepage)
end
end

def get_api_metadata(gem)
begin
JSON.parse(open("#{Gem.host}/api/v1/gems/#{gem}.json").read)
rescue OpenURI::HTTPError
puts "Did not find #{gem} on rubygems.org"
nil
end
end

def launch_browser(gem, uri)
if uri.nil? || uri.empty?
puts "Did not find page for #{gem}, opening RubyGems page instead."
uri = "https://rubygems.org/gems/#{gem}"
end

open_default_browser(uri)
rescue Gem::MissingSpecError => e
puts e.message
end

def open_rubygems(gem)
open_default_browser("https://rubygems.org/gems/#{gem}")
end

def open_rubytoolbox(gem)
open_default_browser("https://www.ruby-toolbox.com/projects/#{gem}")
end

def open_default_browser(uri)
open_browser_cmd = ENV['BROWSER']
if open_browser_cmd.nil?
if open_browser_cmd.nil? || open_browser_cmd.empty?
puts uri
else
system(open_browser_cmd, uri)
Expand Down
134 changes: 68 additions & 66 deletions test/rubygems/test_gem_commands_web_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@ def setup
@cmd = Gem::Commands::WebCommand.new
@mock = MiniTest::Mock.new
ENV['BROWSER'] = nil
@gem = "rails"
response = <<~HEREDOC
{
"source_code_uri": "http://github.com/rails/rails",
"documentation_uri": "http://api.rubyonrails.org",
"homepage_uri": "http://rubyonrails.org"
}
HEREDOC

@mock.expect(:read, response)
@gem = "bestgemever"
end

def test_default_option
OpenURI.stub :open_uri, @mock do
assert_output("http://github.com/rails/rails\n") do
def test_default_option_should_be_homepage
@mock.expect(:homepage, "https://bestgemever.example.io")
Gem::Specification.stub :find_by_name, @mock do
assert_output("https://bestgemever.example.io\n") do
@cmd.handle_options [@gem]
@cmd.execute
end
Expand All @@ -33,101 +25,111 @@ def test_default_option
end

def test_open_the_documentation
OpenURI.stub :open_uri, @mock do
assert_output("http://api.rubyonrails.org\n") do
metadata = {
"documentation_uri" => "https://www.example.info/gems/bestgemever/0.0.1",
}
@mock.expect(:metadata, metadata)

Gem::Specification.stub :find_by_name, @mock do
assert_output("https://www.example.info/gems/bestgemever/0.0.1\n") do
@cmd.handle_options ["-d", @gem]
@cmd.execute
end
end
@mock.verify
end

def test_open_the_homepage
OpenURI.stub :open_uri, @mock do
assert_output("http://rubyonrails.org\n") do
@mock.expect(:homepage, "https://bestgemever.example.io")

Gem::Specification.stub :find_by_name, @mock do
assert_output("https://bestgemever.example.io\n") do
@cmd.handle_options ["-w", @gem]
@cmd.execute
end
end
@mock.verify
end

def test_open_the_source_code
OpenURI.stub :open_uri, @mock do
assert_output("http://github.com/rails/rails\n") do
metadata = {
"source_code_uri" => "https://example.com/user/bestgemever"
}
@mock.expect(:metadata, metadata)

Gem::Specification.stub :find_by_name, @mock do
assert_output("https://example.com/user/bestgemever\n") do
@cmd.handle_options ["-c", @gem]
@cmd.execute
end
end
@mock.verify
end

def test_open_github
OpenURI.stub :open_uri, @mock do
assert_output("http://github.com/rails/rails\n") do
@cmd.handle_options ["-g", @gem]
@cmd.execute
def test_open_when_info_is_missing
[
["-c", "This gem has no info about its source code.\n"],
["-d", "This gem has no info about its documentation.\n"],
].each do |a|
option = a[0]
error = a[1]
@mock.expect(:metadata, {})
Gem::Specification.stub :find_by_name, @mock do
assert_output(error) do
@cmd.handle_options [option, @gem]
@cmd.execute
end
end
@mock.verify
end
end

def test_open_rubygems
OpenURI.stub :open_uri, @mock do
assert_output("https://rubygems.org/gems/rails\n") do
Gem::Specification.stub :find_by_name, @mock do
assert_output("https://rubygems.org/gems/#{@gem}\n") do
@cmd.handle_options ["-r", @gem]
@cmd.execute
end
end
end

def test_open_rubytoolbox
OpenURI.stub :open_uri, @mock do
assert_output("https://www.ruby-toolbox.com/projects/rails\n") do
@cmd.handle_options ["-t", @gem]
@cmd.execute
end
end
@mock.verify
end

def test_search_unexisting_gem
raises_exception = proc { raise OpenURI::HTTPError.new("error", nil) }

OpenURI.stub :open_uri, raises_exception do
gem = "this-is-an-unexisting-gem"
assert_output(/Did not find #{gem} on rubygems.org\n/) do
@cmd.handle_options [gem]
@cmd.execute
end
gem = "this-is-an-unexisting-gem"
assert_output(/Could not find '#{gem}'/) do
@cmd.handle_options [gem]
@cmd.execute
end
end

def test_open_rubygems_if_it_could_not_find_page
OpenURI.stub :open_uri, @mock do
out, _ = capture_io do
@cmd.executor.launch_browser("rails", "")
end
assert_match(/Did not find page for rails, opening RubyGems page instead./, out)
assert_match(/https:\/\/rubygems.org\/gems\/rails/, out)
end
end
# def test_open_rubygems_if_it_could_not_find_page
# Gem::Specification.stub :find_by_name, @mock do
# out, _ = capture_io do
# @cmd.executor.launch_browser("rails", "")
# end
# assert_match(/Did not find page for rails, opening RubyGems page instead./, out)
# assert_match(/https:\/\/rubygems.org\/gems\/rails/, out)
# end
# end

def test_open_browser_if_env_variable_is_set
OpenURI.stub :open_uri, @mock do
open_browser_cmd = "open"
uri = "http://github.com/rails"
open_browser_cmd = "open"
uri = "http://github.com/rails"

env_mock = MiniTest::Mock.new
env_mock.expect(:call, open_browser_cmd, ['BROWSER'])
env_mock = MiniTest::Mock.new
env_mock.expect(:call, open_browser_cmd, ['BROWSER'])

browser_mock = MiniTest::Mock.new
browser_mock.expect(:call, true, [open_browser_cmd, uri])
browser_mock = MiniTest::Mock.new
browser_mock.expect(:call, true, [open_browser_cmd, uri])

ENV.stub :[], env_mock do
@cmd.executor.stub :system, browser_mock do
@cmd.executor.open_default_browser(uri)
end
ENV.stub :[], env_mock do
@cmd.executor.stub :system, browser_mock do
@cmd.executor.open_default_browser(uri)
end

browser_mock.verify
env_mock.verify
end

browser_mock.verify
env_mock.verify
end

end

0 comments on commit 3982cd6

Please sign in to comment.