Skip to content

Instantly share code, notes, and snippets.

@rubiii
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubiii/8829498 to your computer and use it in GitHub Desktop.
Save rubiii/8829498 to your computer and use it in GitHub Desktop.
check whether a certain project name is available as a gem or npm package
#!/usr/bin/env ruby
require 'httpclient'
require 'json'
class Chk
RUBYGEMS_URL = 'http://rubygems.org/gems/%s'
NPM_URL = 'https://npmjs.org/package/%s'
HTTPError = Class.new(StandardError)
def initialize(client)
@client = client
end
attr_reader :client
def rubygems(name)
puts "#{name} (rubygems): #{availability(name, RUBYGEMS_URL)}"
end
def npm(name)
puts "#{name} (npm): #{availability(name, NPM_URL)}"
end
private
def availability(name, url)
available?(name, url) ? 'available!' : 'taken'
end
def available?(name, url)
response = client.get(url % name)
case response.status
when 404 then true
when 200 then false
else raise HTTPError, 'something went wrong!'
end
end
end
if ARGV.any?
names = ARGV
else
puts 'feed me possible project name(s)!'
exit
end
client = HTTPClient.new
chk = Chk.new(client)
names.each do |name|
chk.rubygems(name)
chk.npm(name)
end
@rubiii
Copy link
Author

rubiii commented Feb 5, 2014

$ chmod +x chk
$ chk sinatra rails fancy-rainbows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment