You’re an Object, Harry!
I’m closing out my 4th week at Flatiron School’s Software Engineering Bootcamp Program, and much like Neo in the final act of The Matrix, I’m beginning to see the world around me as strings of Code…
The concept of Object Orientation (OO) has expanded my understanding of the idea that everything around us can be defined as a unique object that exists within our universe. On the surface, some objects may look the same as others, but each object has its own unique fingerprint. Objects can own other objects, belong to other objects, and they can even create other objects. As I continue working on my Command Line Interface (CLI) Project, I see now that our responsibility as OO programmers is to create our own universe filled with objects that can interact with each other. We need to provide data and functionality, which then gives life and purpose to our program.
Populating the Universe
The idea for my CLI Project was to create a sort of Social Network for Hogwarts School of Witchcraft & Wizardry. After sifting through a bunch of API’s, I settled on the Harry Potter API from www.potterapi.com, which provides routes for Characters, Houses, and Spells. As soon as the program starts, the API Class pulls data directly from this source and instantiates objects based on each route. The following method assists in pulling from the correct route and properly parses the collected data:
def get_data( route )
url = @url_base + route + "?key=" + @api_key
uri = URI( url )
response = Net::HTTP.get( uri )
arr = JSON.parse( response )
The way I see it, #get_data allows us to populate our Hogwarts universe with 4 different Houses, each containing a number of Wizards who have access to an assortment of Spells. As the User of the program, you must navigate the CLI to interact with other Wizards, make friends, make enemies, and also learn/create your own spells.
Uh Oh, Something Broke
About 80% of the way through developing my application, the server for the API suddenly became unavailable. Since my API class was not able to fetch any data, my program could not instantiate any objects, therefore leaving the user with a blank screen, followed momentarily with a JSON parse error. I sent an email to the creator of the API and she responded quickly, informing me that she would look into the issue. However, as I waited several hours for something that was pretty much out of my control, a thought occurred: What can I control in this situation? Why not scrape some websites!
Scraping to the Rescue
Finding the right website to scrape took a bit more effort than I had anticipated. When we scraped for information in our labs, we were provided with website examples that more or less listed which HTML elements and classes contained the information we needed. But now that I was scouring the web on my own, I recognized the patience and precision required to get the right information. Some websites don’t even specify class attributes, and you’re left relying on what you know about the HTML DOM, iterating through nested tables, rows, columns and divs. It took several attempts to come up with the correct queries via binding.pry, but eventually I was able to successfully scrape 2 different websites to gather my House and Spell data. Fortunately, there was another Harry Potter API that I could use to populate my Character objects, but sadly it did not provide nearly as much information as the original source. Regardless, I was grateful to have found new sources for all my objects while keeping the functionality of my CLI in tact.
Perhaps the greatest thing I can take away from this project is the idea of Object Relationships. As the creator of my own little universe, I feel an obligation to each instantiated object to ensure that it can fulfill its purpose within the program. Does it have the variables and methods it needs in order to interact with the other objects? Is the object aware of its relationship to other objects? Does it know how many Spells exist in its own collection? Does a Wizard know if they are friends or enemies with another Wizard? My program currently creates 3 types of objects to encompass the requirements of this project, but I can imagine adding new models like Wands, Brooms, Animals, Ghosts, Monsters, and so much more to truly expand the range of possibilities for this universe. I guess I’ll need to scrape more websites to accomplish that!
As I started adding more and more functionality to my CLI class, I noticed it was looking more and more cluttered, making it rather difficult to find methods that I wanted to modify. I found that separating code via modules not only allowed me to categorize my methods, but also made it simpler to find what I was looking for. I thought to myself:
If I were to allow another programmer to look through my code, I need to make sure that it is organized enough that they wouldn’t need to ask me how something works or where they can find a particular method or variable.
include Commands::InstanceMethods attr_accessor :name, :house, :info, :commands, :history def initialize
@history = 
I’m sure there are better ways to refactor and organize your code, maybe using Namespaces, Inheritance, or something similar, but for now I’ll stick with what’s familiar. I look forward to learning new programming techniques & conventions, and overall becoming a more confident and well-rounded programmer as the weeks progress. With that said, and with hopes of a successful assessment, week 5 here I come!
Hogwarts Social Network
If you’re interested in viewing or contributing to this project, check out the Github Repo here. The README file contains installation instructions, as well as credits to all website sources that made this project possible. Additionally, the video below provides a walkthrough demo of the application: