loading...

The Making of a Gem

szam profile image Samuel Grasse-Haroldsen Updated on ・4 min read

At Flatiron School our first milestone project is designing and developing a cli data gem. We are given the option to utilize an API or scrap what we need from the web. I have always loved produce, and plants are absolutely fascinating to me so I looked for a good website with plenty of information on fruits and veggies.

To Scrap or not to Scrap

I found an awesome website with tons of information! fruitsandveggies.org seemed too good to be true... and it was. Their terms are very explicit in not allowing any sort of scraping without written permission. So I continued the search and found a simple yet elegant API - fruityvice. It didn't have any veggies or that many fruit, but the data was accurate.

Using an API

Ruby has many wonderful gems (a fun Ruby specific term for libraries) to access and parse API data. There is actually a gem to do just about anything, and if there isn't an API you can always make one like I did! To get started I made a class to get info from the API and parse it. The gems that helped me with this were:

  • httparty: to make a get request from the API
  • json: to parse a JSON document and convert it into a ruby data structure
  • colorize: to make the command line results pretty
  • pry: debugging

I didn't use colorize or pry in the API class but I just wanted to list all the gems I used.

Storing the Data

Next I needed to make a ruby class to capture an instance of a fruit along with all the information that the API provided. I made sure to include an @@all class array that stored all the instances of fruit. Due to the sheer size of the hash provided by the API, I decided to try out mass assignment in my initialize method. A simple each block that sent the value of the hash element to the key writer method worked perfectly. The nutritional information of a fruit was nested in another hash so I would have to access that using a key.

The Command Line

After all was said and done I needed to make the interactive part of the application. The user needed to be able to make choices. A while loop and a case statement did the trick. Adding a couple of methods to present the information nicely (and colorfully) and I was all done. The user had options. They could:

  • print all the fruit names
  • print all fruit names listed with their scientific name
  • print all fruit names with their nutritional info
  • print which fruits had the most nutrition (fat, protein, carbs, etc.)
  • print which fruits had the least nutrition
  • type the name of a fruit and see all of its information
  • and of course quit

Fruit, fruit, and more fruit.

Publishing a Gem

The hardest part was publishing my gem on RubyGems. I didn't have much experience with the bundle gem but I tried creating a new gem using bundle, copying over my source code, and filling in the gemspec file. It was a hassle and I learned some very important lessons. Bundle gem will automatically make a module with the name of the gem containing your version number. I had a class with the same name as my gem and it really confused the Ruby interpreter. Also the gemspec file is generated with some nice code in it for grabbing all the necessary files but mine was also grabbing the generated gem file and requiring itself to build itself which was very confusing. So after hours of searching for the solutions, I changed the module name and hard-coded in my source code files in the gemspec file. Boom. Now I could finally publish my gem!

Lesson Learned

I learned a lot from working on this project. Patience and breaks are absolutely essential when debugging problems. Stack Overflow has the solution even though it might not look like your problem or the solution. Get the application working and then refactor to clean it all up - it's an excellent way to spot issues and brush up on what your code is doing. And the most important lesson I learned was that making something new and exciting from start to finish is an awesome experience. As we learn a new skill like coding I think it is easy to simply write some code to get a test to pass, but we don't learn as much that way. It is essential to learn that way at first, but once you have enough knowledge you must build something by applying that knowledge. You gain wisdom in taking on a huge project and using every ounce of effort you have. So code on and change the world one puts at a time.

If you want to check out my gem

fruit-cli-gem on github

gem install fruit-info

Posted on by:

Discussion

pic
Editor guide