Class: PryGem::GemSearch

Inherits:
Pry::ClassCommand
  • Object
show all
Defined in:
lib/pry-gem/gem_search.rb

Overview

Since:

  • 1.0.0

Constant Summary collapse

API_ENDPOINT =

Since:

  • 1.0.0

'https://rubygems.org/api/v1/search.json'.freeze

Instance Method Summary collapse

Instance Method Details

#options(opt) ⇒ Object

Since:

  • 1.0.0



21
22
23
24
25
26
# File 'lib/pry-gem/gem_search.rb', line 21

def options(opt)
  opt.on :l, :limit, 'Limit the number of results (max: 30)',
         default: 10,
         as: Integer,
         argument: true
end

#process(str) ⇒ Object

Since:

  • 1.0.0



28
29
30
31
32
33
# File 'lib/pry-gem/gem_search.rb', line 28

def process(str)
  uri = URI.parse(API_ENDPOINT)
  uri.query = URI.encode_www_form(query: str)
  gems = JSON.parse(Net::HTTP.get(uri))
  _pry_.pager.page list_as_string(gems, opts[:limit])
end

#setupObject

Since:

  • 1.0.0



16
17
18
19
# File 'lib/pry-gem/gem_search.rb', line 16

def setup
  require 'json' unless defined?(JSON)
  require 'net/http' unless defined?(Net::HTTP)
end