A simple API for books.
-
Fork and clone this repository.
-
Change into the new directory.
-
Install dependencies with
bundle install. -
Create a
.envfor sensitive settings (touch .env). -
Generate new different secrets for both
developmentandtestenvironments (bundle exec rake secret). -
Store them in
.envwith keysSECRET_KEY_BASE_<DEVELOPMENT|TEST>respectively. -
Create a database with
bundle exec rake db:create. -
Create a database schema with
bundle exec rake db:migrate. -
Add data to the database with
bundle exec rake db:seed db:examples. -
Run the HTTP server with
bundle exec rails serverorbin/rails server.
library-api-guide 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.
rake routeslists the endpoints available in your API.rake testruns automated tests.rails consoleopens a REPL that pre-loads the API.rails dbopens your database client and loads the correct database.rails serverstarts the API.scripts/*.shrun variouscurlcommands to test the API. See below.
| 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 |
| 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 |
Request:
curl --include --request POST http://localhost:4741/sign-up \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "an@example.email",
"password": "an example password",
"password_confirmation": "an example password"
}
}'scripts/sign-up.shResponse:
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 1,
"email": "an@example.email"
}
}Request:
curl --include --request POST http://localhost:4741/sign-in \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "an@example.email",
"password": "an example password"
}
}'scripts/sign-in.shResponse:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 1,
"email": "an@example.email",
"token": "33ad6372f795694b333ec5f329ebeaaa"
}
}Request:
curl --include --request PATCH http://localhost:4741/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.shResponse:
HTTP/1.1 204 No ContentRequest:
curl --include --request DELETE http://localhost:4741/sign-out/$ID \
--header "Authorization: Token token=$TOKEN"ID=1 TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/sign-out.shResponse:
HTTP/1.1 204 No Content| Verb | URI Pattern | Controller#Action |
|---|---|---|
| GET | /users |
users#index |
| GET | /users/1 |
users#show |
Request:
curl --include --request GET http://localhost:4741/users \
--header "Authorization: Token token=$TOKEN"TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/users.shResponse:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"users": [
{
"id": 2,
"email": "another@example.email"
},
{
"id": 1,
"email": "an@example.email"
}
]
}Request:
curl --include --request GET http://localhost:4741/users/$ID \
--header "Authorization: Token token=$TOKEN"ID=2 TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/user.shResponse:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 2,
"email": "another@example.email"
}
}- 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- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.
