Building My First Ruby Gem

Eric Contreras Cabrera
5 min readMar 7, 2021

Getting Started

Coming up with a CLI project idea was not easy. I usually have many ideas but no structure on how to actually make them become reality. I had so many interests, I did not know where to start. After a long process of elimination, I finally had three final ideas that could shape up to be a great first CLI project:

  1. Solar System and Planets Information CLI
  2. Sneaker Information CLI
  3. Destiny 2 (Video game) Lore CLI

It was advised in class to use web scraping or an easy-to-use API as the main source of information. Web scraping is the process of manually extracting data from an original source, usually a huge HTML file with lots of CSS. I will be honest and admit that scraping websites for the precise information did not seem like a fun experience, so I opted for using an API instead. After scouting the web for the proper resources, I found that the Destiny 2 API needed extra steps of verification, which I did not learn yet and would unnecessarily complicate my project. (Although, if we had more time for this project, I certainly would have been up for the challenge.) Next, I found a great Solar System API that contained a lot of useful information. However, I felt overwhelmed with the amount of information contained in this API. I felt I might need something a bit simpler to tackle in my first big project as a software engineering student. Finally, I found a great API that contained a lot of useful sneaker information. This was close to home since, as of lately, I really enjoy learning about sneakers and I want to grow my small sneakers collection. This is when it all started coming together. I am personally interested in knowing when and which sneakers will be released in the upcoming months. It is usually really hard to get any sneakers at retail price online because the websites can never handle the traffic and sell out to bots instantly, but I digress. I decided I would write a CLI gem that would give me specific information about certain sneakers without me having to go through the horrible experience of the ad-ridden click-bait traps that most sneaker sites have become. So that’s exactly what I did.

Setting up the Project

After spending nearly two months learning about the Ruby language at Flatiron, it was time to put it all together and start building my first CLI project. I had a solid idea to build upon: Upcoming Sneaker Releases. I knew some external gems would be needed, such as open-uri, net/http and json to assist with formatting the API data. Additionally, I knew I would be needing at least three different classes:

  1. API Data Class : Handle the GET requests and parse and assign data.
  2. CLI Class: Handle the menus, user interface, and user input.
  3. Sneaker Class: Handle sneaker objects and their attributes and print them.

After reading some helpful information about CLI project building, I learned that it was best to start simple and then add complexity later. I did not have a working CLI or Sneaker Class yet, so I decided to focus on understanding how the API data was handled by Ruby and incorporate that into my API Class.

Building the API Class

I started by checking which API queries would work the best for me based on the attributes I was interested in such as name, brand, price and release date. Once I found the proper way to communicate with the API, I was able to use some existing ruby gems to make my API class work. I used the open-uri gem to parse the API link into the net/http gem method that would use a GET request to obtain the API data. It was done in the following manner:

brands_URL = “https://api.thesneakerdatabase.com/v1/brands"uri = URI.parse(brands_URL)response = Net::HTTP.get_response(uri)

Next, I added the ability to retrieve data from the sneakers API and initialize Sneaker objects and its attributes with the data. In order to do this, I had set up a Sneakers Class class variable that would be populated by all the data obtained from the sneakers API.

Building the Sneakers Class

While I was building the Sneakers Class, I considered what functions the class would need to handle. After a lot of trial and error checking for the best way to handle the API data, I ended up with 7 different methods plus the initialize method. At initialization, all the attributes are set and objects saved to the class variable. The main methods include print_brands, printSnkrs, print_Snkr_info, find_by_brand. The rest are class variable accessors which include self.all and self.brandsArray, plus a save method used in the initialize. Below is the a snippet of a print method which takes in some user input, then prints the appropriate data:

def self.print_Snkr_info(input)puts “Name: #{@@allSnkrs[input].name.capitalize}”…end

The goal of this method is to print the relevant sneaker information to the end user on the command line accessed through the CLI Class.

Building the CLI Class

The CLI class is the one that brings the classes together and actually executes the methods from the other classes. The main idea for the CLI was to get user input and display some information based on what was selected. It was also time to start thinking about the bound limits for the user input, as not everyone plays nice. I only wanted to have some specified commands available; that way, the amount of effort for the user would be minimal and reduce potential errors due to illegal user input. The CLI class would end up having two different menus, where the second menu is based on the user’s selection in the first. Each menu would be encapsulated by its own loop, which the user would have control over. Note that the CLI Class only calls the Sneakers Class, as that’s where the print methods are located, and the Sneakers Class would obtain the data by calling the API class. This way, all classes had specific jobs and the method chaining is easier to keep track of.

Wrapping Up

I can still remember how accomplished I felt when everything was working as intended. A few months ago, I could not even imagine myself writing any code, much less making an entire gem. After getting through the initial mental block of not knowing where to begin to finally commit my gem to GitHub, this experience is definitely a milestone at the start of my software engineering career. I learned so many aspects of my future as a software engineer from project brainstorming to implementation, testing, and debugging. But, what makes this experience truly magnificent is the fact that once I got everything working, my mind was already thinking of all the new features I could add in the future. For now, my gem only lists a specific (15 to be exact) amount of sneakers obtained from the API because having so much information printed on the terminal at once can get overwhelming. The addition would be a “next page” type of interface where user input can toggle the next 15 items. Additionally, I could add some filters, like gender or color, and only display sneakers matching this criteria. This project really showed me the versatility of software engineering. It makes me hopeful that one day I could actually make a difference and contribute to the ever growing list of amazing software around the world.

--

--