This application is a Ruby on Rails API for storing recipes for users. Users can create, update, and delete recipes.
API URL: https://sjacobs146-recipe-api.herokuapp.com
- Ruby
- Rails
- PostgreSQL
Please see the front-end component of this project for more information about my planning process, etc.: https://github.com/sjacobs146/recipe-tracker
Note: not all of the entities were implemented
I began this full-stack project by implementing the back-end first. I started with the simplest relationship: users have many recipes. I created the recipes resource first. I used the Rails scaffold command to generate the database migration script, model and controller. Once I was able to create records in the Recipes table, I added the relationship to users. I updated the recipes controller to extend ProtectedController so that users could only see and update their own recipes. I used shell scripts to run curl commands to test each of my endpoints. Most of the problems I had were copy/paste errors. I looked at the terminal session that was running my code locally to see the errors. The api code is pretty simple, so I didn't run into anything complicated.
Verb | URI Pattern | Controller#Action |
---|---|---|
GET | /recipes |
recipes#index |
GET | /recipes/:id |
recipes#show |
POST | /recipes |
recipes#create |
PATCH | /recipes/:id |
recipes#update |
DELETE | /recipes/:id |
recipes#delete |
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/recipes"
curl "${API}${URL_PATH}" \
--include \
--request GET \
--header "Authorization: Token token=$TOKEN"
TOKEN=BAhJIiU2YjQ0MDNmYTAwYjJhZWVjMTgyMmZjYTdlNTdlMGI2ZQY6BkVG--29964af2634899b453ab75d3f3e6fa080375c7c1 sh scripts/get-recipes.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"recipes": [
{
"id": 8,
"name": "Pumpkin Pie",
"serves": 4,
"category": "dessert",
"ingredients": "flour, sugar, Crisco, pumpkin, eggs, milk, spices",
"directions": "Make crust, add pumpkin mixture. Bake.",
"user_id": 1,
"editable": true
},
{
"id": 9,
"name": "Apple Cake",
"serves": 12,
"category": "Desserts",
"ingredients": null,
"directions": "Make batter, place sliced apples on batter, sprinkle topping over all, bake at 350 for 45 minutes.",
"user_id": 1,
"editable": true
},
{
"id": 7,
"name": "Dutch Apple Pie",
"serves": 8,
"category": "Desserts",
"ingredients": "flour, sugar, Crisco, apples, cinnamon, sugar",
"directions": "Make bottom crust, place apple mixture in, top with crumb mixture, bake 375 for 45 minutes",
"user_id": 1,
"editable": true
},
{
"id": 19,
"name": "Pot Roast",
"serves": 6,
"category": "Desserts",
"ingredients": null,
"directions": "Brown roast on all sides in Dutch oven, add beef broth, bring to a boil. Put lid on Dutch oven, place in oven at 325 degrees for 3 hours. ",
"user_id": 1,
"editable": true
}
]
}
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/recipes"
curl "${API}${URL_PATH}/$ID" \
--include \
--request GET \
--header "Authorization: Token token=$TOKEN"
TOKEN=BAhJIiU2YjQ0MDNmYTAwYjJhZWVjMTgyMmZjYTdlNTdlMGI2ZQY6BkVG--29964af2634899b453ab75d3f3e6fa080375c7c1 ID=1 sh scripts/get-recipe.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"recipe": {
"id": 8,
"name": "Pumpkin Pie",
"serves": 4,
"category": "dessert",
"ingredients": "flour, sugar, Crisco, pumpkin, eggs, milk, spices",
"directions": "Make crust, add pumpkin mixture. Bake.",
"user_id": 1,
"editable": true
}
}
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/recipes"
curl "${API}${URL_PATH}" \
--include \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Token token=$TOKEN" \
--data '{
"recipe": {
"name": "'"${NAME}"'",
"serves": "'"${SERVES}"'",
"category": "'"${CATEGORY}"'",
"ingredients": "'"${INGREDIENTS}"'",
"directions": "'"${DIRECTIONS}"'"
}'
TOKEN=BAhJIiU2YjQ0MDNmYTAwYjJhZWVjMTgyMmZjYTdlNTdlMGI2ZQY6BkVG--29964af2634899b453ab75d3f3e6fa080375c7c1 NAME="Test Recipe" SERVES=6 CATEGORY=Dessert INGREDIENTS="Test Ingredients" DIRECTIONS="Test Directions" sh scripts/create-recipe.sh
Response:
HTTP/1.1 201 Created
Location: http://localhost:4741/recipes/21
Content-Type: application/json; charset=utf-8
{
"recipe": {
"id": 21,
"name": "Test Recipe",
"serves": 4,
"category": "dessert",
"ingredients": "flour, sugar, Crisco, pumpkin, eggs, milk, spices",
"directions": "Make crust, add pumpkin mixture. Bake.",
"user_id": 1,
"editable": true
}
}
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/recipes"
curl "${API}${URL_PATH}/$ID" \
--include \
--request PATCH \
--header "Content-Type: application/json" \
--header "Authorization: Token token=$TOKEN" \
--data '{
"recipe": {
"name": "'"${NAME}"'",
"serves": "'"${SERVES}"'",
"category": "'"${CATEGORY}"'",
"ingredients": "'"${INGREDIENTS}"'",
"directions": "'"${DIRECTIONS}"'"
}
}'
TOKEN=BAhJIiU2YjQ0MDNmYTAwYjJhZWVjMTgyMmZjYTdlNTdlMGI2ZQY6BkVG--29964af2634899b453ab75d3f3e6fa080375c7c1 ID=21 NAME="Update Recipe" SERVES=8 CATEGORY=Dessert INGREDIENTS="Update Ingredients" DIRECTIONS="Update Directions" sh scripts/update-recipe.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"recipe": {
"id": 21,
"name": "Update Recipe",
"serves": 8,
"category": "Dessert",
"ingredients": "Update Ingredients",
"directions": "Update Directions",
"user_id": 1,
"editable": true
}
}
API="${API_ORIGIN:-http://localhost:4741}"
URL_PATH="/recipes"
curl "${API}${URL_PATH}/$ID" \
--include \
--request DELETE \
--header "Authorization: Token token=$TOKEN"
TOKEN=BAhJIiU2YjQ0MDNmYTAwYjJhZWVjMTgyMmZjYTdlNTdlMGI2ZQY6BkVG--29964af2634899b453ab75d3f3e6fa080375c7c1 ID=21 sh scripts/delete-recipe.sh
Response:
HTTP/1.1 204 No Content
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 http://localhost:4741/sign-up \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"password": "'"${PASSWORD}"'",
"password_confirmation": "'"${PASSWORD}"'"
}
}'
[email protected] PASSWORD=hannah 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 http://localhost:4741/sign-in \
--include \
--request POST \
--header "Content-Type: application/json" \
--data '{
"credentials": {
"email": "'"${EMAIL}"'",
"password": "'"${PASSWORD}"'"
}
}'
[email protected] PASSWORD=hannah scripts/sign-in.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 1,
"email": "[email protected]",
"token": "BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f"
}
}
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
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
Verb | URI Pattern | Controller#Action |
---|---|---|
GET | /users |
users#index |
GET | /users/1 |
users#show |
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": "[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=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/user.sh
Response:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
"user": {
"id": 2,
"email": "[email protected]"
}
}