Class: CocoaPodsKeys::Keyring

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, keys = []) ⇒ Keyring



9
10
11
12
13
# File 'lib/keyring.rb', line 9

def initialize(name, path, keys = [])
  @name = name.to_s
  @path = path.to_s
  @keys = keys
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



7
8
9
# File 'lib/keyring.rb', line 7

def keys
  @keys
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/keyring.rb', line 7

def name
  @name
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/keyring.rb', line 7

def path
  @path
end

Class Method Details

.from_hash(hash) ⇒ Object



15
16
17
# File 'lib/keyring.rb', line 15

def self.from_hash(hash)
  new(hash['name'], hash['path'], hash['keys'])
end

.keychain_prefixObject



27
28
29
# File 'lib/keyring.rb', line 27

def self.keychain_prefix
  'cocoapods-keys-'
end

Instance Method Details

#camel_cased_keysObject



67
68
69
# File 'lib/keyring.rb', line 67

def camel_cased_keys
  Hash[keychain_data.map { |(key, value)| [key[0].downcase + key[1..-1], value] }]
end

#code_nameObject



23
24
25
# File 'lib/keyring.rb', line 23

def code_name
  name.split(/[^a-zA-Z0-9_]/).map { |s| s[0].upcase + s[1..-1] }.join('')
end

#keychainObject



31
32
33
# File 'lib/keyring.rb', line 31

def keychain
  @keychain ||= Keychain.generic_passwords
end

#keychain_dataObject



45
46
47
48
49
# File 'lib/keyring.rb', line 45

def keychain_data
  Hash[
    @keys.map { |key| [key, keychain_value(key)] }
  ]
end

#keychain_has_key?(key) ⇒ Boolean



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/keyring.rb', line 51

def keychain_has_key?(key)
  has_key = !keychain_value(key).nil?

  if has_key && !@keys.include?(key)
    @keys << key
  elsif !has_key && @keys.include?(key)
    @keys.delete(key)
  end

  has_key
end

#keychain_value(key) ⇒ Object



63
64
65
# File 'lib/keyring.rb', line 63

def keychain_value(key)
  ENV[key] || keychain.where(service: self.class.keychain_prefix + name, account: key).first.password
end

#save(key, value) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/keyring.rb', line 35

def save(key, value)
  item = keychain.where(service: self.class.keychain_prefix + name, account: key).first
  if item
    item.password = value
    item.save!
  else
    keychain_has_keykeychain.create(service: self.class.keychain_prefix + name, password: value, account: key)
  end
end

#to_hashObject



19
20
21
# File 'lib/keyring.rb', line 19

def to_hash
  { 'keys' => @keys, 'path' => @path, 'name' => @name }
end