Class: Gemical::Auth

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gemical/auth.rb

Instance Method Summary collapse

Methods included from Singleton

included

Instance Method Details

#basic_authArray

Returns Basic authentication credentials, for API request.

Returns:

  • (Array)

    Basic authentication credentials, for API request



55
56
57
# File 'lib/gemical/auth.rb', line 55

def basic_auth
  [credentials.last, 'x'] if credentials
end

#configurationGemical::Configuration

Returns The configuration.

Returns:



6
7
8
# File 'lib/gemical/auth.rb', line 6

def configuration
  Gemical.configuration
end

#credentials[String, String]

Returns Username and password.

Returns:

  • ([String, String])

    Username and password



47
48
49
50
51
# File 'lib/gemical/auth.rb', line 47

def credentials
  return nil unless configuration.credentials?
  pair = configuration.credentials.read.split(/\s+/)[0..1]
  pair if pair.size == 2 && pair.all? {|v| string_value?(v) }
end

#find_vault(name) ⇒ Gemical::Vault

Returns A vault object for the given name.

Parameters:

  • vault (String)

    The vault’s name

Returns:



71
72
73
74
# File 'lib/gemical/auth.rb', line 71

def find_vault(name)
  ensure_vaults!
  vaults.find {|v| v == name }
end

#primary_vaultString

Returns The user’s primary vault setting.

Returns:

  • (String)

    The user’s primary vault setting



61
62
63
64
65
# File 'lib/gemical/auth.rb', line 61

def primary_vault
  return nil unless configuration.primary_vault?
  value = configuration.primary_vault.read.split(/\s+/).first
  value if string_value?(value)
end

#vaultsArray<Gemical::Vault>

Returns Available vaults.

Returns:



38
39
40
41
42
43
# File 'lib/gemical/auth.rb', line 38

def vaults
  verify_account!
  @vaults ||= Gemical.connection.get("/vaults").map do |hash|
    Gemical::Vault.new(hash)
  end
end

#verify_account!Hash

Verifies account, asks for credentials if not setup

Returns:

  • (Hash)

    Account information



13
14
15
16
17
18
19
20
21
# File 'lib/gemical/auth.rb', line 13

def verify_account!
  @verified_account ||= if credentials
    
  else
    say_ok "\nWelcome to Gemical! You are new, right?"
    say_ok "We need to authenticate your account:"
    collect_credentials
  end
end

#verify_vault!Hash

Verifies account, asks for primary choice if not setup

Returns:

  • (Hash)

    Vault information



26
27
28
29
30
31
32
33
34
# File 'lib/gemical/auth.rb', line 26

def verify_vault!
  verify_account!

  @verified_vault ||= if primary_vault
    find_vault(primary_vault) || collect_vault("Ooops, you don't seem to have access to '#{primary_vault}' anymore.")
  else
    collect_vault("You haven't specified your primary vault yet.")
  end
end