diff --git a/Gemfile b/Gemfile index c004f4ca..840a55ab 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source "https://rubygems.org" gem "sinatra-activerecord" -gem "sqlite3" +gem "sqlite3", "~> 1.4.0" gem "pry" gem "require_all" +gem "rest-client" diff --git a/Gemfile.lock b/Gemfile.lock index 9589226d..0d650116 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,47 +1,63 @@ GEM remote: https://rubygems.org/ specs: - activemodel (6.0.3.1) - activesupport (= 6.0.3.1) - activerecord (6.0.3.1) - activemodel (= 6.0.3.1) - activesupport (= 6.0.3.1) - activesupport (6.0.3.1) + activemodel (6.1.1) + activesupport (= 6.1.1) + activerecord (6.1.1) + activemodel (= 6.1.1) + activesupport (= 6.1.1) + activesupport (6.1.1) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - zeitwerk (~> 2.2, >= 2.2.2) - coderay (1.1.1) - concurrent-ruby (1.1.6) - i18n (1.8.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + zeitwerk (~> 2.3) + coderay (1.1.3) + concurrent-ruby (1.1.8) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + http-accept (1.7.0) + http-cookie (1.0.3) + domain_name (~> 0.5) + i18n (1.8.7) concurrent-ruby (~> 1.0) - method_source (0.8.2) - minitest (5.14.1) - mustermann (1.0.3) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (1.0.0) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2020.1104) + minitest (5.14.3) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + netrc (0.11.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rack (2.2.3) - rack-protection (2.0.7) + rack-protection (2.1.0) rack - require_all (1.3.3) - sinatra (2.0.7) + require_all (3.0.0) + rest-client (2.1.0) + http-accept (>= 1.7.0, < 2.0) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) + ruby2_keywords (0.0.4) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.12) - activerecord (>= 3.2) + sinatra-activerecord (2.0.22) + activerecord (>= 4.1) sinatra (>= 1.0) - slop (3.6.0) - sqlite3 (1.3.13) - thread_safe (0.3.6) + sqlite3 (1.4.2) tilt (2.0.10) - tzinfo (1.2.7) - thread_safe (~> 0.1) - zeitwerk (2.3.0) + tzinfo (2.0.4) + concurrent-ruby (~> 1.0) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.7) + zeitwerk (2.4.2) PLATFORMS ruby @@ -49,8 +65,9 @@ PLATFORMS DEPENDENCIES pry require_all + rest-client sinatra-activerecord - sqlite3 + sqlite3 (~> 1.4.0) BUNDLED WITH - 1.14.6 + 2.2.3 diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb new file mode 100644 index 00000000..cf3ebb29 --- /dev/null +++ b/app/models/ingredient.rb @@ -0,0 +1,12 @@ +class Ingredient < ActiveRecord::Base + has_many :recipes + + def self.add__or_create_ingredient(ingredient) + if Ingredient.all.find{|i| i.name == ingredient} + else + Ingredient.create(name: ingredient) + end + Ingredient.all.find{|i| i.name == ingredient} + end + +end diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb new file mode 100644 index 00000000..a9699e5a --- /dev/null +++ b/app/models/ingredient_recipe.rb @@ -0,0 +1,12 @@ +class IngredientRecipe < ActiveRecord::Base + belongs_to :ingredient + belongs_to :recipe + + def self.find_or_create_ingredient_recipe(recipe_id, ingredient_id) + if IngredientRecipe.all.find {|ir| ir.recipe_id == recipe_id && ir.ingredient_id == ingredient_id} + else + IngredientRecipe.create(recipe_id: recipe_id, ingredient_id: ingredient_id) + end + IngredientRecipe.all.find {|ir| ir.recipe_id == recipe_id && ir.ingredient_id == ingredient_id} + end +end \ No newline at end of file diff --git a/app/models/recipe.rb b/app/models/recipe.rb new file mode 100644 index 00000000..fd781410 --- /dev/null +++ b/app/models/recipe.rb @@ -0,0 +1,13 @@ +class Recipe < ActiveRecord::Base + has_many :ingredients, through: :ingredient_recipes + has_many :users, through: :user_recipes + + def self.find_or_create_recipe(name, instructions) + if Recipe.all.find{|r| r.name == name} + else + Recipe.create(name: name, instructions: instructions) + end + Recipe.all.find{|r| r.name == name} + end +end + diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..f0f2f0ae --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,5 @@ +class User < ActiveRecord::Base + has_many :recipes + has_many :ingredients, through: :recipes + +end \ No newline at end of file diff --git a/app/models/user_recipe.rb b/app/models/user_recipe.rb new file mode 100644 index 00000000..2c7fb574 --- /dev/null +++ b/app/models/user_recipe.rb @@ -0,0 +1,4 @@ +class UserRecipe < ActiveRecord::Base + belongs_to :user + belongs_to :recipe +end \ No newline at end of file diff --git a/bin/api_parsing.rb b/bin/api_parsing.rb new file mode 100644 index 00000000..a5bbe92c --- /dev/null +++ b/bin/api_parsing.rb @@ -0,0 +1,38 @@ +require_relative '../config/environment' +require 'rest-client' +require 'json' +require 'pry' + + +def get_data(name) + + + url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" + response = RestClient.get(url) + result = JSON.parse(response) + + while result["drinks"] == nil + puts "Drink not found. Please input another recipe name." + name = gets.chomp + url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" + response = RestClient.get(url) + result = JSON.parse(response) + end + + drink_name = result["drinks"][0]["strDrink"] + drink_instructions = result["drinks"][0]["strInstructions"] + current_ingr_num = 1 + current_recipe = Recipe.find_or_create_recipe(drink_name, drink_instructions) + + while result["drinks"][0]["strIngredient#{current_ingr_num}"] + current_ingredient = Ingredient.add__or_create_ingredient(result["drinks"][0]["strIngredient#{current_ingr_num}"]) + IngredientRecipe.find_or_create_ingredient_recipe(current_recipe.id, current_ingredient.id) + current_ingr_num += 1 + end + + + + + +end + diff --git a/bin/run.rb b/bin/run.rb old mode 100644 new mode 100755 index cf08c338..7d40ded8 --- a/bin/run.rb +++ b/bin/run.rb @@ -1,5 +1,36 @@ require_relative '../config/environment' +require_relative 'api_parsing' + +puts "Welcome to the Cocktail Recipe Interface" +puts "Please enter your username" +user_name = gets.chomp #call find_or_create_by_name method in user.rb +puts "What would you like to do?" +puts "1. Find a random recipe by ingredient" + #call suggest_random_recipe using find_recipe_by_ingredient method and then + #picking a random array element in recipe.rb + #be sure to prompt_user_for_rating +puts "2. Show my favorite recipes" + #traverse through user_recipe table to find highest ratings and return as an array +puts "3. Suggest a new recipe" + #traverse through recipes and spit out random one + #be sure to prompt_user_for_rating +puts "4. Look up a recipe by recipe name" -puts "HELLO WORLD" + #call find_recipe_by_name +puts "5. List all recipes with a certain ingredient" + #call find_recipe_by_ingredient method and return an array of choices +puts "6. Import a recipe from the database" +choice = gets.chomp +choice = choice.to_i +if choice == 6 + puts "Enter the recipe name you are looking for" + name = gets.chomp + get_data(name) +end + #if choice == 1 do blah blah blah etc. + +#make method prompt_user_for_rating which will ask for a rating +#then store it with the recipe in the user_recipe table +#make method update_rating ? diff --git a/config/environment.rb b/config/environment.rb index 4dbe13e5..2a7073f6 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -2,4 +2,4 @@ Bundler.require ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') -require_all 'lib' +require_all 'app' diff --git a/db/migrate/001_create_recipes.rb b/db/migrate/001_create_recipes.rb new file mode 100644 index 00000000..7ac876a0 --- /dev/null +++ b/db/migrate/001_create_recipes.rb @@ -0,0 +1,8 @@ +class CreateRecipes < ActiveRecord::Migration[5.1] + def change + create_table :recipes do |t| + t.string :name + t.text :instructions + end + end +end \ No newline at end of file diff --git a/db/migrate/002_create_ingredients.rb b/db/migrate/002_create_ingredients.rb new file mode 100644 index 00000000..8b71cef1 --- /dev/null +++ b/db/migrate/002_create_ingredients.rb @@ -0,0 +1,8 @@ +class CreateIngredients < ActiveRecord::Migration[5.1] + def change + create_table :ingredients do |t| + t.string :name + t.string :type + end + end +end \ No newline at end of file diff --git a/db/migrate/003_create_users.rb b/db/migrate/003_create_users.rb new file mode 100644 index 00000000..027c8b61 --- /dev/null +++ b/db/migrate/003_create_users.rb @@ -0,0 +1,7 @@ +class CreateUsers < ActiveRecord::Migration[5.1] + def change + create_table :users do |t| + t.string :name + end + end +end \ No newline at end of file diff --git a/db/migrate/004_create_ingredient_recipes.rb b/db/migrate/004_create_ingredient_recipes.rb new file mode 100644 index 00000000..c00ea099 --- /dev/null +++ b/db/migrate/004_create_ingredient_recipes.rb @@ -0,0 +1,8 @@ +class CreateIngredientRecipes < ActiveRecord::Migration[5.1] + def change + create_table :ingredient_recipes do |t| + t.integer :ingredient_id + t.integer :recipe_id + end + end +end \ No newline at end of file diff --git a/db/migrate/005_create_user_recipes.rb b/db/migrate/005_create_user_recipes.rb new file mode 100644 index 00000000..7254b9c5 --- /dev/null +++ b/db/migrate/005_create_user_recipes.rb @@ -0,0 +1,9 @@ +class CreateUserRecipes < ActiveRecord::Migration[5.1] + def change + create_table :user_recipes do |t| + t.integer :user_id + t.integer :recipe_id + t.integer :rating + end + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..edd8452b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,40 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 5) do + + create_table "ingredient_recipes", force: :cascade do |t| + t.integer "ingredient_id" + t.integer "recipe_id" + end + + create_table "ingredients", force: :cascade do |t| + t.string "name" + t.string "type" + end + + create_table "recipes", force: :cascade do |t| + t.string "name" + t.text "instructions" + end + + create_table "user_recipes", force: :cascade do |t| + t.integer "user_id" + t.integer "recipe_id" + t.integer "rating" + end + + create_table "users", force: :cascade do |t| + t.string "name" + end + +end