-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
95 lines (80 loc) · 2.3 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
92
93
94
95
require 'rubygems'
require 'sinatra'
require 'sinatra/reloader'
require 'sqlite3'
def get_db
SQLite3::Database.new('./database/barbershop.sqlite')
end
configure do
get_db.execute 'CREATE TABLE IF NOT EXISTS "users"
(
"id" INTEGER PRIMARY KEY NOT NULL ,
"username" VARCHAR NOT NULL DEFAULT (null) ,
"phone" VARCHAR OT NULL ,
"datestamp" VARCHAR NOT NULL ,
"barber" VARCHAR NOT NULL ,
"color" VARCHAR NOT NULL
)'
end
get '/' do
erb "Hello! <a href=\"https://github.com/bootstrap-ruby/sinatra-bootstrap\">Original</a> pattern has been modified for <a href=\"http://rubyschool.us/\">Ruby School</a>"
end
get '/about' do
erb :about
end
get '/contacts' do
erb :contacts
end
get '/visit' do
erb :visit
end
post '/visit' do
@username = params[:username]
@phone = params[:phone]
@datetime = params[:datetime]
@barber = params[:barber]
@color = params[:color]
@error = []
hash_errors = {
:username => "Введите имя",
:phone => "Введите телефон",
:datetime => "Введите дату"
}
hash_errors.each {|key, value| @error << value if params[key] == ''}
if @error.size!= 0
return (erb :visit)
end
get_db.execute 'insert into Users (username, phone, datestamp, barber, color)
values(?, ?, ?, ?, ?)', [@username, @phone, @datetime, @barber, @color]
erb "OK, username is #{@username}, #{@phone}, #{@datetime}, #{@barber}, #{@color}"
end
post '/contacts' do
require 'pony'
@email = params[:email]
@message = params[:message]
@error = []
hash_errors = {
:email => "Введите почту",
:message => "Введите текст письма"
}
hash_errors.each {|key, value| @error << value if params[key] == ''}
if @error.size!= 0
return (erb :visit)
end
Pony.mail({
:to => @email,
:via => :smtp,
:subject => "Отзыв barber",
:body => @message,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => '',
:password => '',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
}
})
erb "OK, to mail #{@email} send message."
end