consul_application_settings 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17d69e290b5ebc4231d99908108785b70dba8ed6deb822734e4e45afff42f919
4
+ data.tar.gz: 4075003ed050f4f4562b998848d2c6596ada40dee54da09c5ffad9aaefe11596
5
+ SHA512:
6
+ metadata.gz: bc59c2b40d1dbb47a0eb85e001cf2ef9753e6210fb0f0491dd0f246e3825630d13b118f8e635acfe82d9571fbf046f338918a73e40e4738a0d0a439239572179
7
+ data.tar.gz: ba7b6d50bd9e859b2e65c29597807f91906c1de84361195d6b7a9f38ac635934c057b25cc818a65d46f894a17ef5329bb2fbb1dd08e31300a65bd13f33465168
data/.codeclimate.yml ADDED
@@ -0,0 +1,12 @@
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
+ bundler-audit:
9
+ enabled: true
10
+ exclude_patterns:
11
+ - "spec/"
12
+ - "*.gemspec"
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - 'tmp/**/*'
5
+
6
+ Metrics/LineLength:
7
+ Max: 120
8
+
9
+ Style/FrozenStringLiteralComment:
10
+ Enabled: false
11
+
12
+ Metrics/BlockLength:
13
+ ExcludedMethods: ['describe', 'context']
data/.simplecov ADDED
@@ -0,0 +1 @@
1
+ SimpleCov.start if ENV["COVERAGE"]
data/.travis.yml ADDED
@@ -0,0 +1,48 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+
6
+ rvm:
7
+ - 2.5.1
8
+
9
+ env:
10
+ - CONSUL_VERSION=1.4.4
11
+
12
+ before_install:
13
+ # Install bundler 2.x
14
+ - gem update --system
15
+ - gem install bundler
16
+ # Install Code Climate
17
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
18
+ - chmod +x ./cc-test-reporter
19
+ - ./cc-test-reporter before-build
20
+ # Install consul
21
+ - curl -sLo consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip
22
+ - unzip "consul.zip"
23
+ - ./consul --version
24
+
25
+ install:
26
+ - bin/setup
27
+
28
+ before_script:
29
+ # Run consul
30
+ - ./consul agent -dev -node travis &
31
+
32
+ script:
33
+ - COVERAGE=true bundle exec rspec
34
+ - gem build consul_application_settings
35
+
36
+ after_script:
37
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
38
+
39
+ deploy:
40
+ provider: rubygems
41
+ api_key: "$RUBY_GEMS_API_KEY"
42
+ on:
43
+ tags: true
44
+
45
+ branches:
46
+ only:
47
+ - master
48
+ - /^\d+\.\d+\.\d+$/
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0]
4
+ - Gem init
5
+ - Reading settings from consul
6
+ - Reading settings from defaults
7
+ - Support deep settings search
8
+ - Support nested configs
9
+
10
+ [Unreleased]: https://github.com/matic-insurance/consul_application_settings/compare/0.1.0...HEAD
11
+ [0.1.0]: https://github.com/matic-insurance/consul_application_settings/compare/cb7194f...0.1.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at volodymyr.m@matic.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in consul_application_settings.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ consul_application_settings (0.1.0)
5
+ diplomat (~> 2.1.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ deep_merge (1.2.1)
12
+ diff-lcs (1.3)
13
+ diplomat (2.1.3)
14
+ deep_merge (~> 1.0, >= 1.0.1)
15
+ faraday (~> 0.9)
16
+ docile (1.3.1)
17
+ faraday (0.15.4)
18
+ multipart-post (>= 1.2, < 3)
19
+ jaro_winkler (1.5.2)
20
+ json (2.2.0)
21
+ multipart-post (2.0.0)
22
+ parallel (1.16.2)
23
+ parser (2.6.2.0)
24
+ ast (~> 2.4.0)
25
+ psych (3.1.0)
26
+ rainbow (3.0.0)
27
+ rake (10.5.0)
28
+ rspec (3.8.0)
29
+ rspec-core (~> 3.8.0)
30
+ rspec-expectations (~> 3.8.0)
31
+ rspec-mocks (~> 3.8.0)
32
+ rspec-core (3.8.0)
33
+ rspec-support (~> 3.8.0)
34
+ rspec-expectations (3.8.2)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-mocks (3.8.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-support (3.8.0)
41
+ rubocop (0.66.0)
42
+ jaro_winkler (~> 1.5.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.5, != 2.5.1.1)
45
+ psych (>= 3.1.0)
46
+ rainbow (>= 2.2.2, < 4.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 1.6)
49
+ rubocop-rspec (1.32.0)
50
+ rubocop (>= 0.60.0)
51
+ ruby-progressbar (1.10.0)
52
+ simplecov (0.16.1)
53
+ docile (~> 1.1)
54
+ json (>= 1.8, < 3)
55
+ simplecov-html (~> 0.10.0)
56
+ simplecov-html (0.10.2)
57
+ unicode-display_width (1.5.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler (~> 2.0)
64
+ consul_application_settings!
65
+ rake (~> 10.0)
66
+ rspec (~> 3.0)
67
+ rubocop (~> 0.66)
68
+ rubocop-rspec (~> 1.32.0)
69
+ simplecov (~> 0.16)
70
+
71
+ BUNDLED WITH
72
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Volodymyr Mykhailyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # ConsulApplicationSettings
2
+
3
+ [![Build Status](https://travis-ci.org/matic-insurance/consul_application_settings.svg?branch=master)](https://travis-ci.org/matic-insurance/consul_application_settings)
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)
6
+
7
+ Gem that simplifies usage of Consul (via [Diplomat gem](https://github.com/WeAreFarmGeek/diplomat))
8
+ to host application settings. Gem provides defaults via yaml files and other utilities
9
+ to simplify storage and control of application with Consul KV storage.
10
+
11
+ Gem is trying to solve a problem of distributing application settings for local development environment and provide defaults
12
+ in production before custom value is set inside of consul.
13
+
14
+ Example use cases:
15
+
16
+ - One engineer has created a new feature that depend on consul key/value.
17
+
18
+ How enginner can notify other engineers that they need to set this value in their consul environments?
19
+
20
+ - DevOps team responsible to configure and maintain deployment.
21
+
22
+ How do they learn (have reference) of what settings and structure application expect?
23
+
24
+ Gem reads any particular setting from consul and if it is missing tries to find value in YAML defaults file
25
+
26
+ **NOTE** Consul is requested every time you query the settings. Defaults YAML file loaded in memory and not changing.
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'consul_application_settings'
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ ### Initialization
39
+
40
+ At the load of application
41
+ ```ruby
42
+ 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'
47
+ end
48
+
49
+ APP_SETTINGS = ConsulApplicationSettings.load
50
+ ```
51
+
52
+ **NOTE** For rails you can add this code to custom initializer `console_application_settings.rb` in `app/config/initializers`
53
+
54
+ **NOTE** Diplomat gem should be configured before requesting any settings
55
+
56
+ ### Settings structure
57
+
58
+ Assuming your defaults file in repository `config/settings.yml` looks like:
59
+ ```yaml
60
+ app_name: 'MyCoolApp'
61
+ hostname: 'http://localhost:3001'
62
+
63
+ integrations:
64
+ database:
65
+ domain: localhost
66
+ user: app
67
+ password: password1234
68
+ slack:
69
+ enabled: false
70
+ webhook_url: 'https://hooks.slack.com/services/XXXXXX/XXXXX/XXXXXXX'
71
+ ```
72
+
73
+ And consul has following settings
74
+ ```json
75
+ {
76
+ "production": {
77
+ "my_cool_app": {
78
+ "hostname": "https://mycoolapp.com",
79
+ "integrations": {
80
+ "database": {
81
+ "domain": "194.78.92.19",
82
+ "password": "*************"
83
+ },
84
+ "slack": {
85
+ "enabled": "true"
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### Query settings via full path
94
+
95
+ Anywhere in your code base, after initialization, you can use
96
+ previously loaded settings to query any key by full path
97
+
98
+ ```ruby
99
+ APP_SETTINGS.app_name # "MyCoolApp"
100
+ APP_SETTINGS.get(:hostname) # "https://mycoolapp.com"
101
+
102
+ APP_SETTINGS.get('integrations/database/user') # "app"
103
+ APP_SETTINGS['integrations/slack/enabled'] # true
104
+ ```
105
+
106
+ **NOTE** Gem is pulling settings from consul with namespace but ignores namespace for defaults
107
+
108
+ ### Nested settings
109
+
110
+ Assuming some part of your code needs to work with smaller part of settings -
111
+ gem provides interface to avoid duplicating absolute path
112
+
113
+ ```ruby
114
+ # You can load subsettings from root object
115
+ db_settings = APP_SETTINGS.load_from('integrations/database')
116
+ db_settings.domain # "194.78.92.19"
117
+ db_settings['user'] # "app"
118
+
119
+ # You can load subsettings from subsettings
120
+ integrations_settings = APP_SETTINGS.load_from('integrations')
121
+ slack_settings = integrations_settings.load_from('slack')
122
+ slack_settings.enabled # true
123
+ slack_settings.get('webhook_url') # "https://hooks.slack.com/services/XXXXXX/XXXXX/XXXXXXX"
124
+ ```
125
+
126
+ ## Development
127
+
128
+ 1. [Install Consul](https://www.consul.io/docs/install/index.html)
129
+ 1. Run `bin/setup` to install dependencies
130
+ 1. Run tests `rspec`
131
+ 1. Add new test
132
+ 1. Add new code
133
+ 1. Go to step 3
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matic-insurance/consul_application_settings.
138
+ This project is intended to be a safe, welcoming space for collaboration,
139
+ and contributors are expected to adhere to the
140
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
141
+
142
+ ## License
143
+
144
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
145
+
146
+ ## Code of Conduct
147
+
148
+ Everyone interacting in the ConsulApplicationSettings project’s codebases, issue trackers,
149
+ chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/consul_application_settings/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'consul_application_settings'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'consul_application_settings/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'consul_application_settings'
7
+ spec.version = ConsulApplicationSettings::VERSION
8
+ spec.authors = ['Volodymyr Mykhailyk']
9
+ spec.email = ['volodymyr.mykhailyk@gmail.com']
10
+
11
+ spec.summary = 'Application settings via Consul with yaml defaults'
12
+ spec.description = 'Gem that simplifies usage of Consul (via Diplomat gem) to host application settings.
13
+ Gem provides defaults and utilities'
14
+ spec.homepage = 'https://github.com/matic-insurance/consul_application_settings'
15
+ spec.license = 'MIT'
16
+
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/matic-insurance/consul_application_settings'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/matic-insurance/consul_application_settings/blob/master/CHANGELOG.md'
21
+ end
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'diplomat', '~> 2.1.3'
33
+
34
+ spec.add_development_dependency 'bundler', '~> 2.0'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop', '~> 0.66'
38
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.32.0'
39
+ spec.add_development_dependency 'simplecov', '~> 0.16'
40
+ end
@@ -0,0 +1,13 @@
1
+ module ConsulApplicationSettings
2
+ # All gem configuration settings
3
+ class Configuration
4
+ # Required attributes
5
+ attr_accessor :defaults_path
6
+ # Optional attributes
7
+ attr_accessor :namespace
8
+
9
+ def initialize
10
+ self.namespace = ''
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
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
+ hash.fetch(key.to_s)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
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 = Diplomat::Kv.get(key_path(name), {}, :return)
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_path(name)
44
+ ConsulApplicationSettings::Utils.generate_path(path, name)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ module ConsulApplicationSettings
2
+ # Utility methods to cast values and work with path
3
+ module Utils
4
+ SEPARATOR = '/'.freeze
5
+ PARSING_CLASSES = [Integer, Float, ->(value) { JSON.parse(value) }].freeze
6
+
7
+ class << self
8
+ def cast_consul_value(value)
9
+ return false if value == 'false'
10
+ return true if value == 'true'
11
+
12
+ cast_complex_value(value)
13
+ end
14
+
15
+ def generate_path(*parts)
16
+ strings = parts.map(&:to_s)
17
+ all_parts = strings.map { |s| s.split(SEPARATOR) }.flatten
18
+ all_parts.reject(&:empty?).join('/')
19
+ end
20
+
21
+ def decompose_path(path)
22
+ parts = path.to_s.split(SEPARATOR).compact
23
+ parts.reject(&:empty?)
24
+ end
25
+
26
+ protected
27
+
28
+ def cast_complex_value(value)
29
+ PARSING_CLASSES.each do |parser|
30
+ return parser.call(value)
31
+ rescue StandardError => _
32
+ nil
33
+ end
34
+ value.to_s
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module ConsulApplicationSettings
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'consul_application_settings/version'
2
+ require 'consul_application_settings/configuration'
3
+ require 'consul_application_settings/defaults'
4
+ require 'consul_application_settings/options'
5
+ require 'consul_application_settings/utils'
6
+ require 'diplomat'
7
+
8
+ # Main class used to configure defaults file path and load initial settings
9
+ module ConsulApplicationSettings
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ attr_accessor :config
14
+ attr_accessor :defaults
15
+ end
16
+
17
+ self.config ||= ConsulApplicationSettings::Configuration.new
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
29
+
30
+ def load
31
+ load_from('')
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consul_application_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Volodymyr Mykhailyk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: diplomat
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.66'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.66'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.32.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.32.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.16'
111
+ description: |-
112
+ Gem that simplifies usage of Consul (via Diplomat gem) to host application settings.
113
+ Gem provides defaults and utilities
114
+ email:
115
+ - volodymyr.mykhailyk@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".codeclimate.yml"
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".rubocop.yml"
124
+ - ".simplecov"
125
+ - ".travis.yml"
126
+ - CHANGELOG.md
127
+ - CODE_OF_CONDUCT.md
128
+ - Gemfile
129
+ - Gemfile.lock
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - bin/console
134
+ - bin/setup
135
+ - consul_application_settings.gemspec
136
+ - lib/consul_application_settings.rb
137
+ - lib/consul_application_settings/configuration.rb
138
+ - lib/consul_application_settings/defaults.rb
139
+ - lib/consul_application_settings/options.rb
140
+ - lib/consul_application_settings/utils.rb
141
+ - lib/consul_application_settings/version.rb
142
+ homepage: https://github.com/matic-insurance/consul_application_settings
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ homepage_uri: https://github.com/matic-insurance/consul_application_settings
147
+ source_code_uri: https://github.com/matic-insurance/consul_application_settings
148
+ changelog_uri: https://github.com/matic-insurance/consul_application_settings/blob/master/CHANGELOG.md
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.0.3
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Application settings via Consul with yaml defaults
168
+ test_files: []