Created
January 19, 2015 18:18
-
-
Save ssaunier/9d507cb80c00944513e0 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 'sinatra' | |
require 'sinatra/reloader' | |
require_relative "cookbook" | |
filename = "recipes.csv" | |
cookbook = Cookbook.new(filename) | |
get '/' do | |
@recipes = cookbook.all | |
erb :home | |
end | |
get '/:index' do | |
index = params[:index].to_i | |
@recipe = cookbook.find(index) | |
erb :recipe | |
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_filename) | |
@recipes = [] # Instances of `Recipe` | |
@csv_filename = csv_filename | |
load_from_csv | |
end | |
def find(index) | |
return @recipes[index] | |
end | |
def add(recipe) | |
@recipes << recipe | |
save | |
end | |
def all | |
return @recipes | |
end | |
def destroy(index) | |
@recipes.delete_at(index) | |
save | |
end | |
def save | |
CSV.open(@csv_filename, 'w') do |csv| | |
@recipes.each do |recipe| | |
csv << [ recipe.name, recipe.description ] | |
end | |
end | |
end | |
private | |
# Store recipes as: | |
# name, description | |
def load_from_csv | |
if File.exist?(@csv_filename) | |
CSV.foreach(@csv_filename) do |row| | |
name = row[0] | |
description = row[1] | |
@recipes << Recipe.new(name, 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_relative "router" | |
require_relative "cookbook" | |
filename = "recipes.csv" | |
cookbook = Cookbook.new(filename) | |
router = Router.new(cookbook) | |
router.start |
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 MarmitonService | |
def fetch_recipes(ingredient) | |
results = [] | |
doc = marmiton_doc(ingredient) | |
doc.css('.m_contenu_resultat').each do |element| | |
results << parse_recipe(element) | |
end | |
return results | |
end | |
private | |
def marmiton_doc(ingredient) | |
url_marmiton = "http://www.marmiton.org/recettes/recherche.aspx?aqt=#{ingredient}" | |
doc = Nokogiri::HTML(open(url_marmiton), nil, 'utf-8') | |
return doc | |
end | |
def parse_recipe(html_element) | |
name = html_element.search('.m_titre_resultat > a').inner_text | |
# "Rating : #{html_element.search('.m_recette_note1').size} / 5" | |
description = html_element.search('.m_texte_resultat').inner_text | |
Recipe.new(name, 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
class Recipe | |
attr_reader :name | |
attr_accessor :description # attr_reader + attr_writer | |
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
Tiramisu | Italien | |
---|---|---|
Tarte au choco | chocolqt | |
Risotto | owiehrw kejrnw ekjrb | |
Trifle aux fraises | ||
Carpaccio de fraises | Ingrédients : - 1 kg de fraises gariguette - 250 g de pain d'épices Carrefour Sélection - 4 boules de crème glacée à la vanille Carrefour - 10 cl de crème de vinaigre balsamique. A l'aide d'un couteau, détailler les... |
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 "recipe" | |
require_relative "view" | |
require_relative "marmiton_service" | |
class RecipesController | |
def initialize(cookbook) | |
@cookbook = cookbook | |
@view = View.new | |
end | |
def create | |
# View => demande nom, description | |
name = @view.ask_for_name | |
description = @view.ask_for_description | |
# Recipe => creer une instance | |
recipe = Recipe.new(name, description) | |
# Cookbook => stocke | |
@cookbook.add(recipe) | |
end | |
def list | |
# Cookbook => donne moi les recettes | |
recipes = @cookbook.all | |
# View => affiche les recettes | |
@view.print_recipes(recipes) | |
end | |
def destroy | |
# View => donne moi l'id de la recette a supprimer | |
index = @view.ask_for_recipe_id | |
# Cookbook => degage la | |
@cookbook.destroy(index) | |
end | |
def edit | |
# View => donne moi l'id de la recette a editer | |
index = @view.ask_for_recipe_id | |
# View => demande nouvelle description | |
new_description = @view.ask_for_description | |
# Cookbook => demande la recette | |
recipe = @cookbook.find(index) | |
# met a jour la description de la recette | |
recipe.description = new_description | |
@cookbook.save | |
end | |
def import | |
ingredient = @view.ask_for_ingredient | |
recipes = MarmitonService.new.fetch_recipes(ingredient) # [ Recipe, Recipe, Recipe] | |
@view.print_recipes(recipes) | |
index = @view.ask_for_recipe_id | |
@cookbook.add(recipes[index]) | |
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 "colorize" | |
require_relative "recipes_controller" | |
class Router | |
def initialize(cookbook) | |
@recipes_controller = RecipesController.new(cookbook) | |
end | |
def start | |
loop do | |
print `clear` | |
@recipes_controller.list | |
print_menu | |
action = gets.chomp.to_i | |
dispatch(action) | |
end | |
end | |
def print_menu | |
puts "------------------" | |
puts "1 - Add a recipe".colorize(:green) | |
puts "2 - Remove recipes".colorize(:green) | |
puts "3 - Edit a recipe".colorize(:green) | |
puts "4 - Import from Marmiton".colorize(:orange) | |
puts "------------------" | |
print "> " | |
end | |
def dispatch(action) | |
case action | |
when 1 then @recipes_controller.create | |
when 2 then @recipes_controller.destroy | |
when 3 then @recipes_controller.edit | |
when 4 then @recipes_controller.import | |
else puts "Try again." | |
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 'csv' | |
filename = "recipes.csv" | |
# CSV.open(filename, 'w') do |csv| | |
# csv << [ "Tarte au chocolat", "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ea quis quibusdam recusandae amet ipsa dicta fuga ipsum ab odio optio adipisci voluptas dolores, temporibus deserunt, nam alias consequuntur, soluta esse!"] | |
# csv << [ "Tarte aux pommes", "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laudantium dolorum, temporibus optio odio aspernatur repellendus id cumque alias quibusdam impedit eligendi sunt delectus numquam ea omnis, nemo deserunt ab expedita?"] | |
# end | |
require_relative 'recipe' | |
require_relative 'cookbook' | |
recipe1 = Recipe.new("Tarte au chocolat", "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id obcaecati aperiam modi, libero at facere minima sit quae quos dignissimos itaque nulla cupiditate accusantium quaerat, a reprehenderit dolore repudiandae? Quam!") | |
recipe2 = Recipe.new("Tarte aux pommes", "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt, ad. Sapiente laborum suscipit voluptate voluptates, sit nemo nostrum dolorum optio corrupti quos repellat recusandae qui beatae saepe id facere. Soluta!") | |
cookbook = Cookbook.new(filename) | |
cookbook.add(recipe1) | |
cookbook.add(recipe2) |
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
filename = "recipes.csv" | |
require_relative "cookbook" | |
cookbook = Cookbook.new(filename) | |
puts "Il y a #{cookbook.all.count} recettes" |
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 "colorize" | |
class View | |
def ask_for_name | |
print "Recipe name? > " | |
gets.chomp | |
end | |
def ask_for_description | |
print "description? > " | |
gets.chomp | |
end | |
def print_recipes(recipes) | |
recipes.each_with_index do |recipe, index| | |
print "#{index + 1} - " | |
puts recipe.name.colorize(:blue) | |
puts "#{recipe.description}" | |
puts "--" | |
end | |
end | |
def ask_for_recipe_id | |
print "Select a recipe > " | |
gets.chomp.to_i - 1 | |
end | |
def ask_for_ingredient | |
puts "what ingredient do you want to use?" | |
gets.chomp | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment