Assignment #3 Advanced
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.
1. You are given a CSV file named students.csv
. This file contains 15000 fake student records (along with headers) containing the following fields: GivenName ,Surname StreetAddress ,City ,State ,ZipCode ,EmailAddress ,Gender ,Pounds 2. Create a class Called Course in course.rb which contains course_name and course_id fields which are set via the class constructor. Provide appropriate read/write accessors to those fields. 3. Create a class called Student in student.rb which contains the following fields (with appropriate read/write accessors): s t u d e n t _ i d ,first_name ,last_name ,city ,state email ,gender ,pounds ,gpa And taking courses .
4. The Student 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 Course and Student classes should override the t o _ s method.
1. Create d b _ generator.rb 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 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 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 Api 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 students
that is retrievable from a class method called students
2. D b A p i needs to have the following methods implemented: require_relative ‘student’ require_relative ‘course’ require ‘yaml’ class DbApi class def self.students … end def self.select_by_gender (gender) … end def self.select_by_first_name (first_name) … end def self.select_by_last_name (last_name) … end def self.select_by_weight_more_than(pounds) … end 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
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:
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.
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
($git checkout -b assignment3
) 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
3. Feel free to include a README with any comments
Assignment #3 Advanced