Skip to content

Commit 427bfcb

Browse files
committed
sinatra v2
1 parent 95f10db commit 427bfcb

10 files changed

+191
-1
lines changed

app.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1+
require_relative 'cookbook'
2+
require_relative 'recipe'
13
require "sinatra"
24
require "sinatra/reloader" if development?
35
require "pry-byebug"
46
require "better_errors"
7+
set :bind, '0.0.0.0'
58
configure :development do
69
use BetterErrors::Middleware
710
BetterErrors.application_root = File.expand_path('..', __FILE__)
811
end
912

13+
1014
get '/' do
11-
'Hello all the world!'
15+
@cookbook = Cookbook.new('recipes.csv').all
16+
erb :index
17+
end
18+
19+
get '/new' do
20+
erb :new
21+
end
22+
23+
post '/recipes' do
24+
@new_recipe = Recipe.new(
25+
{ name: params["name"],
26+
rating: params["rating"],
27+
prep_time: params["time"],
28+
description: params["description"],
29+
done: false }
30+
)
31+
@updated = Cookbook.new('recipes.csv')
32+
@updated.add_recipe(@new_recipe)
33+
@cookbook = Cookbook.new('recipes.csv').all
34+
erb :index
1235
end
36+
37+
38+
get '/about' do
39+
erb :about
40+
end
41+

cookbook.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'csv'
2+
require_relative "recipe"
3+
4+
class Cookbook
5+
def initialize(csv_file_path)
6+
@csv_file_path = csv_file_path
7+
@recipes = []
8+
@recipes = loading_csv
9+
end
10+
11+
def all
12+
@recipes
13+
end
14+
15+
def add_recipe(recipe)
16+
@recipes << recipe
17+
writing_csv
18+
end
19+
20+
def remove_recipe(recipe_index)
21+
@recipes.delete_at(recipe_index)
22+
writing_csv
23+
end
24+
25+
def mark_as_done(recipe_index)
26+
@recipes[recipe_index].done = true
27+
writing_csv
28+
@recipes[recipe_index].name
29+
end
30+
31+
def loading_csv
32+
# no headers headers: :first_row
33+
# csv_options = { col_sep: ',', quote_char: '"' }
34+
CSV.foreach(@csv_file_path) do |row|
35+
# recipe_done = row[3] == "true" ? true : false
36+
@recipes << Recipe.new(
37+
{ name: row[0],
38+
description: row[1],
39+
rating: row[2],
40+
done: (row[3] == "true"),
41+
prep_time: row[4] }
42+
)
43+
end
44+
@recipes
45+
end
46+
47+
def writing_csv
48+
# no headers headers: :first_row
49+
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' }
50+
CSV.open(@csv_file_path, 'wb', csv_options) do |csv|
51+
@recipes.each do |v|
52+
csv << [v.name, v.description, v.rating, v.done, v.prep_time]
53+
end
54+
end
55+
end
56+
end

public/application.css

Whitespace-only changes.

public/application.js

Whitespace-only changes.

recipe.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Recipe
2+
attr_accessor :name, :description, :rating, :done, :prep_time
3+
4+
def initialize(arg = {})
5+
@name = arg[:name]
6+
@description = arg[:description]
7+
@rating = arg[:rating]
8+
@done = arg[:done]
9+
@prep_time = arg[:prep_time]
10+
end
11+
end

recipes.csv

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"Tiramisu aux fraises","Ingrédients : fraises,citron jaune,oeuf,mascarpone,sucre,biscuit à la cuillère.Préparer 2 h à l'avance....","4","false","0"
2+
"Tiramisu aux fraises","Ingrédients : fraises,citron jaune,oeuf,mascarpone,sucre,biscuit à la cuillère.Préparer 2 h à l'avance....","4","false","20"
3+
"Bettes façon Cloclo","Ingrédients : bette à carde , côtes et feuilles,échalote,tomates pelées,beurre,vin rosé ou blanc,bouillon de poule,curry fort,crème à sauce,poivre,sel.Hacher grossièrement les bettes, feuilles et côtes. Faire...","4","true","40"
4+
"test","/ploof","3","false","73"
5+
"Flan coco","Ingrédients : oeuf,noix de coco,lait concentré sucré,lait écrémé,caramel.Séparer les jaunes des blancs d'oeufs. Verser le lait concentré sucré avec la même dose de lait écrémé. Ajouter la noix de coco et...","4","false","60"
6+
"Sorbet fraise-basilic par Laurent Mariotte","Ingrédients : fraises parfumées (ciflorette, charlotte…),basilic frais,sucre de canne,jus de citron.La veille, préparez un sirop : portez le sucre et 10 cl d’eau à ébullition avec...","0","true","15"
7+
"Cake","oh my cake","3","false","20"

views/about.erb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h1>The team</h1>
2+
3+
<ul>
4+
<li>My buddy</li>
5+
<li>Me</li>
6+
</ul>
7+
8+

views/index.erb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<<div>
2+
<% @cookbook.each_with_index do |v, i| %>
3+
<li>
4+
<div>
5+
<%="#{i + 1}. [#{v.done ? 'X' : ' '}] #{v.name} (#{v.rating}/5) - #{v.prep_time} min \n #{v.description}"%>
6+
</div>
7+
</li>
8+
<% end %>
9+
</div>
10+
<div>
11+
<a href="/new">new</a>
12+
</div>
13+

views/layout.erb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
8+
<title>Cookbook Sinatra</title>
9+
10+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
11+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
12+
<link rel="stylesheet" href="/application.css">
13+
</head>
14+
<body>
15+
16+
<div class="container">
17+
<div class="row">
18+
<div class="col-sm-8 col-sm-offset-2">
19+
<%= yield %>
20+
</div>
21+
</div>
22+
</div>
23+
24+
25+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
26+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
27+
<script src="/application.js"></script>
28+
</body>
29+
</html>

views/new.erb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
<form action='/recipes' method="post">
3+
<div>
4+
<label for="box1">Nom de la recette :</label>
5+
<input type="text" id="box1" placeholder="Cake,..." name="name">
6+
</div>
7+
<br>
8+
<div>
9+
<label for="box2">Note de la recette (de 0 à 5) : </label>
10+
<div>
11+
<input type="range" min="0" max="5" step="1" list="marks" placeholder="./5" id="box2" name="rating">
12+
<datalist id="marks">
13+
<option value="0" label="0">
14+
<option value="1" label="1">
15+
<option value="2" label="2">
16+
<option value="3" label="3">
17+
<option value="4" label="4">
18+
<option value="5" label="5">
19+
</datalist>
20+
</div>
21+
</div>
22+
<br>
23+
<div>
24+
<label for="box3">Temps de préparation de la recette (en min) : </label>
25+
<input type="number" min="0" max="500" step= "5" id="time" name="time">
26+
</div>
27+
<br>
28+
<div>
29+
<label for="box4">Temps de préparation de la recette : </label>
30+
<textarea id="box4" name="description" placeholder="description de la recette"></textarea>
31+
</div>
32+
<div class="button">
33+
<button type="submit">Envoyer la recette</button>
34+
</div>
35+
</form>
36+
<br>
37+
<a href="/">Retour à la liste</a>

0 commit comments

Comments
 (0)