Class: Auth

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

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/red_drop/auth.rb', line 4

def initialize
  puts "loading..."

  file = File.open("secret.txt", "a+")
  @secret = file.read
  file.close

  @conn = Faraday.new(
    url: "https://api.digitalocean.com/v2/droplets",
    headers: {"Authorization" => "Bearer #{@secret}"},
  )
  @res = @conn.get
end

Instance Method Details

#tokenObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/red_drop/auth.rb', line 18

def token
  while @secret.empty? or not @res.success?
    puts "\nThere in no valid API token in memory,\nplease enter a valid API token."
    @secret = gets.chomp
    File.write("secret.txt", @secret)

    @conn.headers["Authorization"] = "Bearer #{@secret}"
    @res = @conn.get
  end
  @conn
end