forked from stephencelis/ghi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghi.rb
56 lines (50 loc) · 1.47 KB
/
ghi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require "net/http"
require "yaml"
YAML::ENGINE.yamler = "syck" if YAML.const_defined? :ENGINE
module GHI
VERSION = "0.2.4"
class << self
def login
return @login if defined? @login
@login = `git config --get github.user`.chomp
if @login.empty?
begin
print "Please enter your GitHub username: "
@login = gets.chomp
valid = user? @login
warn "invalid username" unless valid
end until valid
`git config --global github.user #@login`
end
@login
end
def token
return @token if defined? @token
@token = `git config --get github.token`.chomp
if @token.empty?
begin
print "GitHub token (https://github.com/account): "
@token = gets.chomp
valid = token? @token
warn "invalid token for #{login}" unless valid
end until valid
`git config --global github.token #@token`
end
@token
end
private
def user?(username)
url = "http://github.com/api/v2/yaml/user/show/#{username}"
!YAML.load(Net::HTTP.get(URI.parse(url)))["user"].nil?
rescue ArgumentError, URI::InvalidURIError
false
end
def token?(token)
url = "http://github.com/api/v2/yaml/user/show/#{login}"
url += "?login=#{login}&token=#{token}"
!YAML.load(Net::HTTP.get(URI.parse(url)))["user"]["plan"].nil?
rescue ArgumentError, NoMethodError, URI::InvalidURIError
false
end
end
end