FindFurryFriend Project — dotenv Ruby Gem

Photo by Kalineri on Unsplash

In last week’s blog I was debating if I should use the dotenv gem to secure my api key and I was also having some trouble taking the data from the petfinder API and saving it to my database. I was mostly having trouble trying to get into the data structure. This week I’ve had some success with both of those issues yay!

Any who, I decided to use the dotenv gem in order to secure my api key. I thought it would make it easier to access throughout my application and I thought it may be more secure. So Heres how I got started:

Add this line to application’s Gemfile:

gem 'dotenv-rails'

And then execute:

bundle install
  1. Create a .env file in your root directory

2. Go to gitignore file and add .env (add any file you don’t want to be public when pushed to github account):

.env
KEY=YOURKEYGOESHERE
SECRET=YOURSECRETGOESHERE

ps. something I was wondering was why is the variable set with capital letters? This is because it is a constant and the value is never going to change.

I’m still using the petfinder gem in my seed file! So, in order to instantiate a client the petfinder gem says to do this:

petfinder = Petfinder::Client.new('your_api_key', 'your_api_secret') 

This would expose my api key and secret, but now that I am using dotenv gem I can do this in my seed file instead:

petfinder = Petfinder::Client.new(ENV["KEY"], ENV["SECRET"])

Alright, next I wanted to find animal shelters in MA and save it to my database. Looking through the petfinder gem I decided to access the endpoint by using a location

organizations, pagination = petfinder.organizations({ location: '02143', limit: 5 })

And then I iterated through that data, found the key of what I was looking for, and saved it to my database:

And yay I got some json back!

Next week i’m going to work on the pets json!

  1. petfinder API
  2. petfinder Gem
  3. dotenv gem

Happy coding ✌️

Lifelong Learner