Working with APIs

Dec 6, 2020 · 5 min read

I created a CLI project using Breaking Bad’s API and would like to go over the things I learnt while making this project. (Note: Breaking Bad is one of my favorite tv shows ever, I was super pumped when I found it’s API)

Let’s talk about working with APIs using Ruby as the programming language.

Before I start, let me briefly describe what is an API? Application Programming Interface (API) is a software intermediary that allows two applications to talk to each other. So for example, whenever we use an app like Twitter, check the weather app, or facebook we are using an API.

To parse information from an API we are going to first require gems. What are gems? A ruby gem is a package of code configured so that it complies with the Ruby style of software distribution. Just like Python has libraries, Ruby has gems.

To use these gems in our program we need to create an environment.rb file in the project directory you are working in. But before I start, let me give a brief overview of the file structure that your projects should contain. (This is mostly conventional and doesn’t really affect your code all that much)


To download gems you need to go into the Gemfile and add the gems you wish to use for the project you are working on.

source “"
gem ‘rspec’
gem ‘pry’
gem “json”
gem “nokogiri”
gem “open-uri”
gem “bundler”
gem ‘rest-client’

Note: After typing in this, make sure to type in ‘bundle install’ in your CLI which will automatically create the Gemfile.lock file in your project directory which basically means you have successfully downloaded the gems into your program.

To use the gems listed above you need to type in require ‘gem’ in your environment.rb file. This same file is going to be used to connect all the other .rb files in your project using require relative ‘./lib/example’. This file is a part of the ‘lib’ folder in your project.

Here is an example of how your environment.rb file may look like:

require ‘pry’
require ‘bundler’
require ‘json’
require ‘rest-client’
Bundler.requirerequire_relative “./project_directory/file”
require_relative “./project_directory/cli”
require_relative “./project_directory/api”

You can use gems such as net/http or rest-client to use the data from the API in your program and json to parse this data.
Nokogiri is a gem used when you are scraping data using HTML tags.

I will be using rest-client here.

We are going to be doing this part of the project in class Api which is going to be in api.rb.
Let’s define a method called base url and save our API url in this;

def self.base_url

We use the keyword “self” here since we are making a class method. The easiest trick to know what self is referring to is to remember that self in an instance method is the instance and self in the class method is the class itself.

Let’s define another class method where we retrieve the url above and then use JSON on it to get only the data (minus all the HTML tags)

def self.load_api
response = RestClient.get(base_url + ‘/characters’)
data = JSON.parse(response.body)
data.each do |character|[“name”], character[“birthday”])
def self.load_data
#the response.body returns the data including all the html tags #data returns the parsed data of an API (without all the metadata tags)

#the '/characters' is one of the endpoints of an api. An endpoint is one end of a communication channel. An API can have multiple endpoints such as '/quotes', '/interactions' etc.
#the code is returning objects to us from the API. which is linked with the class Character.#the class method self.load data which loads the characters from the API; this method will be used in our cli.rb by calling this function.

However, the code above will break and give us a NoMethodError for Character class. So let’s quickly create a new file called character.rb and initialize this object.

class Character
attr_accessor :name, :birthday
@@all = []
def initialize(name, birthday)
@name = name
@birthday = birthday
@@all << self
def self.all
#here we have two attribute accessors name and birthday for our Character class.
#the class method self.all reads all the characters from the all array

This brings us to our CLI class. Our application is going to run from here. This is the file where we will be doing most of our coding. Command Line Interface (CLI) is a text-based user interface (UI) used to view and manage computer files along with many other functions. The cli.rb is going to have a lot of methods that will interact directly with the user but I am going to mention only the important ones.

class CLI
def start
puts "Welcome"
puts "Loading..."
def main_menu_options
puts "type '1' for info about characters"
puts "type 'exit' to exit"
def main_menu
input = gets.chomp
if input == "1"
else input == "exit"
puts "exiting application"
def list_characters
Character.all.each.with_index(1) do |character, index|
puts "#{index}: #{}"
#list_characters will give us a list of characters along with the index number starting from 1.#it is important to use .all on Character as this gives us each and every character from our API.

This cli.rb class above is a low level, one level deep class that only lists the name of our imaginary API url. You can add a lot more functionality to this CLI by adding a sub_menu function that goes another level deep and gives information about our characters and other functions such as invalid_choice that will not break when the user enters an input other than the set input above.

How to run this file in the CLI?
This is where the run file in your bin folder of your project directory comes into play.
Make sure to add the environment file here by typing the following.

require_relative ‘../lib/environment’

To run this file, simply type in ruby bin/run in the CLI.

The one thing I learnt while making this project was the use ‘pry’. Just insert ‘binding.pry’ wherever you wish to see what the return value of your code is and work accordingly. I have to admit, I barely used it before working on this project.

This project from phase 1 of Flatiron school was interesting and made me learn quite a bit about CLI and APIs in general. Looking forward to making similar projects to get a better hold of how to use data from an API.

Thanks a lot for reading.


The Startup

Get smarter at building your thing. Join The Startup’s +729K followers.