Class: Bcome::Node::MetaDataLoader

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/objects/node/meta_data_loader.rb

Constant Summary collapse

META_DATA_FILE_PATH_PREFIX =
'bcome/metadata'.freeze

Instance Method Summary collapse

Constructor Details

#initializeMetaDataLoader

Returns a new instance of MetaDataLoader.



7
8
9
# File 'lib/objects/node/meta_data_loader.rb', line 7

def initialize
  @all_metadata_filenames = Dir["#{META_DATA_FILE_PATH_PREFIX}/*"]
end

Instance Method Details

#dataObject



15
16
17
# File 'lib/objects/node/meta_data_loader.rb', line 15

def data
  @data ||= do_load
end

#data_for_namespace(namespace) ⇒ Object



19
20
21
# File 'lib/objects/node/meta_data_loader.rb', line 19

def data_for_namespace(namespace)
  data[namespace.to_sym] ? data[namespace.to_sym] : {}
end

#decryption_keyObject



11
12
13
# File 'lib/objects/node/meta_data_loader.rb', line 11

def decryption_key
  @decryption_key
end

#do_loadObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/objects/node/meta_data_loader.rb', line 39

def do_load
   = {}
  @all_metadata_filenames.each do |filepath|
    next if filepath =~ /-unenc/  # we only read from the encrypted, packed files. 

    begin
      filedata = load_file_data_for(filepath)
      .deep_merge!(filedata)
    rescue Psych::SyntaxError => e
      raise Bcome::Exception::InvalidMetaDataConfig, "Error: #{e.message}"
    end
  end
  
end

#load_file_data_for(filepath) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/objects/node/meta_data_loader.rb', line 28

def load_file_data_for(filepath)
 if filepath =~ /.enc/  # encrypted file contents 
    prompt_for_decryption_key unless decryption_key
    encrypted_contents = File.read(filepath)     
    decrypted_contents = encrypted_contents.decrypt(decryption_key) 
    return YAML.load(decrypted_contents)
  else # unencrypted
    return YAML.load_file(filepath)
  end
end

#prompt_for_decryption_keyObject



23
24
25
26
# File 'lib/objects/node/meta_data_loader.rb', line 23

def prompt_for_decryption_key
  message = "Please enter your metadata encryption key: ".informational
  @decryption_key = ::Readline.readline("\n#{message}", true).squeeze('').to_s
end