consul_application_settings 0.1.3 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2781ea5f69c58f31fd715aee2077fe95d20bff9cba28ba51581a7241f8a4bd1
4
- data.tar.gz: 5a1143fdb696ec589688c89c76488e3d06a6627a37115999e111231d2d688388
3
+ metadata.gz: 8e6c1820af05af3bcc9adca21879296cbcfc8898e69ffa7136bac1d785ae1bb1
4
+ data.tar.gz: 0e8b5c29bf2992a6d36e7a064b8887922b69ec5f820073157b53a21dc5905089
5
5
  SHA512:
6
- metadata.gz: 245f2bb1637928d4c5e61cdbe993a584da5e73f62f34d9ea1c1608fb45c223882db9154bc4f676d2cf0f23fb7529a81eef0a33b35a2c7148b9b57746f18f2586
7
- data.tar.gz: 36b25f0e8bf076f72f8f60cf54708f3622c5c1ed18b11f9049a86a7f76157db74dca025a1da1c5f4819b8affc42146ebd0c19281a67d5834ddd7c1f1119fcf20
6
+ metadata.gz: 976fec73196a99d0a642d9f4d9243beb0a0925f44f140ff6e77091dac1919f36472b1467344e190da01b071803412d6ec612ccb31f8751a9d06d3f471a49fd1f
7
+ data.tar.gz: e0d2e02025e94d2d5fc6ac4dd24a4073f9ac228969e67afbd9afb5cafa297c6a347dd68a53fac9a4ad6b4c2ce875b036372894b175e1bd36e6db138c1b31e9d9
@@ -39,13 +39,15 @@ jobs:
39
39
  - name: Set up Dependencies
40
40
  run: bundle install --path vendor/bundle
41
41
 
42
- - name: Run Tests
43
- uses: paambaati/codeclimate-action@v2.3.0
42
+ - name: Run specs
44
43
  env:
45
44
  COVERAGE: true
46
- CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
47
- with:
48
- coverageCommand: ./bin/rspec
45
+ run: bundle exec rspec
46
+
47
+ - name: Upload coverage
48
+ env:
49
+ CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
50
+ run: bash <(curl -s https://codecov.io/bash)
49
51
 
50
52
  release:
51
53
  runs-on: ubuntu-latest
data/.simplecov CHANGED
@@ -1 +1,6 @@
1
- SimpleCov.start if ENV["COVERAGE"]
1
+ if ENV["COVERAGE"]
2
+ require 'simplecov'
3
+ require 'codecov'
4
+ SimpleCov.start
5
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
6
+ end
data/Gemfile.lock CHANGED
@@ -2,29 +2,34 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  consul_application_settings (0.0.0)
5
- diplomat (~> 2.1.3)
5
+ diplomat (~> 2.5.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.0)
11
+ codecov (0.4.3)
12
+ simplecov (>= 0.15, < 0.22)
11
13
  deep_merge (1.2.1)
12
14
  diff-lcs (1.3)
13
- diplomat (2.1.3)
14
- deep_merge (~> 1.0, >= 1.0.1)
15
- faraday (~> 0.9)
15
+ diplomat (2.5.1)
16
+ deep_merge (~> 1.2)
17
+ faraday (>= 0.9)
16
18
  docile (1.3.1)
17
- faraday (0.17.3)
19
+ faraday (1.3.0)
20
+ faraday-net_http (~> 1.0)
18
21
  multipart-post (>= 1.2, < 3)
22
+ ruby2_keywords
23
+ faraday-net_http (1.0.1)
19
24
  jaro_winkler (1.5.2)
20
- json (2.2.0)
25
+ json (2.3.1)
21
26
  multipart-post (2.1.1)
22
27
  parallel (1.16.2)
23
28
  parser (2.6.2.0)
24
29
  ast (~> 2.4.0)
25
30
  psych (3.1.0)
26
31
  rainbow (3.0.0)
27
- rake (10.5.0)
32
+ rake (13.0.1)
28
33
  rspec (3.8.0)
29
34
  rspec-core (~> 3.8.0)
30
35
  rspec-expectations (~> 3.8.0)
@@ -49,6 +54,7 @@ GEM
49
54
  rubocop-rspec (1.32.0)
50
55
  rubocop (>= 0.60.0)
51
56
  ruby-progressbar (1.10.0)
57
+ ruby2_keywords (0.0.4)
52
58
  simplecov (0.16.1)
53
59
  docile (~> 1.1)
54
60
  json (>= 1.8, < 3)
@@ -61,8 +67,9 @@ PLATFORMS
61
67
 
62
68
  DEPENDENCIES
63
69
  bundler (~> 2.0)
70
+ codecov (~> 0.4)
64
71
  consul_application_settings!
65
- rake (~> 10.0)
72
+ rake (~> 13.0)
66
73
  rspec (~> 3.0)
67
74
  rubocop (~> 0.66)
68
75
  rubocop-rspec (~> 1.32.0)
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # ConsulApplicationSettings
2
2
 
3
3
  ![Build Status](https://github.com/matic-insurance/consul_application_settings/workflows/ci/badge.svg?branch=master)
4
- [![Test Coverage](https://api.codeclimate.com/v1/badges/b0eaebcf83898535ea4e/test_coverage)](https://codeclimate.com/github/matic-insurance/consul_application_settings/test_coverage)
5
- [![Maintainability](https://api.codeclimate.com/v1/badges/b0eaebcf83898535ea4e/maintainability)](https://codeclimate.com/github/matic-insurance/consul_application_settings/maintainability)
4
+ [![Test Coverage](https://codecov.io/gh/matic-insurance/consul_application_settings/branch/master/graph/badge.svg?token=5E8NA8EE8L)](https://codecov.io/gh/matic-insurance/consul_application_settings)
6
5
 
7
6
  Gem that simplifies usage of Consul (via [Diplomat gem](https://github.com/WeAreFarmGeek/diplomat))
8
7
  to host application settings. Gem provides defaults via yaml files and other utilities
@@ -23,7 +22,7 @@ Example use cases:
23
22
 
24
23
  Gem reads any particular setting from consul and if it is missing tries to find value in YAML defaults file
25
24
 
26
- **NOTE** Consul is requested every time you query the settings. Defaults YAML file loaded in memory and not changing.
25
+ **NOTE** Consul is requested every time you query the settings. Defaults YAML file is loaded in memory and is not changing.
27
26
 
28
27
  ## Installation
29
28
 
@@ -40,13 +39,17 @@ gem 'consul_application_settings'
40
39
  At the load of application:
41
40
  ```ruby
42
41
  ConsulApplicationSettings.configure do |config|
43
- # Specify path to defaults file
44
- config.defaults = Rails.root.join('config/settings.yml')
45
- # Specify namespace to consul settings
46
- config.namespace = 'staging/my_cool_app'
42
+ # Specify path to the base settings YML. Default: 'config/application_settings.yml'
43
+ config.base_file_path = Rails.root.join('config/my_settings.yml')
44
+ # Specify path to the local settings YML, which overrides the base file. Default: 'config/application_settings.local.yml'
45
+ config.local_file_path = Rails.root.join('config/my_settings.local.yml')
46
+ # Specify whether exceprion should be thrown on Consul connection errors. Default: false
47
+ config.disable_consul_connection_errors = true
47
48
  end
48
49
 
49
50
  APP_SETTINGS = ConsulApplicationSettings.load
51
+ # Specify path to settings both in YML files and Consul
52
+ AUTH_SETTIGNS = ConsulApplicationSettings.load('authentication')
50
53
  ```
51
54
 
52
55
  **NOTE** For rails you can add this code to custom initializer `console_application_settings.rb` in `app/config/initializers`
@@ -55,7 +58,7 @@ APP_SETTINGS = ConsulApplicationSettings.load
55
58
 
56
59
  ### Settings structure
57
60
 
58
- Assuming your defaults file in repository `config/settings.yml` looks like:
61
+ Assuming your defaults file in repository `config/application_settings.yml` looks like:
59
62
  ```yaml
60
63
  staging:
61
64
  my_cool_app:
@@ -98,15 +101,13 @@ Anywhere in your code base, after initialization, you can use
98
101
  previously loaded settings to query any key by full path
99
102
 
100
103
  ```ruby
101
- APP_SETTINGS.app_name # "MyCoolApp"
104
+ APP_SETTINGS['app_name'] # "MyCoolApp"
102
105
  APP_SETTINGS.get(:hostname) # "https://mycoolapp.com"
103
106
 
104
107
  APP_SETTINGS.get('integrations/database/user') # "app"
105
108
  APP_SETTINGS['integrations/slack/enabled'] # true
106
109
  ```
107
110
 
108
- **NOTE** Gem is pulling settings from consul with namespace but ignores namespace for defaults
109
-
110
111
  ### Nested settings
111
112
 
112
113
  Assuming some part of your code needs to work with smaller part of settings -
@@ -114,36 +115,30 @@ gem provides interface to avoid duplicating absolute path
114
115
 
115
116
  ```ruby
116
117
  # You can load subsettings from root object
117
- db_settings = APP_SETTINGS.load_from('integrations/database')
118
- db_settings.domain # "194.78.92.19"
119
- db_settings['user'] # "app"
120
-
121
- # You can load subsettings from subsettings
122
- integrations_settings = APP_SETTINGS.load_from('integrations')
123
- slack_settings = integrations_settings.load_from('slack')
124
- slack_settings.enabled # true
125
- slack_settings.get('webhook_url') # "https://hooks.slack.com/services/XXXXXX/XXXXX/XXXXXXX"
118
+ db_settings = APP_SETTINGS.load('integrations/database')
119
+ db_settings.get(:domain) # "194.78.92.19"
120
+ db_settings['user'] # "app"
126
121
  ```
127
122
 
128
123
  ### Gem Configuration
129
124
  You can configure gem with block:
130
125
  ```ruby
131
126
  ConsulApplicationSettings.configure do |config|
132
- config.namespace = 'staging/my_cool_app'
127
+ config.local_file_path = 'config/config.yml'
133
128
  end
134
129
  ```
135
130
  or one option at a time
136
131
  ```ruby
137
- ConsulApplicationSettings.config.namespace = 'staging/my_cool_app'
132
+ ConsulApplicationSettings.config.local_file_path = 'config/config.yml'
138
133
  ```
139
134
 
140
135
  All Gem configurations
141
136
 
142
- | Configuration | Required | Default | Type | Description |
143
- |----------------------------------|----------|---------|---------|------------------------------------------------------------------------------|
144
- | defaults | yes | | String | Path to the file with default settings |
145
- | namespace | no | | String | Base path to read settings from in consul and defaults |
146
- | disable_consul_connection_errors | no | false | Boolean | Do not raise exception when consul is not available (useful for development) |
137
+ | Configuration | Required | Default | Type | Description |
138
+ |----------------------------------|----------|-----------------------------------------|---------|------------------------------------------------------------------------------|
139
+ | base_file_path | no | 'config/application_settings.yml' | String | Path to the file with base settings |
140
+ | local_file_path | no | 'config/application_settings.local.yml' | String | Path to the file with local settings overriding the base settings |
141
+ | disable_consul_connection_errors | no | true | Boolean | Do not raise exception when consul is not available (useful for development) |
147
142
 
148
143
  ## Development
149
144
 
@@ -29,12 +29,13 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ['lib']
31
31
 
32
- spec.add_dependency 'diplomat', '~> 2.1.3'
32
+ spec.add_dependency 'diplomat', '~> 2.5.1'
33
33
 
34
34
  spec.add_development_dependency 'bundler', '~> 2.0'
35
- spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
36
  spec.add_development_dependency 'rspec', '~> 3.0'
37
37
  spec.add_development_dependency 'rubocop', '~> 0.66'
38
38
  spec.add_development_dependency 'rubocop-rspec', '~> 1.32.0'
39
39
  spec.add_development_dependency 'simplecov', '~> 0.16'
40
+ spec.add_development_dependency 'codecov', '~> 0.4'
40
41
  end
@@ -1,11 +1,11 @@
1
1
  require 'consul_application_settings/version'
2
2
  require 'consul_application_settings/configuration'
3
- require 'consul_application_settings/defaults'
4
- require 'consul_application_settings/options'
3
+ require 'consul_application_settings/consul_provider'
4
+ require 'consul_application_settings/file_provider'
5
+ require 'consul_application_settings/settings_provider'
5
6
  require 'consul_application_settings/utils'
6
- require 'diplomat'
7
7
 
8
- # Main class used to configure defaults file path and load initial settings
8
+ # The gem provides possibility to load settings from Consul and automatically fall back to data stored in file system
9
9
  module ConsulApplicationSettings
10
10
  class Error < StandardError; end
11
11
 
@@ -16,19 +16,11 @@ module ConsulApplicationSettings
16
16
 
17
17
  self.config ||= ConsulApplicationSettings::Configuration.new
18
18
 
19
- class << self
20
- def configure
21
- yield(config)
22
- self.defaults = ConsulApplicationSettings::Defaults.read(config.defaults_path)
23
- end
24
-
25
- def load_from(path)
26
- settings_path = ConsulApplicationSettings::Utils.generate_path(config.namespace, path)
27
- ConsulApplicationSettings::Options.new(settings_path, defaults)
28
- end
19
+ def self.configure
20
+ yield(config)
21
+ end
29
22
 
30
- def load
31
- load_from('')
32
- end
23
+ def self.load(path = '')
24
+ SettingsProvider.new(path, config)
33
25
  end
34
26
  end
@@ -1,14 +1,14 @@
1
1
  module ConsulApplicationSettings
2
2
  # All gem configuration settings
3
3
  class Configuration
4
- # Required attributes
5
- attr_accessor :defaults_path
6
- # Optional attributes
7
- attr_accessor :namespace
8
- attr_accessor :disable_consul_connection_errors
4
+ DEFAULT_BASE_FILE_PATH = 'config/app_settings.yml'.freeze
5
+ DEFAULT_LOCAL_FILE_PATH = 'config/app_settings.local.yml'.freeze
6
+ attr_accessor :base_file_path, :local_file_path, :disable_consul_connection_errors
9
7
 
10
8
  def initialize
11
- self.namespace = ''
9
+ @base_file_path = DEFAULT_BASE_FILE_PATH
10
+ @local_file_path = DEFAULT_LOCAL_FILE_PATH
11
+ @disable_consul_connection_errors = true
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,33 @@
1
+ require 'diplomat'
2
+
3
+ module ConsulApplicationSettings
4
+ # Provides access to settings stored in Consul
5
+ class ConsulProvider
6
+ def initialize(base_path, config)
7
+ @base_path = base_path
8
+ @config = config
9
+ end
10
+
11
+ def get(path)
12
+ value = fetch_value(path)
13
+ ConsulApplicationSettings::Utils.cast_consul_value(value)
14
+ end
15
+
16
+ private
17
+
18
+ def fetch_value(path)
19
+ full_path = generate_full_path(path)
20
+ Diplomat::Kv.get(full_path, {}, :return)
21
+ rescue SystemCallError, Faraday::ConnectionFailed, Diplomat::PathNotFound => e
22
+ raise e unless disable_consul_connection_errors?
23
+ end
24
+
25
+ def generate_full_path(path)
26
+ ConsulApplicationSettings::Utils.generate_path(@base_path, path)
27
+ end
28
+
29
+ def disable_consul_connection_errors?
30
+ @config.disable_consul_connection_errors
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,54 @@
1
+ require 'yaml'
2
+
3
+ module ConsulApplicationSettings
4
+ # Provides access to settings stored in file system with support of base and local files
5
+ class FileProvider
6
+ def initialize(base_path, config)
7
+ @base_path = base_path
8
+ @config = config
9
+ load
10
+ end
11
+
12
+ def get(path)
13
+ read_path(path).clone
14
+ end
15
+
16
+ private
17
+
18
+ def load
19
+ base_yml = read_yml(base_file_path)
20
+ local_yml = read_yml(local_file_path)
21
+ @data = DeepMerge.deep_merge!(local_yml, base_yml, preserve_unmergeables: false, overwrite_arrays: true,
22
+ merge_nil_values: true)
23
+ end
24
+
25
+ def base_file_path
26
+ @config.base_file_path
27
+ end
28
+
29
+ def local_file_path
30
+ @config.local_file_path
31
+ end
32
+
33
+ def read_yml(path)
34
+ return {} unless File.exist?(path)
35
+
36
+ YAML.safe_load(IO.read(path))
37
+ rescue Psych::SyntaxError, Errno::ENOENT => e
38
+ raise ConsulApplicationSettings::Error, "Cannot read settings file at #{path}: #{e.message}"
39
+ end
40
+
41
+ def read_path(path)
42
+ full_path = ConsulApplicationSettings::Utils.generate_path(@base_path, path)
43
+ parts = ConsulApplicationSettings::Utils.decompose_path(full_path)
44
+ parts.reduce(@data, &method(:read_value))
45
+ end
46
+
47
+ def read_value(hash, key)
48
+ raise ConsulApplicationSettings::Error, 'reading arrays not implemented' if hash.is_a? Array
49
+ return {} if hash.nil?
50
+
51
+ hash.fetch(key.to_s, nil)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ module ConsulApplicationSettings
2
+ # Provides access to settings stored in Consul or in file system
3
+ class SettingsProvider
4
+ def initialize(base_path, config)
5
+ @consul_provider = ConsulProvider.new(base_path, config)
6
+ @file_provider = FileProvider.new(base_path, config)
7
+ end
8
+
9
+ def get(path)
10
+ consul_value = @consul_provider.get(path)
11
+ !consul_value.nil? && consul_value != '' ? consul_value : @file_provider.get(path)
12
+ end
13
+
14
+ alias [] get
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module ConsulApplicationSettings
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '2.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consul_application_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Volodymyr Mykhailyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-14 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diplomat
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.3
19
+ version: 2.5.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.1.3
26
+ version: 2.5.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: codecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.4'
111
125
  description: |-
112
126
  Gem that simplifies usage of Consul (via Diplomat gem) to host application settings.
113
127
  Gem provides defaults and utilities
@@ -117,7 +131,6 @@ executables: []
117
131
  extensions: []
118
132
  extra_rdoc_files: []
119
133
  files:
120
- - ".codeclimate.yml"
121
134
  - ".github/workflows/main.yml"
122
135
  - ".gitignore"
123
136
  - ".rspec"
@@ -136,8 +149,9 @@ files:
136
149
  - consul_application_settings.gemspec
137
150
  - lib/consul_application_settings.rb
138
151
  - lib/consul_application_settings/configuration.rb
139
- - lib/consul_application_settings/defaults.rb
140
- - lib/consul_application_settings/options.rb
152
+ - lib/consul_application_settings/consul_provider.rb
153
+ - lib/consul_application_settings/file_provider.rb
154
+ - lib/consul_application_settings/settings_provider.rb
141
155
  - lib/consul_application_settings/utils.rb
142
156
  - lib/consul_application_settings/version.rb
143
157
  homepage: https://github.com/matic-insurance/consul_application_settings
data/.codeclimate.yml DELETED
@@ -1,15 +0,0 @@
1
- version: 2
2
- plugins:
3
- # disabled before git-legal supports bundler 2 https://github.com/kmewhort/git.legal-codeclimate/issues/6
4
- # git-legal:
5
- # enabled: true
6
- rubocop:
7
- enabled: true
8
- channel: rubocop-0-73
9
- config:
10
- file: ".rubocop.yml"
11
- bundler-audit:
12
- enabled: true
13
- exclude_patterns:
14
- - "spec/"
15
- - "*.gemspec"
@@ -1,43 +0,0 @@
1
- require 'yaml'
2
-
3
- module ConsulApplicationSettings
4
- # Reading default file from YAML file and providing interface to query them
5
- class Defaults
6
- attr_reader :contents
7
-
8
- def initialize(hash)
9
- @contents = hash
10
- end
11
-
12
- def get(name)
13
- read_path(name, contents)
14
- end
15
-
16
- def load_from(path)
17
- keys = ConsulApplicationSettings::Utils.decompose_path(path)
18
- new_defaults = keys.reduce(contents) { |hash, key| read_path(key, hash, {}) }
19
- self.class.new(new_defaults)
20
- end
21
-
22
- def self.read(path)
23
- new YAML.safe_load(IO.read(path))
24
- rescue Psych::SyntaxError, Errno::ENOENT => e
25
- raise ConsulApplicationSettings::Error, "Cannot read defaults file at #{path}: #{e.message}"
26
- end
27
-
28
- private
29
-
30
- def read_path(path, hash, default = nil)
31
- parts = ConsulApplicationSettings::Utils.decompose_path(path)
32
- result = parts.reduce(hash, &method(:read_value))
33
- result || default
34
- end
35
-
36
- def read_value(hash, key)
37
- raise ConsulApplicationSettings::Error, 'reading arrays not implemented' if hash.is_a? Array
38
- return {} if hash.nil?
39
-
40
- hash.fetch(key.to_s)
41
- end
42
- end
43
- end
@@ -1,53 +0,0 @@
1
- require 'json'
2
-
3
- module ConsulApplicationSettings
4
- # Reads settings from consul or ask defaults for value
5
- class Options
6
- attr_reader :path, :defaults
7
-
8
- def initialize(path, defaults)
9
- @path = path
10
- @defaults = defaults.load_from(path)
11
- end
12
-
13
- def load_from(new_path)
14
- full_path = ConsulApplicationSettings::Utils.generate_path(path, new_path)
15
- self.class.new(full_path, defaults)
16
- end
17
-
18
- def get(name)
19
- consul_value = key_value(name)
20
- if consul_value.nil? || consul_value.empty?
21
- defaults.get(name)
22
- else
23
- ConsulApplicationSettings::Utils.cast_consul_value(consul_value)
24
- end
25
- end
26
-
27
- def [](name)
28
- get(name)
29
- end
30
-
31
- # rubocop:disable Style/MethodMissingSuper
32
- def method_missing(name, *_args)
33
- get(name)
34
- end
35
- # rubocop:enable Style/MethodMissingSuper
36
-
37
- def respond_to_missing?(_name)
38
- true
39
- end
40
-
41
- private
42
-
43
- def key_value(name)
44
- Diplomat::Kv.get(key_path(name), {}, :return)
45
- rescue SystemCallError, Faraday::ConnectionFailed, Diplomat::PathNotFound => e
46
- raise e unless ConsulApplicationSettings.config.disable_consul_connection_errors
47
- end
48
-
49
- def key_path(name)
50
- ConsulApplicationSettings::Utils.generate_path(path, name)
51
- end
52
- end
53
- end