blinkist-config 1.0.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
+ SHA1:
3
+ metadata.gz: fcbcd596ce5bde72a5ffe9ee8b07f772f4034dbc
4
+ data.tar.gz: 91711dab272c12e0ec40b4dbc2ccdef1d2ea7995
5
+ SHA512:
6
+ metadata.gz: de38e2672296c3d350d225eceae51f4773fa5deaf4b12a9437f2a98ebbda3bd62626b731be296b604977afcef530ac069cc692c5c9c9b84ba8ed5f1f80ce2a04
7
+ data.tar.gz: db98b96b4fdd279187c2130ecd12dd86c5d405038a71090cef298e173c6753110a076be7f077f1d846e5222f7a7f4669a420366b8409f8b89837211c9465a9f6
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,77 @@
1
+ AllCops:
2
+ Exclude:
3
+ - '**/schema.rb'
4
+ - 'bin/**'
5
+ - 'vendor/**/*'
6
+
7
+ Lint/ParenthesesAsGroupedExpression:
8
+ Enabled: true
9
+ Exclude:
10
+ - 'spec/**/*'
11
+
12
+ Metrics/ModuleLength:
13
+ Exclude:
14
+ - "**/*_spec.rb"
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - "**/*_spec.rb"
19
+
20
+ Style/BlockDelimiters:
21
+ Enabled: true
22
+ Exclude:
23
+ - 'spec/**/*'
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Style/EmptyLinesAroundClassBody:
29
+ Enabled: false
30
+
31
+ Style/EmptyLinesAroundModuleBody:
32
+ Enabled: false
33
+
34
+ Style/GuardClause:
35
+ Enabled: false
36
+
37
+ Style/PredicateName:
38
+ Enabled: false
39
+
40
+ Style/SignalException:
41
+ Enabled: false
42
+
43
+ Style/SpaceAroundEqualsInParameterDefault:
44
+ Enabled: false
45
+
46
+ Style/StringLiterals:
47
+ Enabled: false
48
+
49
+ Style/ZeroLengthPredicate:
50
+ Enabled: false
51
+
52
+ # Offense count: 9
53
+ Metrics/AbcSize:
54
+ Enabled: false
55
+
56
+ # Offense count: 1
57
+ # Configuration parameters: CountComments.
58
+ Metrics/ClassLength:
59
+ Enabled: false
60
+
61
+ # Offense count: 2
62
+ Metrics/CyclomaticComplexity:
63
+ Enabled: false
64
+
65
+ # Offense count: 79
66
+ # Configuration parameters: AllowURI, URISchemes.
67
+ Metrics/LineLength:
68
+ Enabled: false
69
+
70
+ # Offense count: 10
71
+ # Configuration parameters: CountComments.
72
+ Metrics/MethodLength:
73
+ Enabled: false
74
+
75
+ # Offense count: 2
76
+ Metrics/PerceivedComplexity:
77
+ Enabled: false
data/Dockerfile ADDED
@@ -0,0 +1,32 @@
1
+ FROM ruby:2.3.1-slim
2
+
3
+ ENV RAILS_ENV=production
4
+ ENV LANG en_US.UTF-8
5
+ ENV LANGUAGE en_US.UTF-8
6
+ ENV LC_ALL en_US.UTF-8
7
+ ENV LC_CTYPE en_US.UTF-8
8
+
9
+ ENV RUNTIME_PACKAGES pkg-config netcat supervisor nodejs git jq uuid-runtime libxslt1-dev build-essential libxml2-dev libxslt1-dev
10
+
11
+ RUN mkdir /tmp/tmpapp
12
+ WORKDIR /tmp/tmpapp
13
+ ADD . /tmp/tmpapp
14
+
15
+ RUN echo 'gem: --no-document' >> ~/.gemrc && \
16
+ apt-get clean && apt-get update -qq && apt-get install -y build-essential $RUNTIME_PACKAGES && \
17
+ gem install bundler --version 1.12.5 && \
18
+ gem install gem-release && \
19
+ bundle update && bundle install --jobs 20 --retry 5 && \
20
+ rm -Rf /tmp/tmpapp/ && \
21
+ rm -rf /usr/share/doc/ /usr/share/man/ /usr/share/locale/ && apt-get clean
22
+
23
+ RUN mkdir /app
24
+ ADD . /app
25
+ WORKDIR /app
26
+
27
+ RUN bundle install -j1
28
+
29
+ RUN chown -R nobody:nogroup /app
30
+ RUN whoami
31
+ USER nobody
32
+ RUN whoami
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blinkist-config.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Blinkist
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sebastian Schleicher
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,103 @@
1
+ # Blinkist::Config
2
+
3
+ This GEM allows you to access configuration stores with different adapters. Here're some examples of usage:
4
+
5
+ ### Using the ENV
6
+
7
+ ```ruby
8
+ # First setup the Config to use the ENV as config store
9
+ Blinkist::Config.env = ENV["RAILS_ENV"]
10
+ Blinkist::Config.app_name = "my_nice_app"
11
+ Blinkist::Config.adapter_type = :env
12
+
13
+ my_config_value = Blinkist::Config.get "some/folder/config"
14
+
15
+ # This is being translated to ENV["SOME_FOLDER_CONFIG"]
16
+ ```
17
+
18
+ ### Having a default value
19
+
20
+ ```ruby
21
+ my_config_value = Blinkist::Config.get "some/folder/config", "default value"
22
+
23
+ # If ENV["SOME_FOLDER_CONFIG"] is nil, "default value" will be returned
24
+ ```
25
+
26
+ ### Using Diplomat & Consul
27
+
28
+ If you want to use Consul's key value store, simply use our diplomat adapter.
29
+
30
+ * [https://www.consul.io/](https://www.consul.io/)
31
+ * [https://github.com/WeAreFarmGeek/diplomat](https://github.com/WeAreFarmGeek/diplomat)
32
+
33
+ The GEM expects consul to listen to `http://172.17.0.1:8500`
34
+
35
+ ```ruby
36
+ # First setup the Config to use the ENV as config store
37
+ Blinkist::Config.env = ENV["RAILS_ENV"]
38
+ Blinkist::Config.app_name = "my_nice_app"
39
+ Blinkist::Config.adapter_type = ENV["CONSUL_AVAILABLE"] == "true" ? :diplomat : :env
40
+
41
+ my_config_value = Blinkist::Config.get "some/folder/config"
42
+
43
+ # This is will try to get a value from Consul's KV store at "my_nice_app/some/folder/config"
44
+ ```
45
+
46
+ ### Using Diplomat with a folder scope
47
+ ```ruby
48
+ # Here we setting a scope outside of the app
49
+
50
+ my_config_value = Blinkist::Config.get "another/config", scope: "global"
51
+
52
+ # This will replace `my_nice_app` with `global` and try to resolve "global/another/config"
53
+ # With :env the scope will simply be ignored
54
+ ```
55
+
56
+
57
+ ## Installation
58
+
59
+ Add this line to your application's Gemfile:
60
+
61
+ ```ruby
62
+ gem "blinkist-config"
63
+ ```
64
+
65
+ ## Usage
66
+
67
+ You have to set up the GEM before you can use it. The basic setup requires this
68
+
69
+ ```ruby
70
+ Blinkist::Config.env = "production" || "test" || "development"
71
+ Blinkist::Config.app_name = "your_app_name" # Used only with diplomat adapter
72
+ Blinkist::Config.adapter_type = :diplomat || :env
73
+ ```
74
+
75
+ It's best to drop a `config.rb` into your app and load this file before every other file. In Rails you can link it into your `application.rb`
76
+
77
+ ```ruby
78
+ require_relative "boot"
79
+ require_relative "config"
80
+
81
+ require "rails"
82
+ # ...
83
+ ```
84
+
85
+ ## Development
86
+
87
+ You can build this project easily with [docker compose](https://docs.docker.com/compose/).
88
+
89
+ ```
90
+ docker-compose run rake
91
+ ```
92
+
93
+ This will execute rake and run all specs by auto correcting the code with rubocop.
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/blinkist-config.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ RuboCop::RakeTask.new do |task|
6
+ task.options = ["-a"]
7
+ task.patterns = ["lib/**/*.rb", "spec/**/*.rb"]
8
+ end
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task(:default).clear
13
+ task default: [:rubocop, :spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blinkist/config"
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
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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require_relative "lib/blinkist/config/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blinkist-config"
8
+ spec.version = Blinkist::Config::VERSION
9
+ spec.authors = ["Sebastian Schleicher, Blinks Labs GmbH"]
10
+ spec.email = ["sj@blinkist.com"]
11
+
12
+ spec.summary = %q{Simple adapter based configuration handler (supports ENV and Consul/Diplomat).}
13
+ spec.description = %q{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
+ spec.homepage = "https://github.com/blinkist/blinkist-config"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_development_dependency "rubocop", '~> 0'
24
+
25
+ spec.add_runtime_dependency "diplomat", "~> 1"
26
+ end
@@ -0,0 +1,21 @@
1
+ version: "2"
2
+ services:
3
+ rake:
4
+ entrypoint: bundle exec rake
5
+ build:
6
+ context: .
7
+ environment:
8
+ - RAILS_ENV=test
9
+ - RACK_ENV=test
10
+ volumes:
11
+ - .:/app
12
+
13
+ gem:
14
+ build:
15
+ context: .
16
+ entrypoint: gem
17
+ volumes:
18
+ - .:/app
19
+ - ~/.gitconfig:/nonexistent/.gitconfig
20
+ - ~/.ssh:/nonexistent/.ssh
21
+ - ~/.gem:/nonexistent/.gem
@@ -0,0 +1,27 @@
1
+ module Blinkist
2
+ class Config
3
+ class Adapter
4
+ def initialize(env, app_name)
5
+ @env = env
6
+ @app_name = app_name
7
+ end
8
+
9
+ def get(_key, _default=nil, **)
10
+ raise NotImplementedError
11
+ end
12
+
13
+ class << self
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
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "adapter"
2
+ require "diplomat"
3
+
4
+ module Blinkist
5
+ class Config
6
+ class DiplomatAdapter < Adapter
7
+ def initialize(env, app_name)
8
+ super env, app_name
9
+
10
+ @items_cache = {}
11
+
12
+ Diplomat.configure do |config|
13
+ config.url = "http://172.17.0.1:8500"
14
+ end
15
+ end
16
+
17
+ def get(key, default=nil, scope: nil)
18
+ scope ||= @app_name
19
+
20
+ diplomat_key = "#{scope}/#{key}"
21
+
22
+ @items_cache.fetch(diplomat_key, Diplomat::Kv.get(diplomat_key))
23
+ rescue Diplomat::KeyNotFound
24
+ default
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "adapter"
2
+
3
+ module Blinkist
4
+ class Config
5
+ class EnvAdapter < Adapter
6
+ def get(key, default = nil, **)
7
+ env_key = key.tr("/", "_").upcase
8
+ ENV[env_key] || default
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Blinkist
2
+ class Config
3
+ VERSION = "1.0.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require_relative "config/version"
2
+ require_relative "config/adapters/env_adapter"
3
+ require_relative "config/adapters/diplomat_adapter"
4
+
5
+ module Blinkist
6
+ class Config
7
+ class << self
8
+ attr_accessor :adapter_type, :logger, :env, :app_name
9
+
10
+ def get(key, default = nil, scope: nil)
11
+ adapter ||= Adapter.instance_for adapter_type, env, app_name
12
+ adapter.get(key, scope: scope) || default
13
+ end
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blinkist-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Schleicher, Blinks Labs GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: diplomat
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ description: This GEM allows you to keep your configuration class-based by calling
84
+ Blinkist::Config.get(...) instead of accessing the ENV directly. You can set up
85
+ different types of adapters to connect to various configuration systems like your
86
+ ENV or Consul's key-value-store.
87
+ email:
88
+ - sj@blinkist.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".gitignore"
94
+ - ".rubocop.yml"
95
+ - Dockerfile
96
+ - Gemfile
97
+ - LICENSE
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - blinkist-config.gemspec
104
+ - docker-compose.yml
105
+ - lib/blinkist/config.rb
106
+ - lib/blinkist/config/adapters/adapter.rb
107
+ - lib/blinkist/config/adapters/diplomat_adapter.rb
108
+ - lib/blinkist/config/adapters/env_adapter.rb
109
+ - lib/blinkist/config/version.rb
110
+ homepage: https://github.com/blinkist/blinkist-config
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.8
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Simple adapter based configuration handler (supports ENV and Consul/Diplomat).
134
+ test_files: []