Class: FileManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb

Overview

This module provides methods that perform the store and retrieve operations on the file based cache of the SDK.

Instance Method Summary collapse

Constructor Details

#initializeFileManager

Returns a new instance of FileManager.



25
26
27
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb', line 25

def initialize
  @logger = Logger.instance
end

Instance Method Details

#delete_file_data(file_path) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb', line 65

def delete_file_data(file_path)
  return unless File.exist?(file_path)

  File.truncate(file_path, 0)
rescue StandardError => e
  @logger.warning(e)
end

#read_bootstrap_configurations_from_file(file_path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb', line 51

def read_bootstrap_configurations_from_file(file_path)
  raise "given bootstrap file path doesn't exist: #{file_path}" unless File.exist?(file_path)

  data = File.read(file_path).strip

  raise "given bootstrap file is empty: #{file_path}" if data.empty?

  begin
    data
  rescue StandardError => e
    raise "failed to parse the json from the given bootstrap file: #{file_path}. Error #{e}"
  end
end

#read_persistent_cache_configurations(file_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb', line 33

def read_persistent_cache_configurations(file_path)
  unless File.exist?(file_path)
    @logger.log("configuration file in the persistent cache doesn't exist")
    return ""
  end

  data = File.read(file_path).strip

  if data.empty?
    @logger.log("configuration file in the persistent cache is empty")
    return ""
  end

  data
rescue StandardError
  ""
end

#store_files(json, file_path) ⇒ Object



29
30
31
# File 'lib/ibm_appconfiguration_ruby_sdk/configurations/internal/file_manager.rb', line 29

def store_files(json, file_path)
  File.write(file_path, json)
end