-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.rb
91 lines (73 loc) · 1.75 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- encoding : utf-8 -*-
require 'sinatra'
require 'csv'
set :csvfile, 'fakedata.csv'
before do
@contacts = []
CSV.parse(File.read(settings.csvfile), headers: :first_row).each do |row|
@contacts << row.to_hash
end
end
helpers do
def get_and_show_id(arr)
id = arr.shift
sprintf('<td><a href="/contacts/%d">%d</a></td>', id, id)
end
def save_csv(contacts)
CSV.open(settings.csvfile ,'wb') do |csv|
csv << contacts.first.keys
contacts.each do |h|
csv << h.values
end
end
end
end
get '/' do
redirect '/contacts'
end
get '/contacts' do
@headers = @contacts.first.keys
if params[:name_like]
@contacts = @contacts.select{|contact| contact["name"].include?(params[:name_like]) }
end
erb :index
end
post '/contacts' do
params[:contact]["created_on"] = Time.now.strftime('%Y/%m/%d')
@new_contact = {}
@contacts.first.keys.each do |k|
@new_contact[k] = params[:contact][k]
end
@new_contact["id"] = @contacts.max{|a,b| a["id"].to_i <=> b["id"].to_i }["id"].to_i + 1
@contacts << @new_contact
save_csv @contacts
redirect '/contacts'
end
get '/contacts/new' do
@contact = {}
@action = '/contacts'
erb :form
end
before %r{\/contacts\/(\d+).*} do
@contact = @contacts.select{|contact| contact["id"] == params[:captures].first}
not_found if @contact.empty?
@contact = @contact.first
end
put '/contacts/:id' do
@contacts[params[:id].to_i - 1].merge! params[:contact]
save_csv(@contacts)
redirect '/contacts'
end
get '/contacts/:id/edit' do
@action = "/contacts/#{@contact['id']}"
@method = :put
erb :form
end
get '/contacts/:id' do
erb :show
end
delete '/contacts/:id' do
@contacts.delete_if{|c| c["id"] == params[:id] }
save_csv @contacts
redirect '/contacts'
end