Class: FastGithub::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_github.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



12
13
14
15
16
17
# File 'lib/fast_github.rb', line 12

def initialize
  @token = ''
  @fast_github_dir = ENV["HOME"] + '/.fast_github/'
  @token_filepath = ENV["HOME"] + '/.fast_github/' + 'token'
  authenticate
end

Instance Attribute Details

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/fast_github.rb', line 10

def token
  @token
end

Instance Method Details

#authenticateObject



19
20
21
22
23
24
25
# File 'lib/fast_github.rb', line 19

def authenticate
  if stored_oauth
    @token = stored_oauth
  else
    request_oauth
  end
end

#request_oauthObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fast_github.rb', line 42

def request_oauth
  uri = URI.parse("https://api.github.com/authorizations/clients/46cdc9a8d97dcf65813e")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Put.new(uri.request_uri)
  print "Enter your Github username: "
  user = gets.chomp
  print "Enter your Github password: "
  pass = STDIN.noecho(&:gets).chomp
  puts ""
  request.basic_auth(user.chomp, pass.chomp)
  request.body = { "client_secret" => "2e05ca092118ef4df3c5089717a0bf8cfe1192f1", "scopes" => [ "repo" ] }.to_json
  response = http.request(request)
  if response.code == "200" || response.code == "201"
    json_response = JSON.parse(response.body)
    save_oath(json_response['token'])
    @token = json_response['token']
  else
    raise "Error connecting to Github API"
  end
end

#save_oath(token) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/fast_github.rb', line 33

def save_oath(token)
  if !File.exists? @fast_github_dir
    Dir.mkdir @fast_github_dir
  end
  file = File.open(@token_filepath, 'w')
  file.write(token)
  file.close
end

#stored_oauthObject



27
28
29
30
31
# File 'lib/fast_github.rb', line 27

def stored_oauth
  if File.exists? @token_filepath
    File.open(@token_filepath, &:readline).chomp
  end
end