Giter Club home page Giter Club logo

library-api's Introduction

General Assembly Logo

library-api

A simple API for books.

Installation

  1. Fork and clone this repository.
  2. Change into the new directory.
  3. Install dependencies with bundle install.
  4. Add secrets to config/secrets.yml.
  5. Create a database with bundle exec rake db:create.
  6. Create a database schema with bundle exec rake db:migrate.
  7. Add data to the database with bundle exec rake db:seed db:examples.
  8. Run the HTTP server with bundle exec rails server.

Structure

library-api follows the standard project structure for Rails 4.

User authentication is built-in.

curl command scripts are stored in scripts with names that correspond to API actions.

Tasks

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

API

books

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

GET /books

GET /books/:id

POST /books

PATCH /books/:id

DELETE /books/:id

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 --include --request POST http://localhost:3000/sign-up \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "[email protected]",
      "password": "an example password",
      "password_confirmation": "an example password"
    }
  }'
scripts/sign-up.sh

Response:

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

{
  "user": {
    "id": 1,
    "email": "[email protected]"
  }
}

POST /sign-in

Request:

curl --include --request POST http://localhost:3000/sign-in \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "[email protected]",
      "password": "an example password"
    }
  }'
scripts/sign-in.sh

Response:

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

{
  "user": {
    "id": 1,
    "email": "[email protected]",
    "token": "33ad6372f795694b333ec5f329ebeaaa"
  }
}

PATCH /change-password/:id

Request:

curl --include --request PATCH http://localhost:3000/change-password/$ID \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "passwords": {
      "old": "an example password",
      "new": "super sekrit"
    }
  }'
ID=1 TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/change-password.sh

Response:

HTTP/1.1 204 No Content

DELETE /sign-out/:id

Request:

curl --include --request DELETE http://localhost:3000/sign-out/$ID \
  --header "Authorization: Token token=$TOKEN"
ID=1 TOKEN=33ad6372f795694b333ec5f329ebeaaa 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 --include --request GET http://localhost:3000/users \
  --header "Authorization: Token token=$TOKEN"
TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/users.sh

Response:

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

{
  "users": [
    {
      "id": 2,
      "email": "[email protected]"
    },
    {
      "id": 1,
      "email": "[email protected]"
    }
  ]
}

GET /users/:id

Request:

curl --include --request GET http://localhost:3000/users/$ID \
  --header "Authorization: Token token=$TOKEN"
ID=2 TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/user.sh

Response:

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

{
  "user": {
    "id": 2,
    "email": "[email protected]"
  }
}

License

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

library-api's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.