Macmillan::Utils

A collection of useful patterns we use in our Ruby applications.

<img src=“https://travis-ci.org/springernature/macmillan-utils.svg?branch=log-formatter” alt=“Build Status” /> <img src=“https://codeclimate.com/github/springernature/macmillan-utils.png” alt=“CodeClimate Rating” /> <img src=“https://codeclimate.com/github/springernature/macmillan-utils/coverage.png” alt=“Test Coverage” /> <img src=“https://img.shields.io/badge/license-MIT-blue.svg” alt=“MIT Licensed” />

Installation

Add this line to your application’s Gemfile:

gem 'macmillan-utils', require: false

And then execute:

$ bundle

Or install it yourself with:

$ gem install macmillan-utils

Usage

Logger Objects

To build logger objects quickly and easily:

require 'macmillan/utils/logger'

logger = Macmillan::Utils::Logger::Factory.build_logger(:syslog, tag: 'myapp')

The logger will automatically use the Macmillan::Utils::Logger::Formatter formatter, and will log at the Logger::INFO level.

See the class documentation for more information:

Settings

Easily store and lookup configuration values in either environment variables, or a ‘application.yml` file.

ENV['REDIS_URL'] = 'localhost'
$settings        = Macmillan::Utils::Settings.instance
redis_url        = $settings.lookup('redis_url')          #=> 'localhost'

The default action is to support BOTH environment variables and a ‘application.yml` file for settings - if you would prefer only one or the other, see the ’Switching Backends’ section.

If a setting is requested and it has not been set (in any configured backend), a Macmillan::Utils::Settings::KeyNotFoundError is raised.

Evironment Variables

Set your environment variables as you would normally, using all caps for the key name, and you can then fetch the value using ‘.lookup`. Simples.

‘application.yml` Files

If you prefer to use a YAML based config file, this is the option for you. The code will first look for a ‘application.yml` file in a `config` directory (within the current working directory), if it doesn’t find one, then it looks in the current working directory, and keeps moving up the filesystem until it finds one. If it doesn’t it raises an error.

Switching Backends

By default both the environment variable and application yaml backends are used, if you’d like only one, you need to specify this before building the settings instance, i.e.:

Macmillan::Utils::Settings.backends = [Macmillan::Utils::Settings::EnvVarsBackend]

would only look for environment variables, whereas

Macmillan::Utils::Settings.backends = [Macmillan::Utils::Settings::AppYamlBackendBackend]

would only look for settings in an ‘application.yml` file.

RSpec Helpers

Add the following to the top of your ‘spec_helper.rb`:

require 'macmillan/utils/rspec/rspec_defaults'
require 'macmillan/utils/rspec/webmock_helper'
require 'macmillan/utils/test_helpers/codeclimate_helper'
require 'macmillan/utils/test_helpers/simplecov_helper'
require 'macmillan/utils/test_helpers/fixture_loading_helper'

Cucumber Helpers

Add the following to the top of your ‘env.rb`:

require 'macmillan/utils/cucumber/cucumber_defaults'
require 'macmillan/utils/cucumber/webmock_helper'
require 'macmillan/utils/test_helpers/codeclimate_helper'
require 'macmillan/utils/test_helpers/simplecov_helper'
require 'macmillan/utils/test_helpers/fixture_loading_helper'

StatsD

Contributing

  1. Fork it ( github.com/springernature/macmillan-utils/fork )

  2. Create your feature branch (‘git checkout -b my-new-feature`)

  3. Commit your changes (‘git commit -am ’Add some feature’‘)

  4. Push to the branch (‘git push origin my-new-feature`)

  5. Create a new Pull Request