-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
69 lines (60 loc) · 1.84 KB
/
app.rb
File metadata and controls
69 lines (60 loc) · 1.84 KB
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
# import libs
# import routes
require 'sinatra'
require 'sinatra/asset_pipeline'
require 'sinatra/partial'
MAIL_ATTRIBUTES = [:name, :mail, :budget]
def invalid_attributes
attributes = []
MAIL_ATTRIBUTES.each do |attr|
attributes << attr if params[:message][attr].nil? || params[:message][attr].strip.empty?
end
attributes << :mail unless params[:message][:mail] && params[:message][:mail].match(/.+@.+\..+/)
attributes
end
def is_valid_mail?
invalid_attributes.empty? && not_spam?
end
def not_spam?
antispam = params[:message][:antispam]
spam = params[:message][:spam]
antispam == '' && spam == ''
end
class App < Sinatra::Base
#set :assets_prefix, %w(assets assets/sass)
set :assets_precompile, %w(*.js styles.css *.png *.jpg *.svg *.eot *.ttf *.woff)
set :assets_css_compressor, :sass
set :assets_js_compressor, :uglifier
register Sinatra::AssetPipeline
# Partials
register Sinatra::Partial
set :partial_template_engine, :slim
get '/' do
slim :index
end
post '/contact' do
return redirect("/?errors=#{invalid_attributes.map(&:to_s).join(",")}") unless is_valid_mail?
name = params[:message][:name]
mail = params[:message][:mail]
budget = params[:message][:budget]
body = params[:message][:body]
Pony.mail(
:to => 'contato@codeland.com.br',
:from => mail,
:reply_to => mail,
:subject => 'Desenvolvimento de Sistema Web',
:body => "Nome: #{name}\n" + "E-mail: #{mail}\n" + "Orçamento: #{budget}\n" + body,
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
)
redirect "/?success=success"
end
end