Black Prince’s Ruby — On the Construction of a Monarchical Command Line Interface using Ruby

Oliver Cawthorne
5 min readOct 22, 2020

The royal history of the United Kingdom, spanning from its humble beginnings of the Kingdom of Wessex to the United Kingdom under the House of Windsor, shows a wild tale of conquest, oppression, scandal, and honour. I therefore took it upon myself to create a Command Line Interface (CLI), built with Ruby, designed to provide a toe-deep tour of who the people were that built this nation, for better or worse, into what it stands to be today.

The Black Prince’s Ruby set within the Imperial State Crown of the United Kingdom. Get it? Ruby?

Putting aside national pride for a moment, let us delve into the project itself and the process involved in crafting this summary. I must firstly thank Flatiron School for providing the opportunity to learn and the tools with which to construct this CLI, which I would not have been able to start, let alone do, just three weeks ago.

I shall also give credit to Britannica for supplying this vast quantity of information on which I am able to present this project.

Let’s start with the CLI interface!

The Main Menu.

Fundamentally, the CLI provides a user with a main menu from the get-go, asking whether the user would like to list just over sixty monarchs. Perhaps the user is not particularly interested in such a long list and would prefer to sort by royal house or dynasty. No problem! There are twelve different houses or dynasties to choose from.

The “Houses and Dynasties Menu”.

Suppose you don’t know much about any of the Danish kings which ruled over old Englaland, as it was known to the Danes. I confess that I know very little about early English history myself. Let’s hone into that!

Harold Harefoot is an interesting name…

Voila!

Now, we arrive at a short bio of Harold Harefoot. With an extra bookmarking facility, we are able to store this information and perhaps do some research of our own later. Excellent!

So how does this all work?

The magic all starts with Nokogiri, a Ruby gem designed to parse HTML and XML, in conjunction with the OpenURI gem, a process known as “scraping”. The process is as follows: a webpage from Britannica which lists all of the Kings and Queens of Britain is parsed using these two gems, and we can immediately begin the hunt for four important pieces of data for each monarch stored in rows: their name; their reign; their house or dynasty; and the URL of the hyperlink associated with each name. A little bit of data cleaning is required — one of the rows contains “data” which is not really data at all, some of the monarchs have numbers at the end of their names intended for footnotes, and so on — but it is all over in a few short lines. At the moment, this is all that is required for the main menu. Thus, the first scrape for data is done!

The house, or dynasty, of the monarch is essential for this project. After all, a monarch has one dynasty, and a dynasty has many monarchs. Being able to sort through this list not as one huge, hefty list of monarchs but instead broken down by dynasty is an essential part of a has-many/belongs-to relationship which becomes immensely useful for our dynasty menu. Specifically, every other monarch who does not belong to that dynasty is ignored, and all relevant ones are plugged straight into a temporary array of instances from which we can select our eye-catching monarch.

It is all well and good to navigate to each monarch at this stage, but the problem is that there is not much more information than this. What was their official title as a monarch? What is their story? Nobody yet knows, and this is where the second scrape comes in. When a monarch is selected, this is the perfect time to initialise another scrape!

We now crack open Nokogiri and OpenURI for the second time on the selected instance of a monarch to take a peek at the title and the biography. The biography is lengthy, and only a short summary is desired, so we can look at, perhaps, the first two paragraphs. We store this biography permanently into the monarchical instance as well as the title. Should we want more biographies and titles, all we have to do is summon the monarch and they shall appear! This functionality of scraping a second time with each called instance of a monarch is essential to the overall speed of the program; scraping all of the biographies from every single URL at the very beginning could be an infuriatingly long wait.

The bookmarks are a simple, fun addition. Should you be interested in a monarch, bookmarking it stores each instance into an empty array which can be called upon later. Duplicate bookmarks are not permitted, the bookmarks can be cleared (playfully referred to as “deposing monarchs” in the CLI), and a friendly reminder upon exiting the CLI will tell you which monarchs you had saved before exiting the program, and insists that you do some more reading!

For the monarchs that really catch your eye!

The application was immense fun to build, sometimes desperately frustrating, and other times deeply rewarding, but I am pleased that I can share this little project. I am excited to share with you my future projects with Flatiron School in the weeks to come!

Happy studies!

--

--

Oliver Cawthorne

Physics student turned accountant turned software engineering student.