The Simplest and Powerful Ruby Gem — Dotenv

Hima Chitalia
Coffee and Codes
Published in
3 min readMay 30, 2017

You may have an awesome idea for building your Rails application. Something that still doesn’t exist or something that is very helpful. But often while building it, you will encounter some of the tasks where you may need to try Ruby Gems that you haven’t used before.

That is what happened with me. While making a simple Ruby application, I had to give it credentials of my app authorization on Facebook, my app’s FACEBOOK_KEY and FACEBOOK_SECRET.

Frankly speaking, I always used to write my Environment variables in terminal just to assign it to the shell session. Something like this:

export FACEBOOK_KEY=your_app_id
export FACEBOOK_SECRET=your_app_secret

It works perfectly fine. But there is a drawback, every time I reopen my terminal, I have to export my app’s credentials so that my shell session can use it. Uggrrrr..!

Being a Rubyist, you get right to be lazy. There is always something you will find to avoid repeating the same task. And that thrust of finding a perfect Ruby Gem landed me on Dotenv. A simple definition of Dotenv “Dotenv is a Ruby Gem that loads variables from a .env file into ENV when the environment is bootstrapped.”

As you can see on it’s Github repo, Readme is very easy to follow but I will show you just 4 simple steps to make it more simpler.

Add Gem in your Gemfile:

Just add gem ‘dotenv’ to your Gemfile and run bundle install in your terminal

Make .env file:

If you don’t have .env file in the root of your project directory, make one with the command touch .env

Write your credentials:

Now write your credentials in it. For example, if you need to write your Facebook App credentials to allow your user sign in through Facebook, you can write:

export FACEBOOK_KEY=8681538697897567v3
export FACEBOOK_SECRET=837429hih878yr2uhir98000009808

Here, the export command will let you reference ENV hash values anywhere in your app. For example, in my app when I needed to make an API call with these values I used <ENV[“FACEBOOk_KEY”]> and <ENV[“FACEBOOK_SECRET”]>to access the stored values from DotEnv.

.gitignori file:

When building your Ruby App, you will a few files that you don’t want to push to Github. Adding those files to .gitignore file will help you do it. Our .env file is also an excellent example of such a files because we don’t want to push our app’s credentials to Github.

If you already don’t have the .gitignore file, make one with touch .gitignore command in the root of your project directory. Now just add this to your .gitignorefile.

#Ignore .env file
/.env

And you are all set with Dotenv. One of the crucial things while developing an app is to make it secured, and it starts with making sure that your app’s credentials with another web apps are secured. Dotenv helps us to do that.

I hope, this tutorial helps you out with your app’s security. Any questions or comments, feel free to write me either at hima.chhag@gmail.com or in the comment section below.

Happy Coding!

--

--