Module: NmDatafile::DataLoading

Included in:
NmDatafile
Defined in:
lib/nm_datafile/data_loading.rb

Instance Method Summary collapse

Instance Method Details

#determine_file_type(attributes_hash) ⇒ Object



28
29
30
31
# File 'lib/nm_datafile/data_loading.rb', line 28

def determine_file_type(attributes_hash)
  attributes_hash = YAML::load attributes_hash
  attributes_hash["file_type"].to_sym
end

#determine_password(hash, symmetric_key) ⇒ Object



34
35
36
37
# File 'lib/nm_datafile/data_loading.rb', line 34

def determine_password(hash, symmetric_key)
  d = YAML::load hash[:encryption]
  ::NmDatafile.clean_decrypt_string(d["password"], symmetric_key)
end

#extract_entities_from_binary_data(binary_data, symmetric_key) ⇒ Object

This method peers through a zip binary data blob and returns a hash consisting of { file_name1: file_data1, etc }



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nm_datafile/data_loading.rb', line 41

def extract_entities_from_binary_data(binary_data, symmetric_key)
  binary_data_io = StringIO.new(binary_data)
  
  hash = {}
  ::Zip::InputStream.open(binary_data_io) do |io|
    while (entry = io.get_next_entry)
      hash[entry.name.to_sym] = io.read
    end
  end
  
  password = self.determine_password(hash, symmetric_key)
  
  decrypt_encryptable_data!(password, hash)
  hash
end

#Load(file_path, symmetric_key) ⇒ Object

(m) Load: loads a file into memory as an NmDatafile TODO: Make lowercase



7
8
9
10
# File 'lib/nm_datafile/data_loading.rb', line 7

def Load(file_path, symmetric_key)
  zip_data = File.read(file_path)
  load_binary_data(zip_data, symmetric_key)
end

#load_binary_data(binary_data, symmetric_key) ⇒ Object

TODO: Make lowercase



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nm_datafile/data_loading.rb', line 13

def load_binary_data(binary_data, symmetric_key)
  hash = extract_entities_from_binary_data(binary_data, symmetric_key)
  
  config = { file_type: determine_file_type(hash[:attributes]),
             symmetric_key: symmetric_key
  }
  
  nmd = NmDatafile.new( config )
  
  nmd.load_attributes(hash[:attributes]) unless hash[:attributes].nil?
  nmd.load_encryption(hash[:encryption])
  
  nmd.load_data([*hash[:data_collections], *hash[:data_objects]])
end