Project ‘Greenery’
Eight weeks into my software development class I was told to create an object oriented program that utilizes a command line interface otherwise known as a ‘CLI’. I decided to make my program relate to something I’ve always wished I’d known more about but always really enjoy doing and that is gardening. I was already pretty familiar with what I could grow in my region, but was curious as to what I could grow if I were in other places within the United States. I knew that if I could harvest some external data relating to states and their USDA Hardiness Zone as well as what plants could grow there I would have all the information I needed. The only problem was looking all this up and typing it all out would not only be painstaking but it would take forever!
Enter Scraping
I scoured the internet to try and find an API end that would give my program JSON data relating to zip codes and USDA hardiness zones…unfortunately either this doesn’t exist or it is just extremely hard to find. So the cleaner option was definitely out this left me with the process known as scraping. To scrape a website meant to send a get request the sites html into your program in order to extract the desired data from the site. This could be done by sending a net-http get request. Unfortunately html is generally just a huge text file, and the sites I pulled were certainly no exception. I needed a way to sift through all of this so that I could find the information I was looking for. This called for research into a Ruby gem that was suited to this need. I found one called nokogiri. With nokogiri I was able to transform the sites html within my program into a series of noko-objects that I could perform useful methods on. This included using CSS selectors or examining the children of a given html branch. With these tools I was able to extract the information I was looking for and use my program to create objects based on this information.
What is an Object?
An object is any piece of data in Ruby, these objects belong to a class. Such as ‘words’ are of the String class in ruby where as the number 1 is of the Integer class. In my case I needed to create State, City, and Zone classes. For instance a State object should know which City objects are in it, but not necessarily what Zone it is in because a State could sometimes have multiple Zones whereas a City could only have one. I felt that a City should know what Zone it is in and a Zone should know what plants can grow in it. I used this table from wikipedia and these pages from almanac.com (changing the number at the end of the url each time) to get the data I needed to create these objects. Any data that was missing I hard coded but this saved me a lot of work! Any plants could just be a string because receiving a list of these was at the end of my program and I didn’t need to operate on their information beyond listing them.
Closing thoughts
This project had its highs and its lows. I can honestly say I had a lot of fun building this though! I plan on adding some additional features such as the months you can plant each plant per region and possibly finding a way for the user to simply enter their zip code and have that relate to their zone. The project as it stands now can be found here. Follow the instructions on the README for setup and have fun gardening!