CLI Final Project

Brian Clark
3 min readJan 3, 2021

I’ve always had an idea for creating a social cigar app where users could record and rate cigars they have smoked. A user could choose to make these public to any followers or friends they have who could then see that user’s reviews and ratings. If someone has a similar taste in cigars as a friend/follower in the app, they could check their review and rating prior to trying a new cigar. This could also be used as a type of log for a user so they could find a cigar they smoked last week, last month, or last year. So I aligned my CLI project for this purpose. I decided to scrape a cigar website that has a whole database of cigar brands, cigars, and cigar attributes. I don’t know exactly how this will help me with my app idea yet but, at the very least, it gave me a little more passion and drive while coding.

I created the CLI portion first which will welcome a user and then allow them to browse a numbered list of cigar brands by individual pages. At this point, the user will then be asked if they want to view the cigars for a specific brand. If they chose to, they can select the number for that brand and will receive a list of all the cigars for that brand. Finally, they will be asked if they want to view the details for any of those listed cigars. Once a cigar is selected, they will be displayed with a list of attributes for that cigar. The user can exit the program at any time by typing it in when prompted. They can return at any time to look up different brands on different pages. This sums up the CLI portion of the project.

Next, I built a brand scraper utilizing Nokogiri which pulls in all the brands of cigars. This was relatively simple because the website listed each brand in sequential order in a HTML table. Originally, this was built to scrape all brands on every page but it took so long that a user might have assumed the program froze or crashed. I opted to only scrape the page at the time it was requested by the user opposed to every page at once. This drastically reduced the amount of time a user was waiting. As the brand scraper iterates over the brands, it will create an instance of that brand in a brand class and saves the name, link, and ID as instance variables. The ID is created internally and has no definition outside of the program. The brand class stores all instances of a brand into a class variable so class methods can be used to find brands by name or ID or all. The brand class has a ‘has many’ relation with the cigar class.

Similarly to the brand scraper, I built a cigar scraper that pulls in all the cigars for that brand. This was much more challenging to scrape though as the cigars table held all the attributes along with cells that I didn’t want as part of the program. Ultimately, I used a loop with two separate counters and each counter incrementing different amounts after each iteration of the loop. As each cigar is scraped, it is also initialized as an instance in a cigar class. The cigar class contains all the attributes that can be found for the cigar on the website such as length, gauge, country, wrapper, filler, color and strength. The cigar class will also store all instances of cigars in a class variable so they can be accessed with class methods just like with brands. The cigar class has a ‘belongs to’ relation with the brand class.

This was definitely a very fun project to work on despite a few headaches and frustrations. I hope that I can utilize this program or the knowledge I’ve acquired to create this program for my app at some point in the future. However, even if it can’t, I definitely got a needed confidence boost by seeing the progress I’ve made so far in such a short amount of time.

--

--