Created
July 22, 2019 22:06
-
-
Save Martin-Alexander/2c3d3fb05e8334b3fa5cd19c9e67581b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'cookbook' # You need to create this file! | |
require_relative 'controller' # You need to create this file! | |
require_relative 'router' | |
csv_file = File.join(__dir__, 'recipes.csv') | |
cookbook = Cookbook.new(csv_file) | |
controller = Controller.new(cookbook) | |
router = Router.new(controller) | |
# Start the app | |
router.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "view" | |
require_relative "recipe" | |
class Controller | |
def initialize(cookbook) | |
@view = View.new | |
@cookbook = cookbook | |
end | |
def list | |
# Ask the repo for all the recipes | |
recipes = @cookbook.all | |
# Tell to the view to display them all | |
@view.display_recipes(recipes) | |
end | |
def create | |
# Tell the view to ask the user for the name and description | |
recipe_name = @view.ask_for("name") | |
recipe_description = @view.ask_for("description") | |
# Create a recipe object | |
new_recipe = Recipe.new(recipe_name, recipe_description) | |
# Give it to the repo to store it | |
@cookbook.add_recipe(new_recipe) | |
end | |
def destroy | |
list | |
# Tell the view to ask the user which recipe they want to delete | |
recipe_index = @view.ask_for_index | |
# Tell the repo to delete that recipe | |
@cookbook.remove_recipe(recipe_index) | |
end | |
def search | |
# VIEW: ask user for keyword | |
keyword = @view.ask_for("keyword") | |
recipe_results = LetscookfrenchRecipeScraper.new(keyword).fetch_results | |
# VIEW: display the results | |
@view.display_recipes(recipe_results) | |
# VIEW: ask for for which recipe they'd like | |
recipe_index = @view.ask_for_index | |
# REPO: Given the recipe add it to the cookbook | |
chosen_recipe = recipe_results[recipe_index] | |
@cookbook.add_recipe(chosen_recipe) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
require_relative "recipe" | |
class Cookbook | |
def initialize(csv_file_path) | |
@csv_file_path = csv_file_path | |
@recipes = [] | |
CSV.foreach(@csv_file_path) do |row| | |
@recipes << Recipe.new(row[0], row[1]) | |
end | |
end | |
def all | |
@recipes | |
end | |
def add_recipe(new_recipe) | |
@recipes << new_recipe | |
save_to_csv | |
end | |
def remove_recipe(recipe_index) | |
@recipes.delete_at(recipe_index) | |
save_to_csv | |
end | |
private | |
def save_to_csv | |
CSV.open(@csv_file_path, "w") do |csv_file| | |
@recipes.each do |recipe| | |
csv_file << [recipe.name, recipe.description] | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "nokogiri" | |
require "open-uri" | |
require_relative "recipe" | |
class LetscookfrenchRecipeScraper | |
def initialize(keyword) | |
@keyword = keyword | |
end | |
def fetch_results | |
# scrape a webpage and get back a list of recipes | |
doc = Nokogiri::HTML(open("http://www.letscookfrench.com/recipes/find-recipe.aspx?aqt=#{keyword}").read) | |
recipe_elements = doc.search(".recette_classique").first(5) | |
recipe_results = [] | |
recipe_elements.each do |recipe_card_element| | |
name = recipe_card_element.search(".m_titre_resultat").text.strip | |
description = recipe_card_element.search(".m_texte_resultat").text.strip | |
recipe_results << Recipe.new(name, description) | |
end | |
return recipe_results | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Recipe | |
attr_reader :name, :description | |
def initialize(name, description) | |
@name = name | |
@description = description | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cake | eggs milk and butter | |
---|---|---|
cookies | butter flour and chocolate | |
'Very strong' lemon pie | Ingredients : egg, pastry, butter, lemon, caster sugar. Preheat the oven to 220 °C (420 °F). Line out a buttered pie mold but do not prick the pastry. Press lemons and save the juice. Clean the skin of 1 lemon and chop it.... | |
pizza | tomatoes, cheese, and dough |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Router | |
def initialize(controller) | |
@controller = controller | |
@running = true | |
end | |
def run | |
puts "Welcome to the Cookbook!" | |
puts " -- " | |
while @running | |
display_tasks | |
action = gets.chomp.to_i | |
print `clear` | |
route_action(action) | |
end | |
end | |
private | |
def route_action(action) | |
case action | |
when 1 then @controller.list | |
when 2 then @controller.create | |
when 3 then @controller.destroy | |
when 4 then @controller.search | |
when 5 then stop | |
else | |
puts "Please press 1, 2, 3 or 4" | |
end | |
end | |
def stop | |
@running = false | |
end | |
def display_tasks | |
puts "" | |
puts "What do you want to do next?" | |
puts "1 - List all recipes" | |
puts "2 - Create a new recipe" | |
puts "3 - Destroy a recipe" | |
puts "4 - Search for recipes online :)" | |
puts "5 - Stop and exit the program" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View | |
def display_recipes(recipes) | |
recipes.each_with_index do |recipe, index| | |
puts "#{index + 1} - #{recipe.name}: #{recipe.description}" | |
end | |
end | |
def ask_for(prompt_message) | |
puts prompt_message | |
user_input = gets.chomp | |
return user_input | |
end | |
def ask_for_index | |
puts "Index:" | |
recipe_index = gets.chomp.to_i - 1 | |
return recipe_index | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment