Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
87 changes: 52 additions & 35 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,56 +1,73 @@
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

DEPENDENCIES
pry
require_all
rest-client
sinatra-activerecord
sqlite3
sqlite3 (~> 1.4.0)

BUNDLED WITH
1.14.6
2.2.3
13 changes: 13 additions & 0 deletions app/models/ingredient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes
has_many :recipes, through: :ingredient_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
43 changes: 43 additions & 0 deletions app/models/ingredient_recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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

def self.find_recipe_by_ingredient(ingredient)

while !Ingredient.all.find {|i| i.name == ingredient}
puts "Ingredient not found. Please enter a valid ingredient"
ingredient = gets.chomp
end

ingredient_id = Ingredient.all.find {|i| i.name == ingredient}.id

ing_rec_instance = IngredientRecipe.all.select {|ir| ir.ingredient_id == ingredient_id}
rec_ids = ing_rec_instance.map{|i| i.recipe_id}
recs = Recipe.all.select {|r| rec_ids.any?(r.id)}
end

def self.print_recipes(recipe)
puts "The recipes containing that ingredient are:"
recipe.each do |r|
puts r.name
end
end

def self.random_recipe_from_ingredient(ingredient)
recipe_array = IngredientRecipe.find_recipe_by_ingredient(ingredient)
suggested_recipe = recipe_array[rand(recipe_array.size - 1)]
puts "Time to make a(n) #{suggested_recipe.name}!"
puts "Here are the ingredients:"
Recipe.list_my_ingredients(suggested_recipe)
puts suggested_recipe.instructions
suggested_recipe
end
end
31 changes: 31 additions & 0 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
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

def self.suggest_random_recipe(recipe_ids_already_made)
all_rec = Recipe.all
new_recipes = []
all_rec.each do |r|
if !recipe_ids_already_made.any? {|e| e == r.id}
new_recipes.push(r)
end
end
new_recipes[rand(new_recipes.size - 1)]
end

def self.list_my_ingredients(recipe)
recipe.ingredients.each {|i| puts i.name}
end


end

81 changes: 81 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'pry'

class User < ActiveRecord::Base
has_many :user_recipes
has_many :ingredients, through: :recipes
has_many :recipes, through: :user_recipes

def self.find_or_create_by_name(name)
if User.all.find {|user| user.name == name}
else
User.create name: name
end
User.all.find {|user| user.name == name}
end

def rate_recipe(recipe)
puts "Would you like to rate this recipe? (y/n)"
input = gets.chomp
if input == "y"
puts "How would you rate this recipe? (1-5)"
rating = gets.chomp
rating = rating.to_i
while rating < 1 || rating > 5
puts "Rating needs to be from 1-5, please try again."
puts "How would you rate this recipe? (1-5)"
rating = gets.chomp
rating = rating.to_i
end
if UserRecipe.all.find{|i| i.user_id == self.id && i.recipe_id == recipe.id}
puts "You have already rated this recipe!"
else
UserRecipe.create(user_id: self.id, recipe_id: recipe.id, rating: rating)
end
UserRecipe.all.select{|i| i.user_id == self.id && i.recipe_id == recipe.id}
else
puts "Ok, maybe next time. "
end
end

def show_highest_ratings
current_users_ratings = UserRecipe.all.select {|i| i.user_id == self.id}
ratings_over_4 = current_users_ratings.select {|i| i.rating >= 4}

if ratings_over_4
ratings_over_4.each do |i|
drink = Recipe.all.find {|recipe| recipe.id == i.recipe_id}
puts "#{drink.name}: #{i.rating} stars"
end
else
puts "You don't have any favorite recipes!"
end
end

def list_my_recipe_ids
user_rec_array = UserRecipe.all.select {|i| i.user_id == self.id}
user_rec_array.map{|r| r.recipe_id}
end


def find_recipe_by_name(name)
rec_name = Recipe.all.find{|r| r.name == name}

if !rec_name
name = get_data(name)
rec_name = Recipe.all.find{|r| r.name == name}
end
# while !rec_name
# puts "Recipe not found. Please try another."
# input_value = gets.chomp
# rec_name = Recipe.all.find{|r| r.name == input_value}
# if !rec_name
# get_data(input_value)
# end
# end
puts "Here is your #{name} recipe."
Recipe.list_my_ingredients(rec_name)
puts rec_name.instructions
self.rate_recipe(rec_name)
end

end
6 changes: 6 additions & 0 deletions app/models/user_recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UserRecipe < ActiveRecord::Base
belongs_to :user
belongs_to :recipe


end
36 changes: 36 additions & 0 deletions bin/api_parsing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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

drink_name


end

Loading