pears 0.0.1

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: 6027c30390f8c8c7eb777118ac67a2c92ab1492c9e56cbf8bfd7f0b3af7dd9b4
4
+ data.tar.gz: 55a01b652c8f57945d864b9b35b2a0ae974cbf434f26012541eb5a62e8fe4f36
5
+ SHA512:
6
+ metadata.gz: 805d0cdae121ba3fc165de1e12177bc63eab1396af53d661652062253b85b40dec8487c7cb49e673784f3cfc4420adc7ef4cd7737f9a10b381655c4ca6e9cacd
7
+ data.tar.gz: c18a180c02015f9a8816f177fdfca43119552e2e5b584cace0877aceab9108a28a21ceeb1099501060599fdacbb22629ae016d5046c766e3e5b7d34886286176
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-12-08
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pears.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pears (0.1.0)
5
+ activesupport
6
+ dry-configurable (>= 0.13.0)
7
+ redis
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.1.4.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ zeitwerk (~> 2.3)
18
+ byebug (11.1.3)
19
+ concurrent-ruby (1.1.9)
20
+ diff-lcs (1.4.4)
21
+ dry-configurable (0.13.0)
22
+ concurrent-ruby (~> 1.0)
23
+ dry-core (~> 0.6)
24
+ dry-core (0.7.1)
25
+ concurrent-ruby (~> 1.0)
26
+ i18n (1.8.11)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.14.4)
29
+ rake (13.0.6)
30
+ redis (4.5.1)
31
+ rspec (3.10.0)
32
+ rspec-core (~> 3.10.0)
33
+ rspec-expectations (~> 3.10.0)
34
+ rspec-mocks (~> 3.10.0)
35
+ rspec-core (3.10.1)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-expectations (3.10.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-mocks (3.10.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-support (3.10.3)
44
+ tzinfo (2.0.4)
45
+ concurrent-ruby (~> 1.0)
46
+ zeitwerk (2.5.1)
47
+
48
+ PLATFORMS
49
+ arm64-darwin-20
50
+
51
+ DEPENDENCIES
52
+ byebug
53
+ pears!
54
+ rake (~> 13.0)
55
+ rspec (~> 3.0)
56
+
57
+ BUNDLED WITH
58
+ 2.2.31
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Steven Kemp
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,67 @@
1
+ # Pears
2
+
3
+ Welcome to the readme of Pears. This library is used to consume configuration
4
+ in Ruby applications in a cascading way. It is named Pears because its a short,
5
+ rememberable word and wasn't taken on rubygem.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pears'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pears
22
+
23
+ ## Usage
24
+
25
+ Configuration is loaded via the provider setting.
26
+
27
+
28
+ ## Terminology
29
+
30
+ - A Subject is a grouping of configuration items with a single interface
31
+ - A Subject contains at least a single "Layer" that wraps a "Provider"
32
+ - A Provider is an adapter(YAML OR REDIS).
33
+ - That wraps it up
34
+
35
+ ##### Providers:
36
+
37
+ - LocalFile - read a yaml file identified by a path into a "Subject"
38
+ - RemoteFile - read a yaml file identified by a URI into a "Subject"
39
+ - Subscription - Read a yaml file identified by a URI as a segment and update it whenever the the provided redis channel gets an update.
40
+ - ENV - read the ENV and overwrite keys found on lower layers of the segment. **
41
+ - LOCO - read a loco file into a segment. **
42
+
43
+ ** Nice to have
44
+
45
+
46
+ ## Todo
47
+ - Figure out if subscription should be a providerof maybe something else.
48
+ - Add Loco as a provider.
49
+ - Add ENV as a provider.
50
+ - Add Redis as a provider.
51
+ - Figure out if there is a sensible tie in with kubernetes.
52
+ - Add support for different file-types.
53
+ - Add different ways to trigger updates from redis subscriptions. (like hand fired events?)
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pears.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "pears"
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 @@
1
+ class Error < StandardError; end
@@ -0,0 +1,18 @@
1
+ module Pears
2
+ module Provider
3
+ class Base
4
+ attr_accessor :data
5
+ def has_key?(key)
6
+ @data.has_key? key
7
+ end
8
+
9
+ def each(&block)
10
+ @data.block(&:yield)
11
+ end
12
+
13
+ def [](key)
14
+ @data[key]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ require 'active_support/core_ext/module'
2
+
3
+ module Pears
4
+ module Providers
5
+ class Builder
6
+ delegate :name, to: :subject
7
+
8
+ def initialize(subject)
9
+ @subject = subject
10
+ end
11
+
12
+ def hash
13
+ raise 'not yet implemented'
14
+ end
15
+
16
+ def loco
17
+ raise 'not yet implemented'
18
+ end
19
+
20
+ def env
21
+ raise 'not yet implemented'
22
+ end
23
+
24
+ def local_file(file_path)
25
+ register Provider::LocalFile.new(file_path)
26
+ end
27
+
28
+ def remote_file(file_url)
29
+ register Provider::RemoteFile.new(file_url)
30
+ end
31
+
32
+ def subscription &block
33
+ register Provider::Subscription.new(@subject.name, &block)
34
+ end
35
+
36
+ private
37
+
38
+ def register provider
39
+ return provider unless @subject.is_a? Subject
40
+
41
+ @subject.push_layer provider
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ module Pears
2
+ module Provider
3
+ class ENV < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module Pears
2
+ module Provider
3
+ class Hash < Base
4
+ def initialize(hash)
5
+ @data = hash
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+
4
+ module Pears
5
+ module Provider
6
+ # Used for loading simple YAML file locally.
7
+ class LocalFile < Base
8
+ def initialize(file_path)
9
+ yaml_data = File.read(file_path)
10
+ @data = parse_yaml(yaml_data)
11
+ end
12
+
13
+ private
14
+
15
+ def parse_yaml(yaml_data)
16
+ YAML.load(yaml_data).with_indifferent_access
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module Pears
2
+ module Provider
3
+ # Maps to a Locolize export
4
+ class Loco < Base
5
+ def initialize(api_key, url)
6
+ @data = fetch_from_loco(api_key, url)
7
+ end
8
+
9
+ private
10
+
11
+ def fetch_from_loco(api_key, url)
12
+ # Get config from loco
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require 'net/http'
2
+
3
+ module Pears
4
+ module Provider
5
+ # Fetch a yaml file via HTTP
6
+ class RemoteFile < LocalFile
7
+ def initialize(remote_url)
8
+ yaml_data = Net::HTTP.get(URI(remote_url))
9
+ @data = parse_yaml(yaml_data)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ require 'redis'
3
+
4
+ module Pears
5
+ module Provider
6
+ # Config using redis subscription. This updates automatically upon Redis
7
+ # channel updates.
8
+ class Subscription < Base
9
+ attr_reader :subscription
10
+
11
+ def initialize(channel,
12
+ redis_host: Pears.config.redis_host,
13
+ &fetcher)
14
+ @channel = channel
15
+ @fetcher = fetcher
16
+
17
+ establish_connection(redis_host)
18
+ subscribe
19
+ reload
20
+ end
21
+
22
+ def reload
23
+ @data = @fetcher.call(Pears::Providers::Builder.new(self))
24
+ .data
25
+ end
26
+
27
+ # This seems a bit "rough" of an approach. we should check if
28
+ # unsubscribing can be done more gracefully.
29
+ def unsubscribe
30
+ @subscription.terminate
31
+ end
32
+
33
+ private
34
+
35
+ def subscribe
36
+ @subscription = Thread.new do
37
+ connection.subscribe(@channel) do |on|
38
+ on.message do |channel, message|
39
+ reload
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def connection
46
+ @connection ||= establish_connection
47
+ end
48
+
49
+ def establish_connection(host='redis')
50
+ @connection = ::Redis.new(host: host,
51
+ reconnect_attempts: 10,
52
+ reconnect_delay: 5)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "provider/base"
2
+ require_relative "provider/local_file"
3
+ require_relative "provider/remote_file"
4
+ require_relative "provider/subscription"
5
+ require_relative "provider/env"
6
+ require_relative "provider/loco"
7
+ require_relative "provider/hash"
8
+ require_relative "provider/builder"
9
+
10
+ module Pears
11
+ module Provider
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ module Pears
2
+ # A subject is a collection of settings.
3
+ # You could different Pears::Subjects in your application for different parts
4
+ # of its domain. for instance: application_settings, theming or translations.
5
+ class Subject
6
+ attr_reader :name
7
+
8
+ def initialize(name, *layers)
9
+ @name = name
10
+ @layers = layers
11
+ Pears.subjects << self
12
+ end
13
+
14
+ def [](key)
15
+ project[key]
16
+ end
17
+
18
+ def keys
19
+ project.keys
20
+ end
21
+
22
+ def has_key?(key)
23
+ project.has_key? key
24
+ end
25
+
26
+ def push_layer(layer)
27
+ @layers << layer
28
+ end
29
+
30
+ # Projects the layers to a hash that van be used as a single source of truth.
31
+ def project
32
+ layers.reverse.reduce(Hash.new) do |hash, layer|
33
+ hash.deep_merge(layer.data)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def layers
40
+ @layers
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pears
4
+ VERSION = "0.0.1"
5
+ end
data/lib/pears.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ require 'dry-configurable'
3
+ require_relative "pears/version"
4
+ require_relative "pears/errors"
5
+ require_relative "pears/provider"
6
+ require_relative "pears/subject"
7
+
8
+ module Pears
9
+ extend Dry::Configurable
10
+
11
+ setting :redis_host, default: :localhost
12
+
13
+ def self.subjects
14
+ @subjects ||= []
15
+ end
16
+
17
+
18
+ # This is the main way of locating a subject.
19
+ def self.subject(name)
20
+ Subject.new(name: name).tap do |subject|
21
+ builder = Providers::Builder.new(subject)
22
+ yield(builder)
23
+ end
24
+ end
25
+ end
data/pears.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pears/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pears"
7
+ spec.version = Pears::VERSION
8
+ spec.authors = ["Steven Kemp"]
9
+ spec.email = ["steven@remarkgroup.Com"]
10
+
11
+ spec.summary = "Ingest hot-swappable configuration data"
12
+ spec.homepage = "https://www.rubygems.com"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.1"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ # Uncomment to register a new dependency of your gem
28
+ spec.add_dependency 'dry-configurable', '~> 0.13.0'
29
+ spec.add_dependency 'activesupport', '~> 6.1.4.1'
30
+ spec.add_dependency 'redis', '~>4.5.1'
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pears
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Kemp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 6.1.4.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 6.1.4.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.5.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.5.1
55
+ description:
56
+ email:
57
+ - steven@remarkgroup.Com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/pears.rb
73
+ - lib/pears/errors.rb
74
+ - lib/pears/provider.rb
75
+ - lib/pears/provider/base.rb
76
+ - lib/pears/provider/builder.rb
77
+ - lib/pears/provider/env.rb
78
+ - lib/pears/provider/hash.rb
79
+ - lib/pears/provider/local_file.rb
80
+ - lib/pears/provider/loco.rb
81
+ - lib/pears/provider/remote_file.rb
82
+ - lib/pears/provider/subscription.rb
83
+ - lib/pears/subject.rb
84
+ - lib/pears/version.rb
85
+ - pears.gemspec
86
+ homepage: https://www.rubygems.com
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://www.rubygems.com
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 3.0.1
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.2.22
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Ingest hot-swappable configuration data
110
+ test_files: []