- Set up an
SQLite
database - Scrape and save data into your database
- Use data to make ruby objects
In this lab, you will:
- Set up your schema
- Scrape data
- Insert it into your database
- Build out methods to manipulate your data.
This mirrors a very "real world" task.
- Set up a local database schema
- Find something on the internet that you want to capture to your local database (copyright etc. permitting) and "scrape it"
- For each interesting thing found on the internet...
- Insert its information into your local database's schema by writing methods
- ...and then write additional methods to your classes so that you can easily work with your local database
We have provided a db/schema_migration.sql
file that will run the SQL
statements to set up your database in db/pokemon.db
. You can initialize the
database file with touch db/pokemon.db
.
Alert: Make sure that your database is located in the
db
directory!
We can tell sqlite3
to execute a migration script upon a new database with a
command like: sqlite3 path/to/database.db < source_of_sql_data
.
In this example, we're going to take information from a web page about those
adorable Pokémon. Instead of having you write a network code that retrieves a
Pokémon catalog and scrapes it, we're going to provide you a "scraper" class in
lib/scraper.rb
and it's going to "scrape" a local HTML file
(pokemon_index.html
). It shouldn't be too hard to see that with just a
little bit of extra code we could scrape a "live" web page.
Your scraper should not do the work of inserting rows into the database.
Its role is to process the file, it should "hand-off" the creation
responsibilities to the Pokemon class. It's appropriate for the scraper to say
Pokemon.create...
but it's not appropriate for the scraper to open up a
connection to the database.
The goal of an ORM class is to "glue" the scraper to the database.
Speaking of ORM classes...
The Pokemon
class (lib/pokemon.rb
) is responsible for saving, adding,
removing, or changing anything about each Pokémon.
It should be handed "raw" Pokémon data from the scraper and handle the ORM work to put the data into the database. It's probably most appropriate to
Create any methods you think you need to make working with the Pokémon data easier.
Notice that #initialize requires keyword arguments.
When you use SQL to insert into a database, you write out the values by hand and insert them into the database. However, when you insert your Pokémon into the database you don't want to insert them into the query via string interpolation because of potential dangerous consequences. Instead, we can do two things:
- We can sanitize the data by removing non-alphanumeric characters
- We can parameterize the query
It is generally recommended to use parameters. Check out this Stack Overflow question for a good example.
View Objectives: on Learn.co and start learning to code for free.