-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
151 lines (125 loc) · 3.91 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#encoding: UTF-8
$LOAD_PATH.unshift(File.expand_path("vendor/ft-0.0.3/lib", File.dirname(__FILE__)))
require 'rubygems'
require 'bundler'
require 'sinatra/base'
require 'erb'
require 'sinatra/partial'
require 'fusion_tables'
require 'json'
require 'pony'
#require 'sequel'
Tilt.register 'md', Tilt::RDiscountTemplate
class NilClass
def empty?; true; end
end
# Application main class
class Techo < Sinatra::Base
register Sinatra::Partial
#################################################################
### helpers functions
#################################################################
helpers do
def root(path)
File.expand_path(path, File.dirname(__FILE__))
end
include Rack::Utils
alias_method :h, :escape_html
def join_cols(row, name)
row.keys.grep(/^#{name}_\d+$/).map do |key|
row[key] unless row[key].empty?
end.compact.join("<br>\n")
end
def yesno(value)
if value == "1"
"Sí"
else
"No"
end
end
def img name
"<img src='/images/#{name}' alt='#{name}'/>"
end
def number_with_delimeter(value)
#
# Add thousands separators to numbers.
#
value.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1.")
end
end # helpers
set :app_file, __FILE__
WWW = /^(https?:\/\/)www\./
COM = /^(https?:\/\/)(.+?)\.com\.ar/
#################################################################
### init
#################################################################
before do
# Application version
@version = "21/10/2013"
if request.url =~ WWW
redirect(request.url.sub(WWW, '\1'), 301)
end
if request.url =~ COM
redirect(request.url.sub(COM, '\1\2.org.ar'), 301)
end
end
configure do
#set :public_folder, Proc.new { File.join(root, "static") }
enable :sessions
end
enable :partial_underscores
set :partial_template_engine, :erb
## HTTP authentication
# We protect all requests (for all directories, etc.) in the application.
# userinfo = "Por favor, ingrese para tener acceso a la información.".encode(Encoding::ISO_8859_1)
# use Rack::Auth::Basic, userinfo do |username, password|
# username == 'catastro' and password == 'utpmp'
# end
#################################################################
### Controller/Handler
#################################################################
get '/' do
footer = partial :"partials/footer"
erb :index, :locals => { :footer => footer }
end
get '/content/:page' do |page|
erb :"content/#{page}"
end
post '/content/contact' do
# SendGrid add-on in heroku for contact mails created at 15-oct-2013.
if params[:organization] then
body = "Nombre: " + params[:first_name] + " " +
params[:last_name] + "\n\n" + "Organización: " +
params[:organization] + "\n\n" + "Email: " +
params[:email] + "\n\n" "Mensaje: " + "\n\n" + params[:message]
else
body = "Nombre: " + params[:first_name] + " " + params[:last_name] +
"\n\n" + "Email: " + params[:email] +
"\n\n" "Mensaje: " + "\n\n" + params[:message]
end
Pony.mail(
:from => params[:first_name] + " " + params[:last_name] +
" <" + params[:email] + ">",
:to => '[email protected]',
:subject => "Relevamiento Argentina: Nuevo mensaje de " +
params[:first_name] + " " + params[:last_name],
:body => body,
:port => '587',
: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 '/content/success'
end
get '/content/success' do
end
# Start the server, if ruby file executed directly.
run! if app_file == $0
end