My First CLI Project

sindhura gollamudi
2 min readMar 31, 2021

I’m creating my first project after learning Ruby over the last month. I decided to create CLI using Nokogiri to scrape the national parks throughout Michigan. Nokogiri is a gem which allows you to parse HTML and XML into Ruby objects. The core skills for building a program with this are things like defining and initializing a class and its instance(s), iterating through hashes and arrays, and building helper methods.

Getting Started

Make sure that you have your essential gems installed and required in your code. Also, I like to use pry so that I can stop the code if needed and take a look at what’s going on!
I created an environment.rb file and added the following
require ‘nokogiri’
require ‘pry’

Time to create Scraper Class:

Now, let’s think about our Scraper class. What kind of methods should it have? What does this class need to keep track of?
We will need to decide this before defining how our new scraper is initialized. I’ve already given this a little thought, and here’s what I came up with:

class Scraper
attr_accessor :parks_site_url, :park_names_info_urls
end

Let’s break this down. Each instance of a Scraper class should parks_site_url and parks_names_info_urls.

Place Class

At this point in my program, I’ve scraped the two URLs and have the data required to make each park. Each Place instance is an object with properties: `index, :name, :address, :directions, :opening_hrs` This makes it incredibly easy to use and display each object in my controller.

All in all, it’s a very simple class. After the Scrape class does its thing the .all method each containing index, name, address, directions and opening_hrs attributes, all ready to be worked within the CLI class.

CLI Class

A CLI allows you to interact with a program using a terminal. A CLI’s job is taking the users input and then outputting the information. I’ve got that working I can scrape the data I need.

The first method in my app is call. call does the job of running all the methods in the correct order to from start to finish.

--

--