Skills Dev 2

Skills Dev Assignment

This assignment will ask you to explore the concepts introduced in class and set up a data-driven API.

Brief: Iterate last week’s assignment into a web api that runs from a database to provide resources for this course

Due Date: Tuesday Nov 8, 12pm ET

Submission: Add completed code to ‘skills2’ folder in Student section of the course’s GitHub Repo.

Learning Objectives

As part of the exercise, students will:

  • build familiarity with Sinatra web APIs (or application programming interfaces) using Ruby;
  • learn how to use JSON responses to return machine readable data-centered responses to a web request;
  • become familiar with databases and how to use tables and records to drive data-drive web-applications using activerecord;
  • apply more advanced concepts in programming to create dynamic responses from a web application (models, loops, validations etc.);

Requirements

For this project, you are to start with the web application we built last week (see week1-solution in the code repo).

Using this as a starting point, you’re going to iterate on the concepts

Using this template, create a basic web ‘api’ that will serve up information about this course to someone who might want to make use of it.

As part of this project you will be asked to add the following functionality to your Sinatra application. For all of the following use a get request.

Basic

  1. Update all of the routes and errors to return formatted JSON responses

Hint: You’ll need to add the ‘json’ gem to your project file.

Hint you can create json easily by creating an hash object like this and using the to_json method on it e.g. { name: "Some name", name_of_array: ["item 1", "item 2", "item 3"], is_it_true: false, counter: 45 }.to_json { status: "OK", message: "The course is called ... " }.to_json

Hint use the following to specify routes will return json at the top of your file

# before do # content_type :json # end

Links

  1. Convert the ‘interesting links’ to be stored in a database. Create a table called ‘course_links’ and a model called ‘course_link’ with the following: - a column called ‘url’ (a string) that will hold the link - a column called ‘title’ (a string) that will have some descriptive information about the link. - seed the database with some initial data. - Test: adding, updating, deleting links

  2. Add the relevant endpoints to allow you to list, create, update, read and destroy records in the database.
    • Add GET ‘/course_links’ that will list all of the links
    • Add POST ‘/course_links’ that will create a new link
    • Add GET ‘/course_links/:id’ that will return a specific link
    • Add PUT ‘/course_links/:id’ that will update the url and title of the link
    • Add DELETE ‘/course_links/:id’ that will remove/destroy any link.
    • Test: adding, updating, deleting links.
    • Hint: use cURL for POST, PUT and DELETE methods
  3. Update the interesting_link method to return a result from ActiveRecord (and the Sqlite Database)

  4. Organize the links by week using a relational table.
    • Create a migration and add a new (integer) column called ‘week_id’ to the course_links table
    • Create a new table called weeks that contains two columns topic (text) and beginning (datetime)
    • In the models for week and course_link define the associating using belongs_to and has_many
    • Seed the database with the summary schedule from the course site.
    • Add the relevant endpoints to allow you to list, create, update, read and destroy records in the database.
    • Update PUT ‘/course_links/:id’ to accept a parameter for week_id and update it if it’s passed
    • Test adding, updating, deleting links.

Assignments

  1. Create a new table called assignments and add a corresponding model. It should contain the following
    • Column: description (text) required - a summary of the assignment
    • Column: week_id (integer) required - the week it’s assigned
    • Column: url (text) - a link to the assignment, if applicable
    • Column: due_date (date_time) - the deadline for the project.
    • Create the appropriate endpoints and actions
    • Create a custom JSON renderer (hint: add a to_json method to your model *) that shows if the due_date has passed or not.

Respond To

For every data driven element of the API, update it to respond to html (default), xml or json.

Check it using cURL curl -v -H "Accept: text/html, */*" http://localhost:9393/tasks curl -v -H "Accept: application/xml, */*" http://localhost:9393/tasks curl -v -H "Accept: application/json, */*" http://localhost:9393/tasks

Advanced

  1. Right now anyone can add, edit or delete these resources. Can you think of a simple way to secure it so that only trusted people can perform sensitive actions?

  2. Once implemented, return an appropriate error if an unauthorised/untrusted visitor tries to access it.

Experiment

Try out some other stuff. Practice beyond these examples and add:

  1. at least one of your own tables to the database
  2. a set of endpoints that allow you to access and manipulate that data

Grading

Each component above is graded equally. Full grade (100%) is awarded for

  • all required endpoints and paramters correctly defined;
  • returing the correct information in response to a request; and
  • a valid implementation of the functionality to deliver that response;