Class: FileSecrets

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = nil) ⇒ FileSecrets

Create a new file secrets accessor. The files will be loaded relative to the provided base directory. If none is provided, it will default to using the directory set in the FILE_SECRETS_BASE environment variable or the current working directory.



18
19
20
# File 'lib/file_secrets.rb', line 18

def initialize(base = nil)
  @base = (base || ENV['FILE_SECRETS_BASE'] || Dir.pwd)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



3
4
5
# File 'lib/file_secrets.rb', line 3

def base
  @base
end

Class Method Details

.[]Object

Get a file value using from the default base.



11
12
13
# File 'lib/file_secrets.rb', line 11

def get(key)
  new.get(key)
end

.get(key) ⇒ Object

Get a file value using from the default base.



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

def get(key)
  new.get(key)
end

Instance Method Details

#get(key) ⇒ Object Also known as: []

Get the contents of the file specified (relative to the base directory). If the file does not exist, this method will return nil. Any trailing line delimiters will be stripped from the returned value.



25
26
27
28
29
30
31
32
33
# File 'lib/file_secrets.rb', line 25

def get(key)
  file_name = File.join(key.split('/'))
  file_path = File.expand_path(file_name, base)
  if File.exist?(file_path) && File.file?(file_path)
    File.read(file_path).chomp
  else
    nil
  end
end