Module: Keyth
- Defined in:
- lib/keyth.rb
Overview
Keyhandling module with various functions for keeping keys out of configuration files.
Class Method Summary collapse
- .add_key(key, value) ⇒ Object
- .delete_key(key) ⇒ Object
- .fetch_keys(obj) ⇒ Object
- .get_key(key) ⇒ Object
- .get_key_safe(key) ⇒ Object
- .keyfile_location ⇒ Object
- .keys(application = nil) ⇒ Object
- .load_keyfile ⇒ Object
- .load_yaml(file) ⇒ Object
- .namespace(namespace) ⇒ Object
- .save_keyfile ⇒ Object
Class Method Details
.add_key(key, value) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/keyth.rb', line 19 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
27 28 29 30 31 32 33 |
# File 'lib/keyth.rb', line 27 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/keyth.rb', line 52 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 do |v| fetch_keys(v) end when obj.is_a?(String) obj.gsub!(/^keyth\:(.*)/) { get_key(Regexp.last_match[1]) } end obj end |
.get_key(key) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/keyth.rb', line 7 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
15 16 17 |
# File 'lib/keyth.rb', line 15 def self.get_key_safe(key) get_key(key) rescue nil end |
.keyfile_location ⇒ Object
75 76 77 78 |
# File 'lib/keyth.rb', line 75 def self.keyfile_location @namespace = 'default' unless @namespace ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml') end |
.keys(application = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/keyth.rb', line 35 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_keyfile ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/keyth.rb', line 80 def self.load_keyfile if File.file?(keyfile_location) @key_list = YAML.load(File.open(keyfile_location)) else @key_list = {} end end |
.load_yaml(file) ⇒ Object
46 47 48 49 50 |
# File 'lib/keyth.rb', line 46 def self.load_yaml(file) load_keyfile unless @key_list keys = YAML.load(file) fetch_keys(keys) end |
.namespace(namespace) ⇒ Object
69 70 71 72 73 |
# File 'lib/keyth.rb', line 69 def self.namespace(namespace) @namespace = namespace @keylist = nil @namespace end |
.save_keyfile ⇒ Object
88 89 90 91 |
# File 'lib/keyth.rb', line 88 def self.save_keyfile load_keyfile unless @key_list File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml } end |