forked from datauy/OGPMontevideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogp.rb
257 lines (224 loc) · 7.53 KB
/
ogp.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# coding: utf-8
require 'sinatra'
require 'sequel'
require 'csv'
require 'dotenv'
require 'sinatra/r18n'
require_relative 'models/init.rb'
require_relative 'lib/form_data.rb'
require 'sendgrid-ruby'
include SendGrid
Dotenv.load
R18n::I18n.default = 'es'
# Start Rollbar config
require 'rollbar'
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
end
# End Rollbar config
before do
session[:locale] = params.fetch('locale', 'es')
end
get '/' do
erb :index
end
get '/inscription' do
inscription = Inscription.new
erb :form, locals: { inscription: inscription }
end
post '/enviar' do
inscription = create_inscription(params)
inscription.save
erb :thanks
end
#get '/complete-inscription' do
# inscription = Inscription.new
# erb :complete_form, locals: { inscription: inscription }
#end
post '/complete' do
inscription = Inscription.where('lower(email) = ?', params[:email].downcase)
if inscription.count == 0
inscription = Inscription.new
inscription.valid?
inscription.errors.add(:email, "El email "+params[:email]+" no esta asociado con ninguna inscricpión, porfavor use el mismo email que uso para registrarse anteriormente.")
return erb :complete_form, locals: { inscription: inscription }
end
if params[:event]
inscription.update(
:event => params[:event].join(', '),
:personal_facebook => params[:personal_facebook],
:personal_twitter => params[:personal_twitter],
:personal_instagram => params[:personal_instagram],
:personal_github => params[:personal_github],
:lunch_confirmation => params[:lunch_confirmation],
:food_preference => params[:food_preference] ? params[:food_preference].join(', ') : '',
:allergy => params[:allergy],
:comments_suggestions => params[:comments_suggestions],
:new_inscription_form => 'No' )
else
inscription.update(
:personal_facebook => params[:personal_facebook],
:personal_twitter => params[:personal_twitter],
:personal_instagram => params[:personal_instagram],
:personal_github => params[:personal_github],
:lunch_confirmation => params[:lunch_confirmation],
:food_preference => params[:food_preference] ? params[:food_preference].join(', ') : '',
:allergy => params[:allergy],
:comments_suggestions => params[:comments_suggestions],
:new_inscription_form => 'No' )
end
erb :thanks
end
get '/thanks' do
erb :thanks
end
get '/agenda' do
erb :agenda
end
get '/gobcamp' do
erb :gobcamp
end
get '/taller-datos-abiertos' do
erb :taller_datos_abiertos
end
get '/data-bootcamp' do
erb :data_bootcamp
end
get '/taller-tecnico-regional' do
erb :taller_tecnico_regional
end
get '/data-art' do
erb :data_art
end
get '/siguiendo-el-dinero' do
erb :siguiendo_el_dinero
end
get '/datos-y-chiliguaros' do
erb :datos_y_chiliguaros
end
get '/sedes' do
erb :sedes
end
get '/evento' do
erb :evento
end
get '/ciudad' do
erb :montevideo
end
get '/contact' do
erb :contact
end
post '/contactar' do
data = JSON.parse('{
"personalizations": [
{
"to": [
{
"email":"'+ ENV['CONTACT_EMAIL'] +'"
}
],
"subject": "[CONDATOS/ABRELATAM] - correo de: '+params['name']+'"
}
],
"from": {
"email": "'+params['email']+'"
},
"content": [
{
"type": "text/plain",
"value": "Nombre: '+params['name']+' \n Correo: '+params['email']+' \n Organización: '+params['email']+' \n País: '+params['country']+' \n \n Mensaje: '+params['message']+'"
}
]
}')
sg = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY'])
response = sg.client.mail._("send").post(request_body: data)
puts response.status_code
puts response.body
erb :contactar
end
get '/export_inscriptions' do
protected!
Sequel::Plugins::CsvSerializer.configure(
Inscription,
col_sep: ',',
encoding: 'UTF-8'
)
content_type 'application/csv'
attachment 'inscripciones.csv'
csv = "name,surname,gender,email,country_origin,country_residence,job,open_data_usage,particpated_before,scholarship_before,scholarship_more_than_once,participates_in_representation,event,financial_support,thematic,open_data_problem,organization,organization_type,website,facebook,twitter,instagram,participate_as_colaborator,colaborator_area,has_proposition,proposition_title,proposition_summary,proposition_why_include,proposition_others_needed,organization_role,github,personal_facebook,personal_twitter,personal_instagram,personal_github,lunch_confirmation,food_preference,allergy,comments_suggestions,new_inscription_form\n"
Inscription.each do |inscription|
csv << inscription.to_csv(except: :id)
end
csv
end
def create_inscription(params)
inscription = Inscription.new(
name: params[:name],
surname: params[:surname],
gender: params[:gender],
email: params[:email],
country_origin: params[:country_origin],
country_residence: params[:country_residence],
job: params[:job],
personal_facebook: params[:personal_facebook],
personal_twitter: params[:personal_twitter],
personal_instagram: params[:personal_instagram],
personal_github: params[:personal_github],
open_data_usage: params[:open_data_usage],
particpated_before: params[:particpated_before],
scholarship_before: params[:scholarship_before],
scholarship_more_than_once: params[:scholarship_more_than_once],
participates_in_representation: params[:participates_in_representation],
event: params[:event] ? params[:event].join(', ') : '',
financial_support: params[:financial_support] ? params[:financial_support].join(', ') : '',
thematic: params[:thematic] ? params[:thematic].join(', ') : '',
open_data_problem: params[:open_data_problem],
organization: params[:organization],
organization_role: params[:organization_role],
organization_type: params[:organization_type],
website: params[:website],
facebook: params[:facebook],
twitter: params[:twitter],
instagram: params[:instagram],
github: params[:github],
participate_as_colaborator: params[:participate_as_colaborator],
colaborator_area: params[:colaborator_area] ? params[:colaborator_area].join(', ') : '',
has_proposition: params[:has_proposition],
proposition_title: params[:proposition_title],
proposition_summary: params[:proposition_summary],
proposition_why_include: params[:proposition_why_include],
proposition_others_needed: params[:proposition_others_needed],
lunch_confirmation: params[:lunch_confirmation],
food_preference: params[:food_preference] ? params[:food_preference].join(', ') : '',
allergy: params[:allergy],
comments_suggestions: params[:comments_suggestions],
new_inscription_form: 'Sí' )
inscription
end
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "No está autorizado\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [ENV['CONTACT_EMAIL'], ENV['E_PWD']]
end
def home_url
"#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
end
def clean_current_url
regex = /\?.+/
request.url.gsub(regex, '')
end
def language_buttons
if (session[:locale] == 'es')
"<a class=\"active\">Español</a>" +
"<a href=\"#{clean_current_url}?locale=en\">English</a>"
else
"<a href=\"#{clean_current_url}?locale=es\">Español</a>" +
"<a class=\"active\">English</a>"
end
end
end