Setting Environment Variable with Figaro Gem

When developing with Rails, often we have to include confidential information in our code. For example if we need to input our API key or some login information. If you are working on open source software or you share your repository with other people, you might want to remove this confidential information from your codes.

One method to secure the information is to move them to environment variables, and read the information variable from the code. Something like this

Pusher.key    = ENV["pusher_key"]

However, setting the environment variable manually might become annoyance if you have to do it many times.

Figaro gem to the rescue! With Figaro, you can just place all your variables in a file named config/application.yml, for example like this

pusher_key: "7381a978f7dd7fas2437"

The previous code will read the environment variable from the application.yml file. Of course you have to remove the application.yml from your repository.

You also can set environment specific variables using figaro. For example:

pusher_key: "7381a978f7dd7fas2437"
  pusher_key: "ad612fh4a44dcac1fb28"

If you want to learn more about environment variable and rails, this article by Taylor Mock and Daniel Tahoe might be useful.