Pears

Welcome to the readme of Pears. This library is used to consume configuration in Ruby applications in a cascading way. It is named Pears because its a short, rememberable word and wasn't taken on rubygem.

Installation

Add this line to your application's Gemfile:

gem 'pears'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install pears

Usage

Examples:

# You can load a dynamic config file like this
dynamic_config = Pears.subject(:settings) do |provider|
  provider.subscription do
    provider.remote_file "https://raw.githubusercontent.com/codefresh-io/yaml-examples/master/codefresh-build-1.yml"
  end
end

# You can "Cascade" data like this
cascading_config = Pears.subject(:settings) do |provider|
  provider.local_file Rails.root.join('config', 'settings.concrete.yml') # defines foo: 'foo'
  provider.local_file Rails.root.join('config', 'settings.default.yml') # defines foo: 'bar', baz: 'baz'
end

cascading_config[:foo] # => foo
cascading_config[:baz] # => baz

# And you can combine these
combined_config = Pears.subject(:settings) do |provider|
  provider.subscription { provider.remote_file "https://admin/configuration.yml" }
  provider.local_file Rails.root.join('config', 'default.yml')
end

Terminology

  • A Subject is a grouping of configuration items with a single interface
  • A Subject contains at least a single "Layer" that wraps a "Provider"
  • A Provider is an adapter(YAML OR REDIS).
  • That wraps it up
Providers:
  • LocalFile - read a yaml file identified by a path into a "Subject"
  • RemoteFile - read a yaml file identified by a URI into a "Subject"
  • Subscription - Read a yaml file identified by a URI as a segment and update it whenever the the provided redis channel gets an update.

You can create a custom povider like this:

class GcloudProvider < Pears::Provider::LocalFile
  def initialize
    @data = parse_yaml Faraday.get(url, {}, authorization: token )
  end

  private

  def url
    'https://velogica-portal-admin.ey.r.appspot.com/settings/' \
    "#{Rails.application.secrets.settings_file}"
  end

  def token
    path = 'http://metadata.google.internal/computeMetadata/v1/instance/'

    'Bearer ' + Faraday.get(path, {}, { metadata_flavor: 'Google' }).body
  end
end

And register it like this:

# config/initializers/pears.rb
Pears::Provider::Builder.enable_provider Pears::GcloudProvider

Todo

Development

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.

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.

Future

  • Factor subject as a distinct, non-provider entitity.
  • Add Loco as a provider.
  • Add ENV as a provider.
  • Figure out if there is a sensible tie in with kubernetes.
  • Add support for different file-types.
  • Add different ways to trigger updates from redis subscriptions.

We want to add the following providers:

  • ENV - read the ENV and overwrite keys found on lower layers of the segment. **
  • LOCO - read a loco file into a segment.
  • Redis - consume data drom a redis key.

Contributing

Hit me up on slack, jira or Bitbucket!

License

The gem is available as open source under the terms of the MIT License.