Class: EY::CLI::API

Inherits:
API show all
Defined in:
lib/engineyard/cli/api.rb

Constant Summary

Constants inherited from API

API::USER_AGENT_STRING

Instance Attribute Summary

Attributes inherited from API

#token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from API

#==, #apps, #apps_for_repo, #environments, request, #resolver, #user

Constructor Details

#initialize(token = nil) ⇒ API

Returns a new instance of API.

Raises:



8
9
10
11
12
13
14
15
16
# File 'lib/engineyard/cli/api.rb', line 8

def initialize(token = nil)
  @token = token
  if ENV['ENGINEYARD_API_TOKEN']
    @token = ENV['ENGINEYARD_API_TOKEN']
  end
  @token ||= EY::EYRC.load.api_token
  @token ||= self.class.fetch_token
  raise EY::Error, "Sorry, we couldn't get your API token." unless @token
end

Class Method Details

.fetch_tokenObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/engineyard/cli/api.rb', line 32

def self.fetch_token
  EY.ui.info("We need to fetch your API token; please log in.")
  begin
    email    = EY.ui.ask("Email: ")
    password = EY.ui.ask("Password: ", true)
    super(email, password)
  rescue EY::API::InvalidCredentials
    EY.ui.warn "Invalid username or password; please try again."
    retry
  end
end

Instance Method Details

#refreshObject



28
29
30
# File 'lib/engineyard/cli/api.rb', line 28

def refresh
  @token = self.class.fetch_token
end

#requestObject



18
19
20
21
22
23
24
25
26
# File 'lib/engineyard/cli/api.rb', line 18

def request(*)
  begin
    super
  rescue EY::API::InvalidCredentials
    EY.ui.warn "Credentials rejected; please authenticate again."
    refresh
    retry
  end
end