(I) Do Sinatra Series 2

Continuing your study and understanding of Sinatra

Purpose

This assignment will familiarize you with the basic mechanics of creating a Sinatra application. By going beyond reading to actually writing working code you will further cement your understanding.

Features that you will be creating

  • A Web service to access a database
  • A REST API
  • A Test Suite, TDD
  • Sinatra + ActiveRecord

Initial API Spec

  • GET /api/users/paul => JSON result
  • Properly withhold password
  • Properly return key information

Do this

  • Look at Sinatra Series API Test Example
  • Test is for a set of specific API calls, not of the database or other internals of the service
  • And before the test runs, it creates a test fixture the particular user being looked up
  • For the test to run there has to be a database (ActiveRecord) which is not yet set up

Database

  • Make sure you have rake installed: gem install rake
  • Create a migration rake db:create_migration NAME=create_users
  • Add to the name, password, email and bio fields to the migration
  • rake db:create
  • Create the corresponding ActiveRecord model in model/user.rb
  • Create database and migrate, both development and test databases!

Implement GET

  • get '/api/users/:name' do
  • Retrieve the parameter :name with params[:name]
  • Use normal activerecord features to retrieve database
  • Use .to_json to convert result to json
  • Use .slice to make sure that the password field is not returned

Extend TDD tests

  • Check that email, bio are also returned
  • Check that password is NOT returned
  • Add code to the User model so that password is not returned
  • And tests now pass

Continue to implement, as follows:

  • Implement test cases (TDD), demonstrate that they fail
  • Implement the new APIs
  • Demonstrate the test cases succeed
  • Demonstrate operation of the server using an API testing client
    • Mac: Insomnia
    • Chrome: Advanced Rest Client
    • Many many others!

Additional APIs

  • POST /api/users
    • Supply HTTP attributes: name, email, password and bio
    • Create a new user
    • Use JSON.parse with symbolize_names
    • Return the right status code.
  • DELETE /api/users/:name
    • Delete user with name :name
    • When user with valid :name, then return status OK
    • When user with invalid :name, then return status 404
  • PUT /api/users/:id
    • Supply parameters name, email, password and bio (one or more of them)
    • Return OK or 404 depending on whether the id is valid
    • Update the stated parameters
    • In the test case, verify that the update actually occurred.
  • POST /api/users/:name/validate
    • Validate that a certain user’s credentials are correct
    • Supply parameter: :password (clear text)
    • When supplied password is the password for user :name
      • Returns status OK
      • And name, email, bio and password of said user
    • When not, returns status 404

Submit this

Zip file with your clean and commented source code |files|