Skip to content

Instantly share code, notes, and snippets.

@adonaldson
Created February 9, 2011 11:02
Show Gist options
  • Save adonaldson/818313 to your computer and use it in GitHub Desktop.
Save adonaldson/818313 to your computer and use it in GitHub Desktop.
"Could not find crack-0.1.8 in any of the sources" -- what does crack do? 'gem? crack'
#!/usr/bin/env ruby
# I named this file gem? and put it into my path. I'm sure that cuteness will blow up in
# my face at some point!
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'CGI'
class RubyGemsSearch
attr_accessor :options
def initialize(options = {})
@options = options
@matches_markup = []
if !@options[:query].nil?
search
end
end
def search
encoded_query = CGI::escape(@options[:query])
doc = open("http://rubygems.org/search?utf8=%E2%9C%93&query=#{encoded_query}") { |f| Hpricot(f) }
@matches_markup = (doc/".gems ol a")
end
def matches
@matches_markup.map do |markup|
tidied = markup.inner_html
tidied.gsub!(/\s+/,' ')
tidied.gsub!(/\<strong\>/, '')
tidied.gsub!(/\<\/strong\>/, ':')
tidied.strip!
tidied
end
end
def matches?
@matches_markup.length > 0
end
end
if ARGV.length == 0
$stderr.puts 'usage: gem? [name of gem]'
abort
end
search = RubyGemsSearch.new(:query => ARGV.join(' '))
if search.matches?
$stdout.puts search.matches[0]
else
$stderr.puts "No gems found matching '#{search.options[:query]}'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment