Sitemap

My first ruby CLI Data Gem Project

5 min readOct 27, 2018

--

Hello, my name is Mac Dowell and I am a Flatiron School student. In this post, I will describe step-by-step how I build my CLI Data Gem.

This project involve building a gem that scrape data from a website. For my project I scraped data from the website ‘Washington Post’. The website provides a list of The Top 100 players and their statistics for the 2017–18 NBA season. So I tooke data from this website to build my ruby gem that gonna shows the list of the players by sorting them by Age, PPG, BLK, 3PT… and group them by trend.

So here are some tips and thinking process I used to built my CLI Gem ‘best_nba_players_18’.

1. Install Bundler Gem and create the gem

For building my ruby gem I used Bundler. To not reinvent the wheel I installed bundler gem using bundler gems guide.

So I opened a text editor, I used VSCode, and type these commands in the terminal.

Install Bundler Gem with this command :

$ gem install bundler

Creat the gem using Bundler, use this command :

$ bundle gem best_nba_players_18

Tip: the code after the $ belong to the terminal.

The last command creates a scaffold directory for my new gem.

2. Add an Executable File

I created a new file inside my gem’s bin directory and name it the same name as my gem “best_nba_players_s18” without extension. I make my file (“best_nba_players_s18”) executable adding this line at the top and using chmod:

!/usr/bin/env ruby

$ chmod +x bin/best_nba_players_s18

The executable file itself just needs a shebang in order to figure out what program to run it with since is not a ruby file. Executable files belong to the bin directory.

And copy paste the requires from the “console” file inside my executable file.

3. File Requirements

To set up my project environment, I required all gems and files I’ll need, inside the file’s module “best_nba_players_s18.rb”

Press enter or click to view image in full size

As you can see I added 3 files inside the module so that mean I’ve to creat 3 ruby files names (cli.rb, players.rb, scraper.rb)

4. Gemspec File

The ‘.gemspec’ specifies the information about a gem such as its name, version, description, authors, homepage… I wrote a small summary, homepage and added my executable file name in ‘spec.executable’ to my ‘.gemspec’ file. You can get some error messages if this step is not done or some required gemspec attributes is missing. I also added dependencies like Nokogiri, Colorize, Terminal-table… as I require them in the step below. (more about .gemspec)

Press enter or click to view image in full size

5. Write Scraper class

After setting up my environment, I was ready to write some code.

First I create my ‘scraper.rb’ file inside best_nba_players_s18 directory.

This file contains the scraper class that has a class method using the self keyword. This method take an url argument and inside it I use ‘open’ and ‘Nokogiri’ to scrape the data from the website by digging the css elements and using children.

Here are the process inside the method:

Grab HTML web page, and parsed the HTML using Nokogori to select all elements that are inside < div class = “player-container trend-neutral” div class=”player-info-inne”> as there is 3 trend so I select elements inside <trend-up>and <trend-down> too, and then iterated over the elements unit with ‘.map do’ to get name, trend, position, team, statistcs… of each players and put them into a hash. So the scrape_page’s method returns a nested array in wich each index is an array that contains several hashes.

Press enter or click to view image in full size

6. Write Players class

The next file I created is the players class for makeing players.

Get Mac Prosper’s stories in your inbox

Join Medium for free to get updates from this writer.

The goal of this class is for managing players’ list after create these players.

The initialize method takes an hash as argument and create instance of players class. Each instance of players is save inside a class variable (all) so I can manage them as needed.

And I created some other classe methods like:

  • ‘create_players_from_collection’ takes an collection as argument and iterates over it to create instance of players.
  • ‘sort’ takes the input from the user throw the cli class and calls the sort_asc or sort_desc.
  • ‘sort_desc’ and ‘sort_asc’ takes the choice and sorts the class variable ‘all’ by the choice. So I can have a player’s list sort by name, PPG, age…
  • ‘find’ takes an input and returns the player who is at this position inthe class variable ‘all’. Allow me to find a player from the ‘all’.
  • ‘group’ takes a choice, selects players that mache the choice and returns them in an array. Allow me to group players by trend, team, position…
  • ‘find_group’ takes an index and returns the player who is at this position.

7. Write CLI class

And to finidh I created a class to manage my gem.

I called it CLI and has some class methods like

  • ‘run’ takes the collection from the Scraper class and calls ‘create_from_collection’ from Players class to make players
  • ‘start’ the main method that manage the gem, shows the menu and take input from the user, calls class methods of Players class.
  • ‘print_players’ to show the list of players.
  • ‘print_player’ to show the selected player’s information.
  • ‘print_players_group’ to show the selected group of players.

I used colorize gem to make my CLI gem prettier and to give some color.

8. Call the start method

In the final step I called the start method from CLI class inside my executable file.

9. Launch the gem

To launch the gem type the following commands:

$ ./bin/setup

$ ./bin/best_nba_players_s18 or ruby bin/best_nba_players_s18

In the end was very satisfying to see it working. Mission accomplished!

* Install the gem

Type the following commandes

$ gem install best_nab_players_18

$ best_nab_players_18

Enjoy it!!!

--

--