-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
rubiii
commented
Feb 5, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment