mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
commit
cafd3d6d58
2 changed files with 41 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
### HEAD
|
||||
|
||||
* Add 'gem-readme' command, prints the README file bundled with a rubygem
|
||||
* Add 'gem-search' command, searches for a gem with the rubygems.org HTTP API
|
||||
|
||||
### 0.10.1
|
||||
|
||||
|
|
40
lib/pry/commands/gem_search.rb
Normal file
40
lib/pry/commands/gem_search.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
class Pry::Command::GemSearch < Pry::ClassCommand
|
||||
match 'gem-search'
|
||||
description 'Search for a gem with the rubygems.org JSON API'
|
||||
group 'Gems'
|
||||
command_options argument_required: true
|
||||
banner <<-BANNER
|
||||
gem-search [options] gem
|
||||
Search for a gem with the rubygems.org HTTP API
|
||||
BANNER
|
||||
|
||||
API_ENDPOINT = 'https://rubygems.org/api/v1/search.json'
|
||||
|
||||
def setup
|
||||
require 'json' unless defined?(JSON)
|
||||
require 'net/http' unless defined?(Net::HTTP)
|
||||
end
|
||||
|
||||
def options(opt)
|
||||
opt.on :l, :limit, 'Limit the number of results (max: 30)',
|
||||
default: 10,
|
||||
as: Integer,
|
||||
argument: true
|
||||
end
|
||||
|
||||
def process(str)
|
||||
uri = URI.parse(API_ENDPOINT)
|
||||
uri.query = URI.encode_www_form(query: str)
|
||||
gems = JSON.load Net::HTTP.get(uri)
|
||||
_pry_.pager.page list_as_string(gems, opts[:limit])
|
||||
end
|
||||
|
||||
private
|
||||
def list_as_string(gems, limit = 10)
|
||||
gems[0..limit-1].map do |gem|
|
||||
name, version, info = gem.values_at 'name', 'version', 'info'
|
||||
"#{text.bold(name)} #{text.bold('v'+version)} \n#{info}\n\n"
|
||||
end.join
|
||||
end
|
||||
Pry::Commands.add_command(self)
|
||||
end
|
Loading…
Reference in a new issue