A web Application Programming Interface (API) is a fancy term for public (or private) data that lives on the Internet for you to access. The data is housed at an endpoint -- represented by a url -- that you can visit in your browser to view the data. Web APIs are often in JSON (JavaScript Object Notation) format, meaning that they look like giant Ruby hashes! This hash-like structure makes it easy for developers like you and me to retrieve and manipulate the API data to fit the needs of the program or application we're writing.
For this lab, you're going to be using the Pokéapi to manipulate some of beloved Charizard's details. Visit http://pokeapi.co/api/v2/pokemon/6/ to get to the information that the API has on Charizard -- the response is HUGE! For the purposes of this lab, let's just work with a much more manageable excerpt of the response. Copy and paste the following hash into your charizard_tables.rb file:
charizard = {
name: "charizard",
weight: 905,
abilities: [
{
slot: 3,
is_hidden: true,
ability: {
url: "http://pokeapi.co/api/v2/ability/94/",
name: "solar-power"
}
},
{
slot: 1,
is_hidden: false,
ability: {
url: "http://pokeapi.co/api/v2/ability/66/",
name: "blaze"
}
}
],
stats: [
{
stat: {
url: "http://pokeapi.co/api/v2/stat/3/",
name: "defense"
},
effort: 0,
base_stat: 78
},
{
stat: {
url: "http://pokeapi.co/api/v2/stat/2/",
name: "attack"
},
effort: 0,
base_stat: 84
},
{
stat: {
url: "http://pokeapi.co/api/v2/stat/1/",
name: "hp"
},
effort: 0,
base_stat: 78
}
]
}
You're going to create two tables from Charizard's information using the terminal-table gem (https://rubygems.org/gems/terminal-table/versions/1.6.0). The first table should contain the character's statistics. The second table should contain the character's abilities. Be sure to run the following command in your terminal before running charizard_tables.rb:
gem install terminal-table
Follow the instructions below to make some magic!
Pro-tip: check out the "pry" gem to debug your program! Check out this great blog post or the official pry documentation for more info.
Create a method #get_stats_names
that retrieves all of the names of the abilities from the Charizard response. The return value of #get_stats_names
should be the array ["defense", "attack", "hp"]
.
Create a method #get_base_stats
that collects all of Charizard's reported stats. The method should return the array [78, 84, 78]
.
Create a method #create_stats_table
that prints out all of Charizard's statistics. Use the methods you've previously created (#get_stats_names
and #get_base_stats
) to populate the table structure according to terminal-table documentation. The final table should look something like this:
See the "charizard_stats_table_example.png"
file in this repo for another look!
Be sure to check out the terminal-table documentation for instructions on how to create and prettify tables!
Create a method #get_ability_details
that collects all of the information about Charizard's abilities: name, slot value, and is_hidden boolean. The method should return the two-dimensional array [["solar-power", 3, true], ["blaze", 1, false]]
.
Create a method #create_abilities_table
that prints out all of Charizard's abilities. Use the method you've previously created (#get_ability_details) to populate the table structure according to terminal-table documentation. The headers for the table should correspond to the values of your two-dimensional details array: ["name", "slot", "hidden"]
. The final table should look like this:
See the "charizard_abilities_table_example.png"
file in this repo for another look!
Done with all of that? Check out these two challenges:
The excerpt only contains four of the 100s of abilities and stats in the Pokemon API response. How would you change the charizard base hash to include two more abilities and two more stats? Would you have to change #get_ability_details
to include the new information in your table?
Change the titles of both tables ("Charizard's Stats" and "Charizard's Abilities") to be red and green, respectively. When you're done with that, the table is your oyster -- color away to your heart's desire!