We’ve started project mode this week and it’s been pretty cool building our own apps without any tests to pass, and solve real world problems with our application. I’ve learned a lot of new things and one of them new gems I learned to make my life easier is the Gon Gem!
Why Use Gon?
How I encountered the problem
Maybe we can just pull the ENV into a variable! WRONG
Maybe we need to interpolate it, this is from a ruby file, afterall. I changed the searches.js file into searches.js.erb so I can write ruby in it. In my file, I tried to set the api key to a variable username. WRONG
Then I tried to concatenate it into my API link to parse. WRONG
Apparently, that still gave me nothing. my var username was just an empty string, which resuilted in a bad API link.
With the Gon Gem
In your Gemfile, you will add the gem
Then you will bundle install to install the gem into your computer. If you haven’t already, add the api key to your application.yml from your figaro gem, like you usually would for regular ruby. Make sure it is in single quotes ‘’ and not double quotes “ ”, I made the mistake of using double quotes and it was not taken well when you ask for it in your model when you do ENV[‘api_key’].
1 2 3 4
Now this will work and you can continue parsing your data efficiently!
The Gon gem has been useful to me. Although this was a relatively easy problem to fix, there are probably other harder and deeper problems you can use it with that is best suited for this gem. For more information the typical uses and what you can use it with, you can go here! Gon gem