Assignment #3 – Advanced Ruby
Objective
For this assignment, you will be creating a query API for a CSV file of students. The API will
query a ‘YAML-base’ which you generate from the CSV file. Your API will be tested with an API
client that is provided to you.
Models
1. You are given a CSV file named s t u d e n t s . c s v
. This file contains 15000 fake student
records (along with headers) containing the following fields: G i v e n N a m e
, S u r n a m e
,
S t r e e t A d d r e s s
, C i t y
, S t a t e
, Z i p C o d e
, E m a i l A d d r e s s
, G e n d e r
, P o u n d s
2. Create a c l a s s
called C o u r s e
in c o u r s e . r b
which contains c o u r s e _ n a m e
and c o u r s e _ i d
fields which are set via the class constructor. Provide appropriate read/write accessors
to those fields.
3. Create a c l a s s
called S t u d e n t
in s t u d e n t . r b
which contains the following fields (with
appropriate read/write accessors): s t u d e n t _ i d
, f i r s t _ n a m e
, l a s t _ n a m e
, c i t y
, s t a t e
,
e m a i l
, g e n d e r
, p o u n d s
, g p a
and t a k i n g _ c o u r s e s
.
4. The S t u d e n t
class should have an empty constructor, but should provide an ability to be
initialized via a block initializer as discussed in the lecture materials.
5. Both C o u r s e
and S t u d e n t
classes should override the t o _ s
method.
Database Generator
1. Create d b _ g e n e r a t o r . r b
file which will:
Read in the CSV file using Ruby’s CSV library (see PickAxe for an example of
reading in a * . c s v
file with headers).
Create an array of students, where each student will have its fields populated
based on the values of the * . c s v
file with the exception of s t u d e n t _ i d
, g p a
, and
t a k i n g _ c o u r s e s
fields. Keep in mind that pounds is a floating point number
and should be stored appropriately.
s t u d e n t _ i d
should just be a value which starts with 1
and gets incremented every
time a new instance of student is created (presumably finishing ~15000)
g p a
should be a randomly generated floating point number with the lowest value
being 2 . 0
and the highest 4 . 0
(Hint: take a look at Ruby’s built-in r a n d
function)
t a k i n g _ c o u r s e s
should be an array of 0 to 4 C o u r s e
objects. The idea here is that
each student is taking a different number of courses. Some students might not
taking any courses while others can be taking all four.
Create 4 courses with some id and name and randomly ‘assign’ 0 to 4 courses to
1 of 3
each created student. (Hint: Ruby has a wonderful s a m p l e
method, which can
choose n
random elements from an array.)
2. Serialize the students array to u n i v e r s i t y _ d b . y m l
using Ruby’s YAML library (see
Pickaxe book for an example).
API
1. Create D b A p i
class in d b _ a p i . r b
which will serve as an API/DSL for selecting records
from the ‘YAML-base’ based on certain conditions. DO NOT re-load the YAML file
every time, rather store students loaded from the YAML file in a class variable called
s t u d e n t s
that is retrievable from a class method called s t u d e n t s
.
2. D b A p i
needs to have the following methods implemented:
r e q u i r e _ r e l a t i v e ‘ s t u d e n t ‘
r e q u i r e _ r e l a t i v e ‘ c o u r s e ‘
r e q u i r e ‘ y a m l ‘
c l a s s D b A p i
c l a s s
d e f s e l f . s t u d e n t s . . . e n d
d e f s e l f . s e l e c t _ b y _ g e n d e r ( g e n d e r ) . . . e n d
d e f s e l f . s e l e c t _ b y _ f i r s t _ n a m e ( f i r s t _ n a m e ) . . . e n d
d e f s e l f . s e l e c t _ b y _ l a s t _ n a m e ( l a s t _ n a m e ) . . . e n d
d e f s e l f . s e l e c t _ b y _ w e i g h t _ m o r e _ t h a n ( p o u n d s ) . . . e n d
e n d
3. The f i r s t _ n a m e
and l a s t _ n a m e
methods should accept a Regexp matcher as an
argument.
4. d b _ c l i e n t . r b
(provided) will use your API. A sample output from running d b _ c l i e n t . r b
is as follows:
2 of 3
The d b _ c l i e n t . r b
prints an intersection of the number of males, the number of people whose
first name contains ‘Andr’, and the number of people who weigh more than 200 pounds.
Submission Guidelines
1. Commit all the files to you local repo and push your repo out to your private team repo
on gihub creating a remote branch called a s s i g n m e n t 3
2. You can do one of the following to accomplish the above:
Create a local branch called a s s i g n m e n t 3
($ g i t c h e c k o u t – b a s s i g n m e n t 3
) and
then push it out by doing $ g i t p u s h r e m o t e _ a l i a s a s s i g n m e n t 3
Work on a m a s t e r
branch in your repo and then to $ g i t p u s h r e m o t e _ a l i a s
m a s t e r : a s s i g n m e n t 3
3. Feel free to include a README with any comments
3 of 3