Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Adds web command #1

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0c3cef7
Add command web
MatheusRich Jun 3, 2019
bc1514e
Remove gem-web dependencies
MatheusRich Jun 10, 2019
6fd06cc
Fix command web tests
MatheusRich Jun 10, 2019
0894ae6
Fix linter errors
MatheusRich Jun 11, 2019
28d1fc6
Remove launchy
MatheusRich Jun 11, 2019
c77e71d
Remove fixtures
MatheusRich Jun 11, 2019
3d43e79
Update Manifest.txt
MatheusRich Jun 11, 2019
a894c04
Make open browser command cross-platform
MatheusRich Jun 18, 2019
f68b1d7
Adds open browser commands
MatheusRich Jun 30, 2019
7a2507c
Change open browser command hash to method
MatheusRich Jul 7, 2019
290f241
Updates open default browser commands
MatheusRich Jul 7, 2019
ff8ed2b
Add test to web command when platform is unsupported
MatheusRich Jul 13, 2019
99722d8
Add tests for all platforms
MatheusRich Jul 15, 2019
0dd4f1a
Update usage on web command
MatheusRich Jul 15, 2019
0c4266b
Fix rubocop issues
MatheusRich Jul 15, 2019
c70106a
Update web command behavior
MatheusRich Jul 20, 2019
52e35ff
Fix web command tests
MatheusRich Jul 22, 2019
22f959d
Remove github and rubytoolbox options from web command
MatheusRich Aug 3, 2019
d7656a1
Remove API call from web command
MatheusRich Aug 3, 2019
b0752dc
Support only some info from metadata
MatheusRich Aug 6, 2019
467205c
Refactor web command executor
MatheusRich Aug 8, 2019
c9e19cd
Add API fallback to web command
MatheusRich Aug 13, 2019
5b1481a
Remove attr acessor from web command
MatheusRich Aug 14, 2019
ccd6053
Remove some useless code
MatheusRich Aug 18, 2019
c40739b
Add minor refactoring
MatheusRich Aug 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ lib/rubygems/commands/stale_command.rb
lib/rubygems/commands/uninstall_command.rb
lib/rubygems/commands/unpack_command.rb
lib/rubygems/commands/update_command.rb
lib/rubygems/commands/web_command.rb
lib/rubygems/commands/which_command.rb
lib/rubygems/commands/yank_command.rb
lib/rubygems/compatibility.rb
Expand All @@ -320,6 +321,7 @@ lib/rubygems/deprecate.rb
lib/rubygems/doctor.rb
lib/rubygems/errors.rb
lib/rubygems/exceptions.rb
lib/rubygems/executor.rb
lib/rubygems/ext.rb
lib/rubygems/ext/build_error.rb
lib/rubygems/ext/builder.rb
Expand Down Expand Up @@ -544,6 +546,7 @@ test/rubygems/test_gem_commands_stale_command.rb
test/rubygems/test_gem_commands_uninstall_command.rb
test/rubygems/test_gem_commands_unpack_command.rb
test/rubygems/test_gem_commands_update_command.rb
test/rubygems/test_gem_commands_web_command.rb
test/rubygems/test_gem_commands_which_command.rb
test/rubygems/test_gem_commands_yank_command.rb
test/rubygems/test_gem_config_file.rb
Expand Down
1 change: 1 addition & 0 deletions lib/rubygems/command_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Gem::CommandManager
:uninstall,
:unpack,
:update,
:web,
:which,
:yank,
].freeze
Expand Down
51 changes: 51 additions & 0 deletions lib/rubygems/commands/web_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'rubygems/command'
require 'rubygems/executor'
require 'rubygems/version_option'

class Gem::Commands::WebCommand < Gem::Command

include Gem::VersionOption
attr_reader :executor

def initialize
super 'web', "Open the gem's homepage",
:command => nil,
:version => Gem::Requirement.default,
:latest => false

add_option("-g", "--github", "Open GitHub page of gem, this searches all urls for a GitHub page. This is the default.") do |v|
options[:github] = v
end
add_option("-c", "--sourcecode", "Open sourcecode gem") do |v|
options[:sourcecode] = v
end
add_option("-d", "--doc", "Open documentation of gem") do |v|
options[:doc] = v
end
add_option("-w", "--webpage", "Open webpage of gem") do |v|
options[:webpage] = v
end
add_option("-r", "--rubygems", "Open the rubygems page of a gem") do |v|
options[:rubygems] = v
end
add_option("-t", "--rubytoolbox", "Open the ruby toolbox page of a gem") do |v|
options[:rubytoolbox] = v
end

@executor = Gem::Web::Executor.new
end

def arguments
"GEMNAME gem to open the webpage for"
end

def usage
"[GEMNAME]"
end

def execute
@executor.open_page(get_one_optional_argument, options)
end

end
73 changes: 73 additions & 0 deletions lib/rubygems/executor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true
require 'open-uri'
require 'json'

module Gem
module Web
class Executor

def open_page(gem, options)
if options[:sourcecode]
find_page(gem, "source_code_uri")
elsif options[:doc]
find_page(gem, "documentation_uri")
elsif options[:webpage]
find_page(gem, "homepage_uri")
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)
end
end

def get_api_metadata(gem)
begin
JSON.parse(open("https://rubygems.org/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)
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

# TODO: Make this cross-platform
def open_default_browser(uri)
system("xdg-open", uri)
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved
end

end
end
end
111 changes: 111 additions & 0 deletions test/rubygems/test_gem_commands_web_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/executor'
require 'rubygems/commands/web_command'

class TestGemCommandsWebCommand < Gem::TestCase

def setup
super
@cmd = Gem::Commands::WebCommand.new
@mock = MiniTest::Mock.new
end

def test_default_option
@mock.expect(:call, true, ["xdg-open", "http://github.com/rails/rails"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[rails]
@cmd.execute
end

@mock.verify
end

def test_open_the_documentation
@mock.expect(:call, true, ["xdg-open", "http://api.rubyonrails.org"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-d rails]
@cmd.execute
end

@mock.verify
end

def test_open_the_homepage
@mock.expect(:call, true, ["xdg-open", "http://rubyonrails.org"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-w rails]
@cmd.execute
end

@mock.verify
end

def test_open_the_source_code
@mock.expect(:call, true, ["xdg-open", "http://github.com/rails/rails"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-c rails]
@cmd.execute
end

@mock.verify
end

def test_open_github
@mock.expect(:call, true, ["xdg-open", "http://github.com/rails/rails"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-g rails]
@cmd.execute
end

@mock.verify
end

def test_open_rubygems
@mock.expect(:call, true, ["xdg-open", "https://rubygems.org/gems/rails"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-r rails]
@cmd.execute
end

@mock.verify
end

def test_open_rubytoolbox
@mock.expect(:call, true, ["xdg-open", "https://www.ruby-toolbox.com/projects/rails"])

@cmd.executor.stub :system, @mock do
@cmd.handle_options %w[-t rails]
@cmd.execute
end

@mock.verify
end

def test_search_unexisting_gem
gem = "this-is-an-unexisting-gem"
assert_output(/Did not find #{gem} on rubygems.org\n/) do
@cmd.handle_options [gem]
@cmd.execute
end
end

def test_open_rubygems_if_it_could_not_find_page
@mock.expect(:call, true, ["xdg-open", "https://rubygems.org/gems/rails"])

@cmd.executor.stub :system, @mock do
assert_output("Did not find page for rails, opening RubyGems page instead.\n") do
@cmd.executor.launch_browser("rails", "")
end
end

@mock.verify
end

end