A simple API for books.
-
Fork and clone this repository.
-
Change into the new directory.
-
Install dependencies with
bundle install
. -
Create a
.env
for sensitive settings (touch .env
). -
Generate new different secrets for both
development
andtest
environments (bundle exec rake secret
). -
Store them in
.env
with 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 server
orbin/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 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 variouscurl
commands 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": "[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]"
}
}
Request:
curl --include --request POST http://localhost:4741/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"
}
}
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.sh
Response:
HTTP/1.1 204 No Content
Request:
curl --include --request DELETE http://localhost:4741/sign-out/$ID \
--header "Authorization: Token token=$TOKEN"
ID=1 TOKEN=33ad6372f795694b333ec5f329ebeaaa scripts/sign-out.sh
Response:
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.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]"
}
]
}
Request:
curl --include --request GET http://localhost:4741/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]"
}
}
- 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 [email protected].