1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

add gem-stat command … (#1707)

thanks to @dannytatom for the original gem-stats rubygems plugin, which
inspired this Pry command.

it returns some general statistics about a rubygem when given a name.
note that rubygems rate limits the requests, and thanks to that this
command can be a "Fail Whale" although the error is handled gracefully.

This commit depends on pull request:
https://github.com/pry/pry/pull/1701
This commit is contained in:
r-obert 2017-11-18 22:39:10 +01:00 committed by GitHub
parent 3499b21a02
commit 337262c76f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 8 deletions

View file

@ -1,7 +1,12 @@
### 0.11.3
### HEAD
#### Features
* Add a new command, "gem-stat", inspired by the rubygem of a similar
name (gem-stats) by [@dannytatom](https://github.com/dannytatom).
See pull request [#1705](https://github.com/pry/pry/pull/1705].
* Add Pry::Platform#known_engines, returns an Array of Ruby engines
(MRI, JRuby, Rubinius) that Pry is known to run on.
@ -13,6 +18,18 @@ See pull request [#1694](https://github.com/pry/pry/pull/1694).
See pull request [#1701](https://github.com/pry/pry/pull/1701).
#### Pry developers
* Optionally skip a spec on specific Ruby engine(s) by providing `expect_failure: [:mri, :jruby]`
as a metadata Hash to the example group.
See pull request [#1694](https://github.com/pry/pry/pull/1694).
### 0.11.3
#### Features
* Add Pry::Testable, an improved modular replacement for PryTestHelpers.
**breaking change**.
@ -56,13 +73,6 @@ See pull request [#1691](https://github.com/pry/pry/pull/1691).
See pull request [#1674](https://github.com/pry/pry/pull/1674).
#### Pry developers
* Optionally skip a spec on specific Ruby engine(s) by providing `expect_failure: [:mri, :jruby]`
as a metadata Hash to the example group.
See pull request [#1694](https://github.com/pry/pry/pull/1694).
### 0.11.0
* Add alias 'whereami[?!]+' for 'whereami' command. ([#1597](https://github.com/pry/pry/pull/1597))

View file

@ -0,0 +1,81 @@
class Pry::Command::GemStat < Pry::ClassCommand
require 'json'
require 'net/http'
STAT_HOST = "rubygems.org"
STAT_PORT = 443
STAT_PATH = "/api/v1/gems/%s.json"
FAIL_WHALE = <<-FAILWHALE
W W W
W W W W
'. W
.-""-._ \ \.--|
/ "-..__) .-'
| _ /
\'-.__, .__.,'
`'----'._\--'
VVVVVVVVVVVVVVVVVVVVV
FAILWHALE
match 'gem-stat'
description 'Show the statistics of a gem (requires internet connection)'
group 'Gems'
command_options argument_required: true
banner <<-BANNER
gem-stats name
Show the statistics of a gem.
Requires an internet connection.
BANNER
def process(name)
client = Net::HTTP.start STAT_HOST, STAT_PORT, use_ssl: true
res = client.get STAT_PATH % URI.encode_www_form_component(name)
case res
when Net::HTTPOK
_pry_.pager.page format_gem(JSON.parse(res.body))
when Net::HTTPServiceUnavailable
_pry_.pager.page <<-FAILURE
#{bright_blue(FAIL_WHALE)}
#{bright_red('Ruby On Rails')}
#{bright_red('Net::HTTPServiceUnavailable')}
FAILURE
else
raise Pry::CommandError, "Bad response (#{res.class})"
end
ensure
client.finish if client
end
private
def format_gem(h)
h = Pry::Config.from_hash(h, nil)
format_str = unindent <<-FORMAT
%{name} %{version}
--
Total Downloads : %{downloads}
Version Downloads : %{version_downloads}
#{red('Dependencies')} (runtime)
--
%{rdependencies}
#{red('Dependencies')} (development)
%{ddependencies}
FORMAT
format_str % {name: green(h.name),
version: bold("v#{h.version}"),
downloads: h.downloads,
version_downloads: h.version_downloads,
rdependencies: format_dependencies(h.dependencies.runtime),
ddependencies: format_dependencies(h.dependencies.development)}
end
def format_dependencies(rdeps)
rdeps.empty? ?
bold("None") :
with_line_numbers(rdeps.map {|h|
"%{name} (%{requirements})" % {name: h["name"], requirements: h["requirements"]}
}.join($/), 1, :bold)
end
Pry::Commands.add_command(self)
end