Ruby data gem Starter kit

Jonathan Fernandez
3 min readJul 10, 2019

Hi, I am Jonathan and in this post I’ll describe how I built out a simple Ruby CLI data gem.

This project called Daily history will provide a quick glance into the history of the current day. I used the webpage https://www.timeanddate.com/on-this-day/ to scrape the data from. The CLI would greet you with:

“ What Happened On This day - Current Date “

And will list a short list of historical headlines in numeric order

The user will then be able to choose a headline to read more about by selecting the number of the headline. Once the number of the headline is selected the user will get a brief summary of the chosen headline.

1. Set up local environment:

I went from the in browser Learn environment to local environment. Below is a link to the instructions on how to get that set up.

https://help.learn.co/en/articles/900121-mac-osx-manual-environment-set-up

A. Took a bit to set up but well worth it since you will get familiar with the file organization aspect of programming, creating, and retrieving of files.

B. Learn how your Terminal and your text editor will work together to create your present and future projects.

C. These are the work flows real world Software developers use so it will come in handy to learn now instead of later.

2. Install Bundler gem :

Install Bundler gem that will pre set your file structure such as

lib : Which will hold hold all the main files of your Cli gem

bin: Where all the executable files will be located. In example your console file or ./bin/daily_history to run the CLI

In addition I added a daily_history.rb file that would consist of all my “required_relative” & “required” files so that the cli files can collaborate with each other.

Gem bundler will also pre set your spec & readme.

A. To install Bundler for my project:

bundle gem daily_history

3. Scraper.rb :

This Scraper class will scrape “www.timeanddate.com/on-this-day ” homepage for data.

A. In my scrape_all method I have

doc = Nokogiri::HTML(open(“https://www.timeanddate.com/on-this-day/")) and then on terminal ran the ./bin/console executable to narrow down the specific data I wanted to use for my cli.

B. After that ran DailyHistory::Scrape.scrape_all

C. Then doc.to_html to get elements from webpage (This is where you can experiment with the elements of the page to get the data you want)

#title — This will scrape the title to my CLI: “What happened On this day -(with current date displaying)“

#headlines This scraper method will return the headlines in numeric order

#summary — Will return the summary of the chosen #headlines

4. cli.rb :This file will display logic and user input

A. CLI opens with a greeting for the user with the scraped #Title from web page as: What happened on this day (Current day)

B. Then CLI will list_headlines in numeric order for user to select by number.

C. Once one of the headlines is selected by number the CLI will return and display a summary associated to the headline.

D. Headline and summary will both display.

E. There is an option for user to list #headlines again or “exit” to exit CLI which will call the #goodbye method

Below is the link to my git hub for daily_history https://github.com/JFern20/daily_history

--

--