Sha256: 506d518792a2c62bc7d9f195be80173e87cdb427eb21e708da8f31a3a85e0be2

Contents?: true

Size: 1.07 KB

Versions: 24

Compression:

Stored size: 1.07 KB

Contents

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

Version data entries

24 entries across 24 versions & 8 rubygems

Version Path
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
xaiml-0.1.3 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
xaiml-0.1.2 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
xaiml-0.1.1 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
xaiml-0.1.0 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
monero_wallet_gen-0.1.0 vendor/bundle/ruby/2.3.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
sb_prime_table-0.1.1 vendor/bundle/ruby/2.4.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
sb_prime_table-0.1.0 vendor/bundle/ruby/2.4.0/gems/pry-0.11.3/lib/pry/commands/gem_search.rb
comiditaULL-0.1.1 vendor/bundle/ruby/2.3.0/gems/pry-0.11.2/lib/pry/commands/gem_search.rb
comidita_ull-0.1.1 vendor/bundle/ruby/2.3.0/gems/pry-0.11.2/lib/pry/commands/gem_search.rb
comidita_ull-0.1.0 vendor/bundle/ruby/2.3.0/gems/pry-0.11.2/lib/pry/commands/gem_search.rb
pry-0.11.3 lib/pry/commands/gem_search.rb
pry-0.11.3-java lib/pry/commands/gem_search.rb
pry-0.11.2 lib/pry/commands/gem_search.rb
pry-0.11.2-java lib/pry/commands/gem_search.rb
tdiary-5.0.6 vendor/bundle/gems/pry-0.11.0/lib/pry/commands/gem_search.rb
pry-0.11.1 lib/pry/commands/gem_search.rb
pry-0.11.1-java lib/pry/commands/gem_search.rb
pry-0.11.0 lib/pry/commands/gem_search.rb
pry-0.11.0-java lib/pry/commands/gem_search.rb