Module: Keyth

Defined in:
lib/keyth.rb

Overview

Keyhandling module with various functions for keeping keys out of configuration files.

Class Method Summary collapse

Class Method Details

.add_key(key, value) ⇒ Object

Adds a key to the store Params:

key

name of the key (without the keyth: prefix)

value

the key value



31
32
33
34
35
36
37
# File 'lib/keyth.rb', line 31

def self.add_key(key, value)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  @key_list[application] = {} unless @key_list.key?(application)
  @key_list[application][key_name] = value
  save_keyfile
end

.delete_key(key) ⇒ Object

Removes a key from the store Params:

key

name of the key (without the keyth: prefix)



42
43
44
45
46
47
48
# File 'lib/keyth.rb', line 42

def self.delete_key(key)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  @key_list[application].delete(key_name)
  @key_list.delete(application) if @key_list[application].empty?
  save_keyfile
end

.fetch_keys(obj) ⇒ Object Also known as: apply_to

Fixes a string, array-alike, or hash-alike by automatically retrieving keys for any value prefixed with “keyth:” Params:

obj

the object to fix



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/keyth.rb', line 76

def self.fetch_keys(obj)
  load_keyfile unless @key_list
  case
  when obj.respond_to?(:keys)
    obj.each do |k, v|
      obj[k] = fetch_keys(v)
    end
  when obj.respond_to?(:each)
    obj.each_with_index do |v, i|
      obj[i] = fetch_keys(v)
    end
  when obj.is_a?(String)
    obj = obj.gsub(/^keyth\:(.*)/) { get_key_safe(Regexp.last_match[1]) || "Missing Key: [#{obj}]" }
  end
  obj
end

.get_key(key) ⇒ Object

Retrieves a key from the store, raising errors if the key is missing Params:

key

name of the key (without the keyth: prefix)



12
13
14
15
16
17
18
# File 'lib/keyth.rb', line 12

def self.get_key(key)
  load_keyfile unless @key_list
  application, key_name = key.split('/')
  fail "Application not found: #{application}!" unless @key_list[application]
  fail "Key not found: #{key}!" unless @key_list[application][key_name]
  @key_list[application][key_name]
end

.get_key_safe(key) ⇒ Object

Retrieves a key from the store, returning nil if the key is missing Params:

key

name of the key (without the keyth: prefix)



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

def self.get_key_safe(key)
  get_key(key) rescue nil
end

.keys(application = nil) ⇒ Object

Gets a list of keys in the store Params:

application

if not nil, only return keys where the part of the key before the slash matches.



53
54
55
56
57
58
59
60
61
62
# File 'lib/keyth.rb', line 53

def self.keys(application = nil)
  load_keyfile unless @key_list
  keys = {}
  @key_list.each do |k1, v|
    v.each do |k2, v2|
      keys[k1 + '/' + k2] = v2 if k1 == application || application.nil?
    end
  end
  keys
end

.load_keyfileObject

Load the keyfile. By default, the keystore is loaded if necessary by the using functions, so it is unnecessary to call this directly.



105
106
107
108
109
110
111
# File 'lib/keyth.rb', line 105

def self.load_keyfile
  if File.file?(keyfile_location)
    @key_list = YAML.pre_keyth_load(File.open(keyfile_location))
  else
    @key_list = {}
  end
end

.load_yaml(file) ⇒ Object

Reads a YAML file, automatically retrieving keys for any value prefixed with “keyth:” Params:

file

file object containing YAML to read



67
68
69
70
71
# File 'lib/keyth.rb', line 67

def self.load_yaml(file)
  load_keyfile unless @key_list
  keys = YAML.pre_keyth_load(file)
  fetch_keys(keys)
end

.namespace(namespace) ⇒ Object



97
98
99
100
101
# File 'lib/keyth.rb', line 97

def self.namespace(namespace)
  @namespace = namespace
  @keylist = nil
  @namespace
end

.save_keyfileObject

Save the keyfile. By default, the keystore is saved when changes are made to it, so it is unnecessary to call this directly.



115
116
117
118
# File 'lib/keyth.rb', line 115

def self.save_keyfile
  load_keyfile unless @key_list
  File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml }
end