blinkist-config 1.0.2 → 1.1.0

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
  SHA1:
3
- metadata.gz: 8ea30ece0d13a1b1603254be36125c28709c77d2
4
- data.tar.gz: 9c9bb44d87a2310bfceb28e2e21c1d2eb387e58f
3
+ metadata.gz: f124d3763ba77723dd15b742f34059417f04e669
4
+ data.tar.gz: 9d83b0e2e91c7f4f8dee43f31d678e8ad29b7ae9
5
5
  SHA512:
6
- metadata.gz: 660c83fc9d879674dd268b90f63bf5398d6b0a470ecbf049844ce29c86d0bdc4b766d77c3e63728a3480025622f585a162d9984259e2f51b074ac70e2576b1a8
7
- data.tar.gz: e5d1b4cd3db5f6522a622dfabbcd8d02ad5a15a69668579e33ab97de07db93e87c2fde7ce83880e5b673afa50cfc5029b493aeea13e364721cdbeebeb1352fc8
6
+ metadata.gz: 357f0e51ee0a058243a548e0f2a5a150554b3d3d022146d96c4850e280f6065279b305f918263b9f7d04e5906dadc301956db83a798e623c4dac525ca6436aae
7
+ data.tar.gz: 8c3352a6add14f838b3a10da3bacdf08304775e25a79304a1dbb1154e8de8b1703e2f457cc8d18cdc5cc9b9b1d22e864c18f71de8b48838d42ab5f44804d40bf
data/README.md CHANGED
@@ -13,16 +13,33 @@ This GEM allows you to access configuration stores with different adapters. Here
13
13
  Blinkist::Config.env = ENV["RAILS_ENV"]
14
14
  Blinkist::Config.app_name = "my_nice_app"
15
15
  Blinkist::Config.adapter_type = :env
16
+ Blinkist::Config.error_handler = :strict
16
17
 
17
- my_config_value = Blinkist::Config.get "some/folder/config"
18
+ my_config_value = Blinkist::Config.get! "some/folder/config"
18
19
 
19
20
  # This is being translated to ENV["SOME_FOLDER_CONFIG"]
20
21
  ```
21
22
 
23
+ ### Error handling
24
+
25
+ When configured with `Blinkist::Config.error_handler = :strict` (as recommended)
26
+ reading a configuration entry for which the value is missing
27
+ (for example missing enviroment variables) will cause
28
+ `Blinkist::Config::ValueMissingError` to be raised.
29
+
30
+ There is also an alternative mode `Blinkist::Config.error_handler = :heuristic` which
31
+ will raise exceptions only when `Blinkist::Config.env == "production"`.
32
+
33
+ This alternative mode is also the default for compatibility.
34
+
22
35
  ### Having a default value
23
36
 
37
+ If you don't want `Blinkist::Config.get!` to scream at you for missing
38
+ configuration entries then you canprovide a default value as a second
39
+ paramter to `get!`:
40
+
24
41
  ```ruby
25
- my_config_value = Blinkist::Config.get "some/folder/config", "default value"
42
+ my_config_value = Blinkist::Config.get! "some/folder/config", "default value"
26
43
 
27
44
  # If ENV["SOME_FOLDER_CONFIG"] is nil, "default value" will be returned
28
45
  ```
@@ -42,7 +59,7 @@ Blinkist::Config.env = ENV["RAILS_ENV"]
42
59
  Blinkist::Config.app_name = "my_nice_app"
43
60
  Blinkist::Config.adapter_type = ENV["CONSUL_AVAILABLE"] == "true" ? :diplomat : :env
44
61
 
45
- my_config_value = Blinkist::Config.get "some/folder/config"
62
+ my_config_value = Blinkist::Config.get! "some/folder/config"
46
63
 
47
64
  # This is will try to get a value from Consul's KV store at "my_nice_app/some/folder/config"
48
65
  ```
@@ -51,7 +68,7 @@ my_config_value = Blinkist::Config.get "some/folder/config"
51
68
  ```ruby
52
69
  # Here we setting a scope outside of the app
53
70
 
54
- my_config_value = Blinkist::Config.get "another/config", scope: "global"
71
+ my_config_value = Blinkist::Config.get! "another/config", scope: "global"
55
72
 
56
73
  # This will replace `my_nice_app` with `global` and try to resolve "global/another/config"
57
74
  # With :env the scope will simply be ignored
data/Rakefile CHANGED
@@ -3,10 +3,10 @@ require "rspec/core/rake_task"
3
3
  require "rubocop/rake_task"
4
4
 
5
5
  RuboCop::RakeTask.new do |task|
6
- task.options = %w(-a)
6
+ task.options = %w[-a]
7
7
  end
8
8
 
9
9
  RSpec::Core::RakeTask.new(:spec)
10
10
 
11
11
  task(:default).clear
12
- task default: %i(rubocop spec)
12
+ task default: %i[rubocop spec]
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require_relative "lib/blinkist/config/version"
@@ -10,12 +10,12 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["sj@blinkist.com"]
11
11
 
12
12
  spec.summary = "Simple adapter based configuration handler (supports ENV and Consul/Diplomat)."
13
- spec.description = "This GEM allows you to keep your configuration class-based by calling Blinkist::Config.get(...) instead of accessing the ENV directly. You can set up different types of adapters to connect to various configuration systems like your ENV or Consul's key-value-store."
13
+ spec.description = "This GEM allows you to keep your configuration class-based by calling Blinkist::Config.get!(...) instead of accessing the ENV directly. You can set up different types of adapters to connect to various configuration systems like your ENV or Consul's key-value-store."
14
14
  spec.homepage = "https://github.com/blinkist/blinkist-config"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.require_paths = %w(lib)
18
+ spec.require_paths = %w[lib]
19
19
 
20
20
  spec.add_development_dependency "bundler", "~> 1.12"
21
21
  spec.add_development_dependency "rake", "~> 12.0"
@@ -12,15 +12,11 @@ module Blinkist
12
12
 
13
13
  class << self
14
14
  def instance_for(type, env, app_name)
15
- case type
16
- when :env
17
- EnvAdapter.new env, app_name
18
- when :diplomat
19
- DiplomatAdapter.new env, app_name
20
- else
21
- raise NotImplementedError
22
- end
15
+ Factory.new("Blinkist::Adapter.for", Adapters::BUILT_IN, env, app_name).call(type)
23
16
  end
17
+
18
+ extend Gem::Deprecate
19
+ deprecate :instance_for, :none, 2017, 12
24
20
  end
25
21
  end
26
22
  end
@@ -0,0 +1,10 @@
1
+ module Blinkist
2
+ class Config
3
+ module Adapters
4
+ BUILT_IN = {
5
+ env: EnvAdapter,
6
+ diplomat: DiplomatAdapter
7
+ }.freeze
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module Blinkist
2
+ class Config
3
+ class Error < ::StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ module Blinkist
2
+ class Config
3
+ class ErrorHandler
4
+ def initialize(env, app_name)
5
+ @env = env
6
+ @app_name = app_name
7
+ end
8
+
9
+ def call(key, scope)
10
+ raise ValueMissingError, "Missing value for #{key} in the scope: #{scope || '<default>'} (Please check the configuration for missing keys)"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module Blinkist
2
+ class Config
3
+ class ProductionOnlyErrorHandler < ErrorHandler
4
+ def call(key, scope)
5
+ super(key, scope) if @env.to_s == "production"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+
2
+ module Blinkist
3
+ class Config
4
+ module ErrorHandlers
5
+ BUILT_IN = {
6
+ strict: ErrorHandler,
7
+ heuristic: ProductionOnlyErrorHandler
8
+ }.freeze
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ module Blinkist
2
+ class Config
3
+ class Factory
4
+ def initialize(aspect, implementations, env=Blinkist::Config.env, app_name=Blinkist::Config.app_name)
5
+ @aspect = aspect
6
+ @implementations = implementations
7
+ @env = env
8
+ @app_name = app_name
9
+ end
10
+
11
+ def call(strategy)
12
+ case strategy
13
+ when Symbol
14
+ klass = @implementations[strategy] ||
15
+ raise(NotImplementedError, "Unknown strategy #{strategy} for #{@aspect}")
16
+ when Class
17
+ klass = strategy
18
+ else
19
+ if strategy.respond_to?(:call)
20
+ return strategy
21
+ else
22
+ raise InvalidStrategyError
23
+ end
24
+ end
25
+
26
+ klass.new(@env, @app_name)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,6 @@
1
+ module Blinkist
2
+ class Config
3
+ class InvalidStrategyError < Error
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Blinkist
2
+ class Config
3
+ class NotImplementedError < Error
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Blinkist
2
+ class Config
3
+ class ValueMissingError < Error
4
+ end
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  module Blinkist
2
2
  class Config
3
- VERSION = "1.0.2".freeze
3
+ VERSION = "1.1.0".freeze
4
4
  end
5
5
  end
@@ -1,19 +1,63 @@
1
1
  require_relative "config/version"
2
+ require_relative "config/error"
3
+ require_relative "config/factory"
4
+ require_relative "config/not_implemented_error"
5
+ require_relative "config/invalid_strategy_error"
6
+ require_relative "config/value_missing_error"
7
+ require_relative "config/error_handlers/error_handler"
8
+ require_relative "config/error_handlers/production_only_error_handler"
9
+ require_relative "config/error_handlers"
2
10
  require_relative "config/adapters/env_adapter"
3
11
  require_relative "config/adapters/diplomat_adapter"
12
+ require_relative "config/adapters"
4
13
 
5
14
  module Blinkist
6
15
  class Config
7
16
  class << self
8
- attr_accessor :adapter_type, :logger, :env, :app_name
17
+ attr_accessor :adapter_type, :logger, :env, :app_name, :error_handler
9
18
 
10
19
  def get(key, default = nil, scope: nil)
11
- adapter.get(key, scope: scope) || default
20
+ get!(key, default, scope: scope)
21
+ end
22
+
23
+ extend Gem::Deprecate
24
+ deprecate :get, "get!", 2017, 12
25
+
26
+ def get!(key, *args, scope: nil)
27
+ # NOTE: we need to do this this way
28
+ # to handle 'nil' default correctly
29
+ case args.length
30
+ when 0
31
+ default = nil
32
+ bang = true
33
+ when 1
34
+ default = args.first
35
+ bang = false
36
+ else
37
+ raise ArgumentError, "wrong number of arguments (given #{args.length + 1}, expected 1..2)"
38
+ end
39
+
40
+ from_adapter = adapter.get(key, scope: scope)
41
+
42
+ if from_adapter.nil? && bang
43
+ handle_error(key, scope)
44
+ else
45
+ return from_adapter || default
46
+ end
12
47
  end
13
48
 
14
49
  def adapter
15
- @adapter ||= Adapter.instance_for adapter_type, env, app_name
50
+ @adapter ||= Factory.new("Blinkist::Config.adapter_type", Adapters::BUILT_IN).call(adapter_type)
16
51
  end
52
+
53
+ def handle_error(key, scope)
54
+ handler = Factory.new("Blinkist::Config.error_handler", ErrorHandlers::BUILT_IN).call(error_handler)
55
+ handler.call(key, scope)
56
+ end
57
+
17
58
  end
59
+
60
+ # NOTE: default configuration goes here
61
+ self.error_handler = :heuristic
18
62
  end
19
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blinkist-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Schleicher, Blinks Labs GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-22 00:00:00.000000000 Z
11
+ date: 2017-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1'
69
69
  description: This GEM allows you to keep your configuration class-based by calling
70
- Blinkist::Config.get(...) instead of accessing the ENV directly. You can set up
70
+ Blinkist::Config.get!(...) instead of accessing the ENV directly. You can set up
71
71
  different types of adapters to connect to various configuration systems like your
72
72
  ENV or Consul's key-value-store.
73
73
  email:
@@ -89,9 +89,18 @@ files:
89
89
  - blinkist-config.gemspec
90
90
  - docker-compose.yml
91
91
  - lib/blinkist/config.rb
92
+ - lib/blinkist/config/adapters.rb
92
93
  - lib/blinkist/config/adapters/adapter.rb
93
94
  - lib/blinkist/config/adapters/diplomat_adapter.rb
94
95
  - lib/blinkist/config/adapters/env_adapter.rb
96
+ - lib/blinkist/config/error.rb
97
+ - lib/blinkist/config/error_handlers.rb
98
+ - lib/blinkist/config/error_handlers/error_handler.rb
99
+ - lib/blinkist/config/error_handlers/production_only_error_handler.rb
100
+ - lib/blinkist/config/factory.rb
101
+ - lib/blinkist/config/invalid_strategy_error.rb
102
+ - lib/blinkist/config/not_implemented_error.rb
103
+ - lib/blinkist/config/value_missing_error.rb
95
104
  - lib/blinkist/config/version.rb
96
105
  homepage: https://github.com/blinkist/blinkist-config
97
106
  licenses:
@@ -113,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
122
  version: '0'
114
123
  requirements: []
115
124
  rubyforge_project:
116
- rubygems_version: 2.6.8
125
+ rubygems_version: 2.6.10
117
126
  signing_key:
118
127
  specification_version: 4
119
128
  summary: Simple adapter based configuration handler (supports ENV and Consul/Diplomat).