Class: Saper::Keychain

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/keychain.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services = {}, options = {}) ⇒ Saper::Keychain

Returns a new keychain instance


34
35
36
# File 'lib/saper/core/keychain.rb', line 34

def initialize(services = {}, options = {})
  @services = Hash[services.map { |k,v| [k.to_s, v] }]
end

Class Method Details

.load(path, options = {}) ⇒ Object

TODO:

5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/saper/core/keychain.rb', line 5

def self.load(path, options = {})
  case File.extname(path)
  when '.yaml'
    load_yaml(path)
  when '.yml'
    load_yaml(path)
  when '.json'
    load_json(path)
  else
    nil
  end
end

.load_json(path) ⇒ Object

TODO:

26
27
28
29
30
# File 'lib/saper/core/keychain.rb', line 26

def self.load_json(path)
  new JSON.parse(IO.read(path), :symbolize_names => true)
rescue
  nil
end

.load_yaml(path) ⇒ Object

TODO:

19
20
21
22
23
# File 'lib/saper/core/keychain.rb', line 19

def self.load_yaml(path)
  new YAML.load_file(path)
rescue
  nil
end

Instance Method Details

#[](service) ⇒ Hash?

Returns access credentials for the specified service.

Parameters:

  • service (String, Symbol)

    service name

Returns:

  • (Hash, nil)

41
42
43
# File 'lib/saper/core/keychain.rb', line 41

def [](service)
  @services[service.to_s] || {}
end

#has?(service) ⇒ Boolean

TODO:

Returns:

  • (Boolean)

61
62
63
# File 'lib/saper/core/keychain.rb', line 61

def has?(service)
  !self[service].empty?
end

#login(service) ⇒ Object

TODO:

46
47
48
# File 'lib/saper/core/keychain.rb', line 46

def (service)
  self[service][:login]
end

#password(service) ⇒ Object

TODO:

51
52
53
# File 'lib/saper/core/keychain.rb', line 51

def password(service)
  self[service][:password]
end

#servicesObject

TODO:

66
67
68
# File 'lib/saper/core/keychain.rb', line 66

def services
  @services.keys.select { |service| has?(service) }
end

#token(service) ⇒ Object

TODO:

56
57
58
# File 'lib/saper/core/keychain.rb', line 56

def token(service)
  self[service][:token]
end