Class: FastGithub::Auth
- Inherits:
-
Object
- Object
- FastGithub::Auth
- Defined in:
- lib/fast_github.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#initialize ⇒ Auth
constructor
A new instance of Auth.
- #request_oauth ⇒ Object
- #save_oath(token) ⇒ Object
- #stored_oauth ⇒ Object
Constructor Details
#initialize ⇒ Auth
Returns a new instance of Auth.
11 12 13 14 15 16 |
# File 'lib/fast_github.rb', line 11 def initialize @token = '' @fast_github_dir = ENV["HOME"] + '/.fast_github/' @token_filepath = ENV["HOME"] + '/.fast_github/' + 'token' authenticate end |
Instance Attribute Details
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/fast_github.rb', line 9 def token @token end |
Instance Method Details
#authenticate ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/fast_github.rb', line 18 def authenticate if stored_oauth @token = stored_oauth else request_oauth end end |
#request_oauth ⇒ Object
41 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 41 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) client_secret = File.open(File.dirname(__FILE__) + "/client_secret", &:readline).chomp request.body = { "client_secret" => client_secret, "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
32 33 34 35 36 37 38 39 |
# File 'lib/fast_github.rb', line 32 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_oauth ⇒ Object
26 27 28 29 30 |
# File 'lib/fast_github.rb', line 26 def stored_oauth if File.exists? @token_filepath File.open(@token_filepath, &:readline).chomp end end |