keyth 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/keyth/dotenv.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  module Dotenv
4
4
  # The two apply functions are all that need to be overwritten
5
5
  class Environment
6
+ # sets all environment variables that are keyth: links with the
7
+ # appropriate key value, sets all missing env variables otherwise
6
8
  def apply
7
9
  each do |k, v|
8
10
  if v =~ /^keyth\:(.*)/
@@ -13,7 +15,10 @@ module Dotenv
13
15
  end
14
16
  end
15
17
 
16
- def apply
18
+ # sets all environment variables that are keyth: links with the
19
+ # appropriate key value, overwrites all env variables with the
20
+ # contents from .env otherwise
21
+ def apply!
17
22
  each do |k, v|
18
23
  if v =~ /^keyth\:(.*)/
19
24
  ENV[k] = Keyth.get_key_safe(Regexp.last_match[1]) || ''
data/lib/keyth/yaml.rb CHANGED
@@ -5,7 +5,11 @@ module YAML
5
5
  alias_method(:pre_keyth_load, :load)
6
6
  end
7
7
 
8
+ # loads the yaml file, then replaces all keyth: links with the
9
+ # appropriate values.
10
+ # Params:
11
+ # +file+:: file object containing YAML to read
8
12
  def self.load(file)
9
13
  Keyth.fetch_keys(pre_keyth_load(file))
10
14
  end
11
- end
15
+ end
data/lib/keyth.rb CHANGED
@@ -6,6 +6,9 @@ require 'keyth/dotenv'
6
6
  # Keyhandling module with various functions for keeping keys
7
7
  # out of configuration files.
8
8
  module Keyth
9
+ # Retrieves a key from the store, raising errors if the key is missing
10
+ # Params:
11
+ # +key+:: name of the key (without the keyth: prefix)
9
12
  def self.get_key(key)
10
13
  load_keyfile unless @key_list
11
14
  application, key_name = key.split('/')
@@ -14,10 +17,17 @@ module Keyth
14
17
  @key_list[application][key_name]
15
18
  end
16
19
 
20
+ # Retrieves a key from the store, returning nil if the key is missing
21
+ # Params:
22
+ # +key+:: name of the key (without the keyth: prefix)
17
23
  def self.get_key_safe(key)
18
24
  get_key(key) rescue nil
19
25
  end
20
26
 
27
+ # Adds a key to the store
28
+ # Params:
29
+ # +key+:: name of the key (without the keyth: prefix)
30
+ # +value+:: the key value
21
31
  def self.add_key(key, value)
22
32
  load_keyfile unless @key_list
23
33
  application, key_name = key.split('/')
@@ -26,6 +36,9 @@ module Keyth
26
36
  save_keyfile
27
37
  end
28
38
 
39
+ # Removes a key from the store
40
+ # Params:
41
+ # +key+:: name of the key (without the keyth: prefix)
29
42
  def self.delete_key(key)
30
43
  load_keyfile unless @key_list
31
44
  application, key_name = key.split('/')
@@ -34,6 +47,9 @@ module Keyth
34
47
  save_keyfile
35
48
  end
36
49
 
50
+ # Gets a list of keys in the store
51
+ # Params:
52
+ # +application+:: if not nil, only return keys where the part of the key before the slash matches.
37
53
  def self.keys(application = nil)
38
54
  load_keyfile unless @key_list
39
55
  keys = {}
@@ -45,12 +61,18 @@ module Keyth
45
61
  keys
46
62
  end
47
63
 
64
+ # Reads a YAML file, automatically retrieving keys for any value prefixed with "keyth:"
65
+ # Params:
66
+ # +file+:: file object containing YAML to read
48
67
  def self.load_yaml(file)
49
68
  load_keyfile unless @key_list
50
69
  keys = YAML.pre_keyth_load(file)
51
70
  fetch_keys(keys)
52
71
  end
53
72
 
73
+ # Fixes a string, array-alike, or hash-alike by automatically retrieving keys for any value prefixed with "keyth:"
74
+ # Params:
75
+ # +obj+:: the object to fix
54
76
  def self.fetch_keys(obj)
55
77
  load_keyfile unless @key_list
56
78
  case
@@ -63,7 +85,7 @@ module Keyth
63
85
  obj[i] = fetch_keys(v)
64
86
  end
65
87
  when obj.is_a?(String)
66
- obj = obj.gsub(/^keyth\:(.*)/) { get_key(Regexp.last_match[1]) }
88
+ obj = obj.gsub(/^keyth\:(.*)/) { get_key_safe(Regexp.last_match[1]) || "Missing Key: [#{obj}]" }
67
89
  end
68
90
  obj
69
91
  end
@@ -78,11 +100,8 @@ module Keyth
78
100
  @namespace
79
101
  end
80
102
 
81
- def self.keyfile_location
82
- @namespace = 'default' unless @namespace
83
- ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
84
- end
85
-
103
+ # Load the keyfile. By default, the keystore is loaded if necessary by
104
+ # the using functions, so it is unnecessary to call this directly.
86
105
  def self.load_keyfile
87
106
  if File.file?(keyfile_location)
88
107
  @key_list = YAML.pre_keyth_load(File.open(keyfile_location))
@@ -91,12 +110,17 @@ module Keyth
91
110
  end
92
111
  end
93
112
 
113
+ # Save the keyfile. By default, the keystore is saved when changes are
114
+ # made to it, so it is unnecessary to call this directly.
94
115
  def self.save_keyfile
95
116
  load_keyfile unless @key_list
96
117
  File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml }
97
118
  end
98
- end
99
-
100
-
101
119
 
120
+ private
102
121
 
122
+ def self.keyfile_location
123
+ @namespace = 'default' unless @namespace
124
+ ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
125
+ end
126
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-17 00:00:00.000000000 Z
12
+ date: 2014-08-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Handles named keys for use in config files
15
15
  email: keith@kludge.co.uk