diff options
author | r-obert <r-obert@users.noreply.github.com> | 2017-11-18 22:39:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-18 22:39:10 +0100 |
commit | 337262c76f498384f351c59b9c91fa7a70672f91 (patch) | |
tree | e71d93ba3f89f57ff08533e130cc68be7a0cc5d6 | |
parent | 3499b21a0295ee114bb5b4e0093b4e355389c5a3 (diff) | |
download | pry-337262c76f498384f351c59b9c91fa7a70672f91.tar.gz |
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
-rw-r--r-- | CHANGELOG.md | 26 | ||||
-rw-r--r-- | lib/pry/commands/gem_stats.rb | 81 |
2 files changed, 99 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 8792edfb..630491f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/lib/pry/commands/gem_stats.rb b/lib/pry/commands/gem_stats.rb new file mode 100644 index 00000000..c652ba0a --- /dev/null +++ b/lib/pry/commands/gem_stats.rb @@ -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 |