Gem Idol: Figaro
Today, I really want to start something different which I believe would be very helpful to new Rails developers. I remember the time when I first came to Rails world, I was impressed with its amazing community and gem ocean. I could find almost every thing I needed which has been written by other developer and delivered free on the Internet through something called "gem".
The main purpose of this series is to help new Rails developers know which good gem they can consider to integrate into their system to solve their issues. This is solely based on my own experience and it might be incorrect for some people, but trust me, these list works for me :).
And the first gem I want to introduce today is Figaro
Link: https://github.com/laserlemon/figaro
Developed by: Steve Richert
When to use?
Typically, you could have many environments to run your Rails app. Three popular ones are: development, test and production. Of course, there could be more such as staging, integration, etc. Each environment could use different configuration such as different host names, emails or AWS Keys. In addition, there are many sensitive information which you don't want to commit into Github such as email password, database password, etc. If I hire other developers to work in my project, I would not them to know my secret email address, you know. In those cases, it's best to store them as environment variables on your deployment server and access to these values through ENV
hash in your Rails app
However, it is inconvenient if you have to set environment variables each time you start Rails server in your development machine. Imagine how hard it is to start your Rails application like this
AWS_ACCESS_KEY_ID=XXXXX AWS_SECRET_KEY=XXXXX rails s
Figaro is here to help you. I recommend this gem to new Rails developer because I still do not forget the moment when I accidentally committed my email password for testing into shared Github repository and every other developers knew my email password. I then had to change my password instantly and I definitely do not want you to step on my foot :).
How to use
After installing this gem into your application, you can create a config file called application.yml
which contains all environment variables you want to exposure in your app. And you don't have to commit this file into Github, it is just for your local machine (add it to .gitignore). For example, the content of my application.yml
looks like this:
AWS_ACCESS_KEY_ID=XXXXX
AWS_SECRET_KEY=XXXXX
And now, I can use these two variables in my app through ENV
hash, like this:
ENV["AWS_ACCESS_KEY_ID"]
ENV["AWS_SECRET_KEY"]
Furthermore, I can also set different values for different environments, like this:
development:
AWS_ACCESS_KEY_ID=XXXXX
AWS_SECRET_KEY=XXXX
test:
AWS_ACCESS_KEY_ID=11111
AWS_SECRET_KEY=11111
production:
AWS_ACCESS_KEY_ID=222222
AWS_SECRET_KEY=222222
Now, depending on the current mode you are running, the value of ENV["AWS_ACCESS_KEY_ID"]
and ENV["AWS_SECRET_KEY"]
will be different accordingly.
But hold on, how to let other developers know which environment variables are available in app if you do not commit application.yml
? That's easy, you just need to create and commit a similar file called application.example.yml
which contains just only names of the variables, the value will be set by each individual developers. An example of application.example.yml
AWS_ACCESS_KEY_ID=
AWS_SECRET_KEY_ID=
Note for Capistrano users
If you are deploying your Rails app with Capistrano, this application.yml
file should be created manually when setting up the project on server, which is similar to database.yml
file. And when you add new variables, remember to go to server and change the content of application.yml
file accordingly.
Thank you for your attention. Looking forward to introducing next gem. Happy coding!