A Guide to Any Upcoming SWE Flatiron Students Approaching their Final Project for Phase 1
In Flatiron’s SWE Bootcamp, phase 1’s final project will have you build a CLI program utilizing an API of your choice. At this point, you would have gone through your Procedural Ruby and OOP Ruby modules. This project will be focusing on OOP Ruby concepts, so make sure to touch up on your modules before tackling this project and don’t forget to ask your cohort-lead for any questions when you’re stuck on something! :)
I’ll be walking you through a step-by-step guide on how I setup and built my project from scratch. Hopefully this guide will be useful to any students currently in phase 1 or upcoming students looking to enroll in Flatiron’s SWE Bootcamp.
Phase 1 Final Project : CryptoStats CLI
What is CryptoStats?
CryptoStats is a CLI ruby gem that fetches data of the top 100 cryptocurrencies from the CoinGecko API and displays updated statistics regarding the current prices, market cap, volume, price change, etc.
Getting Started
- Open your terminal and install bundler.
$ gem install bundler
2. Create the gem folder for our project.
$ bundle gem cryptostats_cli
Refer to the image below to create the rest of the files needed for this project. (you can ignore the .gem files)
Creating and linking our GitHub repository
- Create a GitHub account or login to your existing account if you already have one.
- Create your GitHub repository without a README.md file.
- Connect your GitHub repository to your local repository, open your terminal and run the command below inside your project folder.
$ git remote add origin git@github.com:[USERNAME]/cryptostats_cli.git
4. Let’s stage, commit, and push our new files into our GitHub repository by running the commands below.
$ git add .
$ git commit -m "initial commit"
$ git push origin master
5. After successfully running everything above without any errors, refresh the page for your GitHub repository. You should see that your files have been successfully uploaded into GitHub.
Time to code!
1. Before we begin, we will need to include the gems we’ll be using for our project inside our Gemfile.
source 'http://rubygems.org'gem 'pry'
gem 'rest-client'
gem 'awesome_print'
gem 'rake', '~> 12.0'
gem 'rspec', '~> 3.0'
2. Include the following code in our environment.rb
require 'pry'
require 'rest-client'
require 'json'
require 'awesome_print'require_relative "./models/cryptostats"
require_relative "./services/api"
require_relative "./services/cli
3. Run the code below inside our project folder’s terminal to install the gems needed for our project.
$ bundle install
Working on our /lib and /bin files
- Models
We will be defining a class called CryptoStats that has a class variable @@all
which we will use to store all of our instances of CryptoStats. We will then define the initialize method that takes in a hash as an argument and iterate through it using Ruby’s .each
method with a key-value pair which we will use to create our instances of CryptoStats using Mass Assignment.
Our class method .all
is a getter method returns an array of CryptoStats instances.
We will then define a method called display_stats
which will print out to the user, the values stored in the keys id
, symbol
, current_price
, market_cap
, total_volume
, high_24h
, low_24h
, price_change_24h
, price_change_percentage_24h
, and last_updated
.
2. Services
We will create a class Api and give it 3 class methods base_url
, load_data
, and load_cryptocurrency
.
Our base_url
method will return the URL linking to our API.
In our load_cryptocurrency
method, we will be sending a request to our API by calling the RestClient
class and passing to its .get
method our base_url
, and store the result in a variable called response
.
We will then run JSON.parse
and pass into it response.body
to convert the string value inside of that variable into a JSON object and store it in a variable called data
which should return an array of hashes.
We will then iterate through data
using Ruby’s .each
method and create new instances of CryptoStats class by initializing it with each of the attributes within the attribute hash.
In our CLI class, we will define 5 methods, start
, main_menu
, main_menu_options
, get_input
, and standardize_crypto_name_length
.
Our start
method will load the data we need from our API into our program and print out our CLI’s main menu.
Our main_menu
method prints out our list of the top 100 cryptocurrencies from CoinGecko and runs our main_menu_options
method.
Our main_menu_options
method prompts the user to select an option and validates for user input. If the user enters the string “exit”, the program will close, if the user enters a number from 1–100, the program will pull the data of the selected cryptocurrency, and if the user enters anything besides the previous two options, the program will throw out an error and will prompt the user to enter another value.
Our get_input
method will be used to store user input data into variables.
And our standardize_crypto_name_length
will be used to set a standard string length for each of the cryptocurrency names. This will help keep the names aligned when we print them out to the user later.
3. /bin
Finally, insert the following code into our run
file.
#!/usr/bin/env rubyrequire_relative "../lib/environment"Cli.new.start
Our first line #!/usr/bin/env ruby
is a shebang line that tells the program loader to run this file in a ruby environment.
We then include the path to environment.rb
which contains all of the gem files and local directories we need to run our program.
Lastly, we initialize a new instance of Cli
and invoke its method .start
to start our program and we’re done! Run the code below in your terminal to see what our CLI program looks like!
$ bin/run