Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

ga-wdi-boston/listr-api

Repository files navigation

General Assembly Logo

listr-api

An API for ember-resources

Installation

  1. [Fork and] clone this repository.
  2. Install dependencies with bundle install.
  3. Create a .env for sensitive settings (touch .env).
  4. Generate new development and test secrets (bundle exec rake secret).
  5. Store them in .env with keys SECRET_KEY_BASE_<DEVELOPMENT|TEST> respectively.
  6. Setup your database with bin/rake db:nuke_pave or bundle exec rake db:nuke_pave.
  7. Run the API server with bin/rails server or bundle exec rails server.

Tasks

Developers should run these often!

  • bin/rake routes lists the endpoints available in your API.
  • bin/rake test runs automated tests.
  • bin/rails console opens a REPL that pre-loads the API.
  • bin/rails db opens your database client and loads the correct database.
  • bin/rails server starts the API.
  • scripts/*.sh run various curl commands to test the API. See below.

API

Lists

Verb URI Pattern Controller#Action
GET /lists lists#index
GET /lists/:id lists#show
POST /lists lists#create
PATCH /lists/:id lists#update
DELETE /lists/:id lists#destroy

GET /lists

Request:

curl --include --request GET http://localhost:4741/lists

Response:

[
  {
    "id": 1,
    "title": "Favorite Things",
    "hidden": false,
    "items": [
      1,
      2,
      3,
      4,
      5
    ]
  },
  {
    "id": 2,
    "title": "Todo",
    "hidden": false,
    "items": [
      6,
      7
    ]
  }
]

GET /lists/:id

Request:

curl --include --request GET http://localhost:4741/lists/$ID

Response:

{
  "id": 1,
  "title": "Favorite Things",
  "hidden": false,
  "items": [
    1,
    2,
    3,
    4,
    5
  ]
}

POST /lists

Request:

curl --include --request POST http://localhost:4741/lists \
  --header "Content-Type: application/json" \
  --data '{
    "list": {
      "title": "Groceries",
      "hidden": false
    }
  }'

Response:

{
  "id": 3,
  "title": "Groceries",
  "hidden": false,
  "items": []
}

PATCH /lists/:id

Request:

curl --include --request PATCH http://localhost:4741/lists/$ID \
  --header "Content-Type: application/json" \
  --data '{
    "list": {
      "hidden": true
    }
  }'

Response:

HTTP/1.1 204 No Content

DELETE /lists/:id

Request:

curl --include --request DELETE http://localhost:4741/lists/$ID

Response:

HTTP/1.1 204 No Content

Items

Verb URI Pattern Controller#Action
GET /lists/:list_id/items items#index
GET /items/:id items#show
POST /lists/:list_id/items items#create
PATCH /items/:id items#update
DELETE /items/:id items#destroy

GET /lists/:list_id/items

Request:

curl --include --request GET "http://localhost:4741/lists/$LIST_ID/items"

Response:

[
  {
    "id": 1,
    "content": "Cats",
    "done": false,
    "list": 1
  },
  {
    "id": 2,
    "content": "Star Wars",
    "done": false,
    "list": 1
  },
  // ...
]

GET /items/:id

Request:

curl --include --request GET http://localhost:4741/items/$ID

Response:

{
  "id": 1,
  "content": "Cats",
  "done": false,
  "list": 1
}

POST /items

Request:

curl --include --request POST http://localhost:4741/items \
  --header "Content-Type: application/json" \
  --data '{
    "item": {
      "content": "Coding",
      "done": false,
      "list_id": 1
    }
  }'

Response:

{
  "id": 8,
  "content": "Coding",
  "done": false,
  "list": 1
}

PATCH /items/:id

Request:

curl --include --request PATCH http://localhost:4741/items/$ID \
  --header "Content-Type: application/json" \
  --data '{
    "item": {
      "done": true,
    }
  }'

Response:

HTTP/1.1 204 No Content

DELETE /items/:id

Request:

curl --include --request DELETE http://localhost:4741/items/$ID

Response:

HTTP/1.1 204 No Content

Authentication

Verb URI Pattern Controller#Action
POST /sign-up users#signup
POST /sign-in users#signin
PATCH /change-password/:id users#changepw
DELETE /sign-out/:id users#signout

POST /sign-up

Request:

curl http://localhost:4741/sign-up \
  --include \
  --request POST \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "'"${EMAIL}"'",
      "password": "'"${PASSWORD}"'",
      "password_confirmation": "'"${PASSWORD}"'"
    }
  }'
EMAIL=ava@bob.com PASSWORD=hannah scripts/sign-up.sh

Response:

HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 1,
    "email": "ava@bob.com"
  }
}

POST /sign-in

Request:

curl http://localhost:4741/sign-in \
  --include \
  --request POST \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "'"${EMAIL}"'",
      "password": "'"${PASSWORD}"'"
    }
  }'
EMAIL=ava@bob.com PASSWORD=hannah scripts/sign-in.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 1,
    "email": "ava@bob.com",
    "token": "BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f"
  }
}

PATCH /change-password/:id

Request:

curl --include --request PATCH "http://localhost:4741/change-password/$ID" \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "passwords": {
      "old": "'"${OLDPW}"'",
      "new": "'"${NEWPW}"'"
    }
  }'
ID=1 OLDPW=hannah NEWPW=elle TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/change-password.sh

Response:

HTTP/1.1 204 No Content

DELETE /sign-out/:id

Request:

curl http://localhost:4741/sign-out/$ID \
  --include \
  --request DELETE \
  --header "Authorization: Token token=$TOKEN"
ID=1 TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/sign-out.sh

Response:

HTTP/1.1 204 No Content

Users

Verb URI Pattern Controller#Action
GET /users users#index
GET /users/1 users#show

GET /users

Request:

curl http://localhost:4741/users \
  --include \
  --request GET \
  --header "Authorization: Token token=$TOKEN"
TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/users.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "users": [
    {
      "id": 2,
      "email": "bob@ava.com"
    },
    {
      "id": 1,
      "email": "ava@bob.com"
    }
  ]
}

GET /users/:id

Request:

curl --include --request GET http://localhost:4741/users/$ID \
  --header "Authorization: Token token=$TOKEN"
ID=2 TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/user.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 2,
    "email": "bob@ava.com"
  }
}

Reset Database without dropping

This is not a task developers should run often, but it is sometimes necessary.

  • locally
bin/rake db:migrate VERSION=0
bin/rake db:migrate db:seed db:examples
  • heroku
heroku run rake db:migrate VERSION=0
heroku run rake db:migrate db:seed db:examples
  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published