Class: Longleaf::ApplicationConfigDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/services/application_config_deserializer.rb

Overview

Deserializer for application configuration files

Class Method Summary collapse

Class Method Details

.absolution(base_pathname, file_path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/longleaf/services/application_config_deserializer.rb', line 88

def self.absolution(base_pathname, file_path)
  if file_path.nil?
    nil
  else
    path = Pathname.new(file_path)
    if path.absolute?
      path = path.expand_path.to_s
    else
      path = (base_pathname + path).to_s
    end
  end
end

.deserialize(config_path, format: 'yaml') ⇒ Object

Deserializes a valid application configuration file as a ApplicationConfigManager option return [ApplicationConfigManager] manager for the loaded configuration

Parameters:

  • config_path (String)

    file path to the service and storage mapping configuration file

  • format (String) (defaults to: 'yaml')

    encoding format of the config file



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/longleaf/services/application_config_deserializer.rb', line 15

def self.deserialize(config_path, format: 'yaml')
  content = load_config_file(config_path)
  config = load(content, format)

  config_md5 = Digest::MD5.hexdigest(content)

  make_paths_absolute(config_path, config)

  ApplicationConfigValidator.new(config).validate_config.raise_if_invalid
  ApplicationConfigManager.new(config, config_md5)
end

.from_yaml(content) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/longleaf/services/application_config_deserializer.rb', line 49

def self.from_yaml(content)
  begin
    YAML.safe_load(content, [], [], true)
  rescue => err
    raise Longleaf::ConfigurationError.new(err)
  end
end

.load(content, format) ⇒ Object

Deserialize a configuration file into a hash return [Hash] hash containing the configuration

Parameters:

  • content (String)

    the contents of the application configuration file

  • format (String)

    encoding format of the config file



40
41
42
43
44
45
46
47
# File 'lib/longleaf/services/application_config_deserializer.rb', line 40

def self.load(content, format)
  case format
  when 'yaml'
    from_yaml(content)
  else
    raise ArgumentError.new("Invalid deserialization format #{format} specified")
  end
end

.make_file_paths_absolute(base_pathname, properties) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/longleaf/services/application_config_deserializer.rb', line 75

def self.make_file_paths_absolute(base_pathname, properties)
  path = properties[AF::LOCATION_PATH]
  return nil if path.nil?

  uri = URI(path)

  if uri.scheme.nil? || uri.scheme.casecmp("file") == 0
    absolution(base_pathname, uri.path)
  else
    path
  end
end

.make_paths_absolute(config_path, config) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/longleaf/services/application_config_deserializer.rb', line 57

def self.make_paths_absolute(config_path, config)
  base_pathname = Pathname.new(config_path).expand_path.parent

  config[AF::LOCATIONS].each do |name, properties|
    properties[AF::LOCATION_PATH] = make_file_paths_absolute(base_pathname, properties)

    # Resolve single field metadata location into expanded form
    md_config = properties[AF::METADATA_CONFIG]
    if md_config.nil?
      next
    end
    if md_config.is_a?(String)
      md_config = { AF::LOCATION => m_config }
    end
    md_config[AF::LOCATION_PATH] = make_file_paths_absolute(base_pathname, md_config)
  end
end