shared-settings-ui 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: 514468c693f649ca7fc63e9f40b45e1da6da1a34768160e7abc1fd2eccfc79e7
4
+ data.tar.gz: e784afc4cf23680497861a254314a0adc5c04aea196102a641cc837d71ce40ed
5
+ SHA512:
6
+ metadata.gz: '068aa5940b764bb5b147a1a30ce7b3f917ecfa507bbd7519547cd13c2936a6950e16659958813051a8a68580a191feac7b9cef0fdd005d877d25d076b46b40a5'
7
+ data.tar.gz: 623a899f857eb39602e4d7999410f818e10fcde718c95c4e848c9829eba859ca51ff8e593af43820456b7dc9d08c445df07dd321171b4ac42f04747661813ed9
@@ -0,0 +1,39 @@
1
+ name: Ruby CI
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ test:
7
+ name: Build and test
8
+ runs-on: ubuntu-latest
9
+
10
+ services:
11
+ redis:
12
+ image: redis
13
+ options: >-
14
+ --health-cmd "redis-cli ping"
15
+ --health-interval 10s
16
+ --health-timeout 5s
17
+ --health-retries 5
18
+ ports:
19
+ - 6379:6379
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+
24
+ - name: Set up Ruby
25
+ uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: 2.6
28
+
29
+ - name: Install dependencies
30
+ run: bundle install
31
+
32
+ - name: Check linting
33
+ run: bundle exec rubocop
34
+
35
+ - name: Run tests
36
+ run: bundle exec rake test
37
+ env:
38
+ # Not actually secret but keeps this file neat
39
+ SHARED_SETTINGS_KEY: ${{ secrets.SHARED_SETTINGS_KEY }}
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ AllCops:
2
+ NewCops: enable
3
+
4
+ Metrics/MethodLength:
5
+ Enabled: false
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Metrics/AbcSize:
14
+ Enabled: false
15
+
16
+ Layout/FirstHashElementIndentation:
17
+ EnforcedStyle: consistent
18
+
19
+ Layout/ArgumentAlignment:
20
+ EnforcedStyle: with_fixed_indentation
21
+
22
+ Naming/FileName:
23
+ Enabled: false
24
+
25
+ Lint/MixedRegexpCaptureTypes:
26
+ Enabled: false
27
+
28
+ Style/DoubleNegation:
29
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec name: 'shared-settings'
4
+ gemspec name: 'shared-settings-ui'
5
+
6
+ gem 'minitest', '~> 5.0'
7
+ gem 'rake', '~> 12.0'
8
+ gem 'redis', '~> 4.0'
9
+ gem 'rubocop', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shared-settings (0.1.0)
5
+ shared-settings-ui (0.1.0)
6
+ rack (>= 2.0, < 3)
7
+ shared-settings (~> 0.1.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.1)
13
+ minitest (5.14.2)
14
+ parallel (1.19.2)
15
+ parser (2.7.1.4)
16
+ ast (~> 2.4.1)
17
+ rack (2.2.3)
18
+ rainbow (3.0.0)
19
+ rake (12.3.3)
20
+ redis (4.2.2)
21
+ regexp_parser (1.7.1)
22
+ rexml (3.2.4)
23
+ rubocop (0.90.0)
24
+ parallel (~> 1.10)
25
+ parser (>= 2.7.1.1)
26
+ rainbow (>= 2.2.2, < 4.0)
27
+ regexp_parser (>= 1.7)
28
+ rexml
29
+ rubocop-ast (>= 0.3.0, < 1.0)
30
+ ruby-progressbar (~> 1.7)
31
+ unicode-display_width (>= 1.4.0, < 2.0)
32
+ rubocop-ast (0.4.0)
33
+ parser (>= 2.7.1.4)
34
+ ruby-progressbar (1.10.1)
35
+ unicode-display_width (1.7.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ minitest (~> 5.0)
42
+ rake (~> 12.0)
43
+ redis (~> 4.0)
44
+ rubocop
45
+ shared-settings!
46
+ shared-settings-ui!
47
+
48
+ BUNDLED WITH
49
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Kieran Eglin
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,154 @@
1
+ # Shared Settings (Ruby)
2
+
3
+ Shared Settings is a simple library for managing runtime settings in Ruby with optional support for encryption and Elixir.
4
+
5
+ Heavily inspired by [Fun with Flags][fwf] and [Flipper][flipper].
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'shared-settings'
13
+ # Optional
14
+ gem 'shared-settings-ui'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install shared-settings
24
+ $ gem install shared-settings-ui
25
+
26
+ This Gem depends on Redis for the default adapter so ensure that the Redis gem is also installed and configured.
27
+
28
+ Once installed, the Gem must be configured. This would normally exist as a file within `config/initializers` if you're using Rails.
29
+
30
+ ```ruby
31
+ @client = Redis.new
32
+ @adapter = SharedSettings::Persistence::Redis.new(@client)
33
+
34
+ SharedSettings.configure do |config|
35
+ config.default { SharedSettings.new(@adapter) }
36
+ # Optional. Can be generaed with `SharedSettings::Utilities::Encryption.generate_aes_key`
37
+ # Store this somewhere secure out of VCS
38
+ config.encryption_key = '...'
39
+ end
40
+ ```
41
+
42
+ The optional UI works with any Rack-based webserver. Assuming you're using Rails, set up your `routes.rb` like so:
43
+
44
+ ```ruby
45
+ mount SharedSettings::UI.app, at: '/shared-settings'
46
+ ```
47
+
48
+ This doesn't provide any form of protection - anyone could visit this URL. To add something like basic auth, you can pass a block like so:
49
+
50
+ ```ruby
51
+ shared_settings_app = SharedSettings::UI.app do |builder|
52
+ builder.use Rack::Auth::Basic do |username, password|
53
+ # Perform validation
54
+ end
55
+ end
56
+
57
+ mount shared_settings_app, at: '/shared-settings'
58
+ ```
59
+
60
+ Elixir support is provided by the [shared-settings-ex][ss-ex] library.
61
+
62
+ ## Why "shared" settings?
63
+
64
+ The intention for this library is to also create an [accompanying Elixir Library][ss-ex] which uses the same storage adapter, format, and UI found here.
65
+
66
+ This means that a Rails app could change a runtime setting in an Elixir app and vice-versa. They would also share a single UI dashboard if configured, allowing a one-stop location to manage parallel apps or to help migration efforts. Of course, this library could be used with Elixir or Ruby individually.
67
+
68
+ The API/storage conventions are designed to be simple enough that additional libraries in other languages (eg: Go) could be created to allow further interop between applications as long as there was a shared data source.
69
+
70
+ ## Encryption
71
+
72
+ Encryption is implemented as AES256. If you choose to provide an encryption key, specified setting values within your storage adapter will be encrypted. Nothing else about the setting, including its name, will be encrypted. Once an encrypted setting is requested via `get` it's automatically decrypted so the plaintext value is returned.
73
+
74
+ ```ruby
75
+ SharedSettings.put(:client_id, 'supersecret', encrypt: true)
76
+ SharedSettings.get(:client_id) # => 'supersecret'
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ The API is quite simple. For most cases, you have `put`, `get`, `delete`, and `exists?`.
82
+
83
+ There is also `all` which returns all raw settings, but this is primarily to support UI.
84
+
85
+ ### Supported Types
86
+
87
+ At a high level, the currently supported types are `string`, `boolean`, `number`, and `range`. `number` includes negative numbers as well as floats. `range`s are inclusive.
88
+
89
+ All types are serialized as strings to be held within the storage adapter.
90
+
91
+ ### Put
92
+
93
+ `put` takes a name as well as a value with a supported type. It returns the name of the setting.
94
+
95
+ ```ruby
96
+ SharedSettings.put(:signups_enabled, true)
97
+ SharedSettings.put(:referral_bonus, 52, encrypt: true)
98
+ ```
99
+
100
+ `put` will overwrite old settings if the provided name already exists. This means there's no method for updating - replacement is the way to go:
101
+
102
+ ```ruby
103
+ SharedSettings.put(:confusing_setting, true)
104
+ SharedSettings.put(:confusing_setting, 2..7)
105
+ ```
106
+
107
+ ### Get
108
+
109
+ `get` takes the name of a setting and returns the value. A `SettingNotFoundError` is returned if the setting doesn't exist.
110
+
111
+ ```ruby
112
+ ranks = SharedSettings.get(:permitted_ranks)
113
+ ```
114
+
115
+ ### Delete
116
+
117
+ `delete` takes the name of a setting and removes it from storage. It's safe to call delete on settings that may not exist.
118
+
119
+ ```ruby
120
+ SharedSettings.delete(:contrived_example)
121
+ SharedSettings.delete(:not_real)
122
+ ```
123
+
124
+ ### Exists?
125
+
126
+ `exists?` takes the name of a setting and returns a boolean reflecting its existence.
127
+
128
+ ```ruby
129
+ SharedSettings.exists?(:signups_enabled)
130
+ ```
131
+
132
+ ### All
133
+
134
+ `all` returns all stored settings in their raw form. This is mainly used by the accompanying UI library but it could also be used to ensure all needed flags exist at boot time.
135
+
136
+ ```ruby
137
+ SharedSettings.all
138
+ ```
139
+
140
+ ## License
141
+
142
+ MIT License
143
+
144
+ Copyright 2021
145
+
146
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
147
+
148
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
149
+
150
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
151
+
152
+ [fwf]: https://github.com/tompave/fun_with_flags
153
+ [flipper]: https://github.com/jnunemaker/flipper
154
+ [ss-ex]: https://github.com/kieraneglin/shared-settings-ex
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_helper'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks name: 'shared-settings'
5
+ Bundler::GemHelper.install_tasks name: 'shared-settings-ui'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.libs << 'lib'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ end
12
+
13
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'shared-settings'
5
+ require 'shared-settings-ui'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,3 @@
1
+ # This file uses kebab-case to match the name of the Gem for autoloading purposes
2
+
3
+ require 'shared_settings/ui'
@@ -0,0 +1,57 @@
1
+ # This file uses kebab-case to match the name of the Gem for autoloading purposes
2
+
3
+ require 'forwardable'
4
+
5
+ require 'shared_settings/version'
6
+ require 'shared_settings/setting'
7
+ require 'shared_settings/instance'
8
+ require 'shared_settings/configuration'
9
+ require 'shared_settings/serialized_setting'
10
+
11
+ require 'shared_settings/persistence/redis'
12
+
13
+ require 'shared_settings/utilities/base16'
14
+ require 'shared_settings/utilities/encryption'
15
+
16
+ module SharedSettings
17
+ class SettingNotFoundError < StandardError; end
18
+
19
+ extend self
20
+ extend Forwardable
21
+
22
+ def new(storage_adapter)
23
+ SharedSettings::Instance.new(storage_adapter)
24
+ end
25
+
26
+ def configure
27
+ yield configuration if block_given?
28
+ end
29
+
30
+ def configuration
31
+ @configuration ||= SharedSettings::Configuration.new
32
+ end
33
+
34
+ def instance
35
+ configuration.default
36
+ end
37
+
38
+ def exists?(name)
39
+ get(name)
40
+
41
+ true
42
+ rescue SettingNotFoundError
43
+ false
44
+ end
45
+
46
+ def_delegators :instance,
47
+ :put,
48
+ :get,
49
+ :all,
50
+ :delete
51
+
52
+ private
53
+
54
+ def configuration=(configuration)
55
+ @configuration = configuration
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module SharedSettings
2
+ class Configuration
3
+ attr_accessor :encryption_key
4
+
5
+ def initialize
6
+ @default = -> { raise ArgumentError, 'Default configuration must be set' }
7
+ end
8
+
9
+ def default(&block)
10
+ if block_given?
11
+ @default = block
12
+ else
13
+ @default.call
14
+ end
15
+ end
16
+ end
17
+ end