API: Active Routes - simply-nourish/nourish-app GitHub Wiki

API: Active Endpoints

Recipes

GET /users/:id/recipes
GET /recipes/:id
GET /recipes --> gets all recipes
POST /users/:id/recipes
PUT /recipes/:id
DELETE /recipes/:id

JSON-formatted responses to GET requests are as follows. Note that multiple recipes (GET /recipes, for instance) will be returned in an array:

{    
  "id" : "(id)",  
  "title" : "(title)",       
  "summary" : "(summary)",     
  "instructions" : "(instructions)",     
  "ingredient_recipes" : [ 
              { "id" : "(id)",
                "measure" : "(measure)", 
                "amount" : "(amount)", 
                "ingredient" : { "id" : "(id)", "name" : "(name") }  
              }, ... ],
  "dietary_restriction_recipes: " : [ { "id" : "(id)", 
                                        "recipe_id" : "(recipe_id)", 
                                        "dietary_restriction": {"id": "(id)", "name": "(name)" } }... ]
  "user" : { "name" : "(name)" }    
}

POST requests should be formatted in JSON as follows:

{    
  "recipe" : { 
    "title" : "(title)",       
    "summary" : "(summary)",     
    "instructions" : "(instructions)",     
    "ingredient_recipes_attributes" : [ 
                                          { 
                                            "measure_id" : "(measure_id)", 
                                            "amount" : "(amount)", 
                                            "ingredient_id": "(ingredient_id)" 
                                          }, ... 
                                       ],
    "dietary_restriction_recipes_attributes" : [ 
                                                   {"dietary_restriction_id" : "(id_1)"}, 
                                                   {"dietary_restriction_id" : "(id_2)"}, ... 
                                               ]
}

PUT requests must also be nested under a "recipe" key, but do not require all fields to be present

To delete a nested resource (i.e., a join table reference), pass the ID of the row of the join table (id), and set "_destroy" to 1. For instance, to delete a particular ingredient in a recipe:

{    
  "recipe" : {    
    "ingredient_recipes_attributes" : [ { "id": "(id)", "_destroy": "1" } ]
  }
}

Users

GET /users
GET /users/:id

Auth (see the devise_token_auth docs)

POST /auth (user account creation)
DELETE /auth (user account deletion)
PUT /auth (user account modify)

POST auth/sign_in (user sign-in)
DELETE auth/sign_out (user sign-out)

Example account creation with JSON:

(POST /auth)
{
  "nickname" : "(nickname)",  
  "first_name" : "(first_name)",       
  "last_name" : "(last_name)",  
  "default_servings" : "(default_servings)",  
  "image" : "(image)",  
  "email" : "(email)",
  "password" : "(password)",
  "password_confirmation" : "(password)",
  "confirm_success_url" : "(url)"    
}

Note that a successful account creation / sign-in will return the following fields in the response header:

access-token
client
uid

You'll need to save these and include them in subsequent requests to remain authenticated.

Meal Plans

GET /users/:id/meal_plans
GET /meal_plans/:id
POST users/:id/meal_plans/
PUT /meal_plans/:id
DELETE /meal_plans/:id

JSON-formatted responses to GET requests are as follows. Note that multiple meal plans (e.g., from GET /users/:id/meal_plans) will be returned in an array:

{    
  "id" : "(id)",  
  "name" : "(name)",         
  "meal_plan_recipes" : [ 
              { "id": "(id)",
                "recipe" : { "id": "(id)", "title" : "(title)" },
                "day" : "(day)", 
                "meal" : "(meal)"
              }, ... ],
  "user" : { "name" : "(name)" }    
}

POST requests should be formatted in JSON as follows:

{    
  "meal_plan" : { 
    "name" : "(name)",       
    "meal_plan_recipes_attributes" : [ 
                                          { 
                                            "day" : "(day)", 
                                            "meal" : "(meal)", 
                                            "recipe_id": "(recipe_id)" 
                                          }, ... 
                                      ]
}

Appropriate parameters for "day" are:

  • monday, tuesday, wednesday, thursday, friday, saturday, sunday

Appropriate parameters for "meal" are:

  • breakfast, lunch, dinner, snack

To delete a nested resource (i.e., a join table reference), pass the ID of the row of the join table (id), and set "_destroy" to 1. For instance, to delete a particular ingredient in a recipe:

{    
  "meal_plan" : {    
    "meal_plan_recipes_attributes" : [ { "id": "(id)", "_destroy": "1" } ]
  }
}

To PUT a new meal_plan_recipe, format similarly to above, but pass all parameters EXCEPT for id.

Shopping Lists

GET /users/:id/shopping_lists
POST /users/:id/shopping_lists
PUT /shopping_lists/:id
DELETE /shopping_lists/:id

JSON format of data returned from the server (say, from a GET request):

{
"id" : "(id)", 
"name" : "(shopping list nme)",  
"meal_plan" : {"id": "(id)", "name": "(meal plan name)"},  
"user": {"nickname": "(nickname)" },  
"ingredient_shopping_lists" : [ { 
           "id": "(id)" 
           "amount" : "(amount)",   
           "purchased" : "(true or false)",   
           "ingredient" : {"id" : "(id)", "name" : "(ingredient name)"  },   
           "measure" : {"id": "(id)",  "name" : "(measure name)"}    
            }, ... ]  
}

JSON format of data for POST-ing to the server:

{
  "shopping_list" : {
         "name" : "(shopping list name)",
         "meal_plan_id" : "(id)"  } 
}

Note that shopping lists are created from meal plans, but can be amended in PUT requests.

To send a PUT request, format JSON like so (you can pick and choose from the following):

{ 
   shopping_list:  { 
       "name" : "(updated name)",
       "ingredient_shopping_lists_attributes" : [ {
                          "id": "(id)"
                          "ingredient_id" : "(measure id)", 
                          "measure_id" : "(measure id)", 
                          "amount" : "(amount)",
                          "purchased" : (true or false),
                          "_destroy" : (true or false) }, ... ]  
    }
 } 

Note that if you want to delete the nested ingredient_shopping_lists_attributes, you just need to pass the ID of the ingredient_shopping_list (id), and set _destroy to a truthy value (1 or true).

If you do not wish to delete the nested resource, ignore the _destroy parameter.

If you wish to create a new nested resource, ignore the id parameter (pass the attributes you want to set, and the attribute will be created).

Ingredients and Ingredient-Categories

GET /ingredients <-- get all ingredients

GET /ingredient_categories/:ingredient_category_id/ingredients
POST /ingredient_categories/:ingredient_category_id/ingredients

GET /ingredient_categories
POST /ingredient_categories

GET /ingredient_categories/:id
PUT /ingredient_categories/:id
DELETE /ingredient_categories/:id

GET /ingredients/:id
PUT /ingredients/:id
DELETE /ingredients/:id

Dietary Restrictions

GET /dietary_restrictions
POST /dietary_restrictions
GET /dietary_restrictions/:id
PUT /dietary_restrictions/:id
DELETE /dietary_restrictions/:id

Measures

GET /measures
GET /measures/:id
POST /measures
PUT /measures/:id
DELETE /measures/:id