Extracting Configuration in Ruby Application

Wolox Engineering
Wolox
Published in
3 min readJan 27, 2015

Hi! Today I’m going to present you a little gem that I extracted after solving the same problem over and over again in Rails. But it can also be used in any Ruby application.

The gem is called app_configuration. I created this gem because in several Rails applications I had the problem of configuring third party services and I could not set sensitive information in the environment files. If you are doing this you shouldn’t. The major issue with setting sensitive information in the environment files is that (unless you don’t use any kind of VCS) you are versioning it.

For example, if I need to implement Facebook connect, what I would normally do is create a Facebook app with my personal account only for development. Then in my development.rb file I would do something like this:

And then if I have to access the Facebook credentials I would do something like this:

I use environmental variables instead of loading credentials from a file because most of the time I use Heroku for staging or production environments and the fastest way to configurate this kind of thing in Heroku is with environmental variables.

The hazard of this approach is that I have to remember to export the environmental variables each time I run the rails server. I could export all the variables in my .bashrc but I don’t like that because I could have name collisions. For example, if I have an application A that uses Facebook connect and I have application B that also uses Facebook connect I would have to do something like:

The other problem with this approach is that the configuration is not self-contained per project. So what I would end up doing is to create .env file in the root of the project and add this file to the .gitignore. The .env file would have all the corresponding exports. Now if you want to run the rails server you need to source the .env file like this:

source .env && bundle exec rails server

You could add an alias to reduce typing in your .bashrc file

alias rails=”source .env && bundle exec rails”

or you could use the forema gem that automatically reads all the variables defined in this file.

Sometimes we use Heroku for staging and Amazon EC2 for production. Generally when we use Amazon to deploy our application we use Chef to set up the VMs. In this case, I preferred to generate a config file using Chef templates and reading sensitive data from encrypted data bags.

So this is why I ended up creating app_configuration. Before creating the gem I did some research in RubyGems and found at least 15 configuration gems but neither of them does what I really want.

What I wanted is the flexibility to have a local self-contained configuration file and the possibility to export the configuration with environmental variables. All the app_configuration features are deeply explained in the project’s documentation but here I will do a quick review.

The previous Facebook connect example could be refactored using app_configuration like this. Instead of defining your configuration in each of the environment files you will define it in the application.rb:

AppConfiguration.for(:facebook)

Then if you want to read the Facebook API key and secret you can do it like this:

This assumes that there is a .facebook.yml file in the current working directory with the following content:

Environmental variables have more precedence than configuration files. So if FACEBOOK_API_KEY is exported, app_configuration will use that value. Note that the attributed name is prefixed with the name of the configuration (in this case FACEBOOK_). This is done to avoid name collisions. Also, remember to add you configuration files to the .gitignore.

So, that is pretty much it. Hope you like it and that you are willing to give it a try. Pull requests are welcome. You can fork app_configuration on Github.

Posted by Guido Marucci Blas(guidomb@wolox.com.ar)

www.wolox.com.ar

--

--