forked from flores/lamersvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsvp.rb
187 lines (159 loc) · 4.1 KB
/
rsvp.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
#!/usr/bin/env ruby
require 'rubygems'
require 'redis'
require 'json'
require 'sinatra'
require 'sinatra/captcha'
require 'aws/ses'
require 'sanitize'
require 'erb'
set :port, 6061
REDIS = Redis.new
NEXT_EVENT = "20121213"
WAITING_LIST = "#{NEXT_EVENT}-waitinglist"
# no limit at this event
RSVP_LIMIT = 70
CONTACT = "rsvp@js.la"
def rsvps_left()
rsvps = REDIS.keys "#{NEXT_EVENT}*"
return RSVP_LIMIT - rsvps.length
end
def waitinglist_count()
rsvps = REDIS.keys "#{WAITING_LIST}*"
rsvps.length
end
def rsvp(redis_connection,user)
REDIS.set "#{redis_connection}:#{user['email']}", user.to_json
end
def already_rsvpd(email)
if REDIS.exists "#{NEXT_EVENT}:#{email}"
return true
else
return false
end
end
def delete(redis_connection,email)
REDIS.del("#{redis_connection}:#{email}")
end
def get_auth(email)
object = JSON.parse(REDIS.get "#{NEXT_EVENT}:#{email}")
object["cancel"]
end
def send_email(email,string)
ses = AWS::SES::Base.new(
:access_key_id => 'AKIAJBTI4BZFDKJTMZVA',
:secret_access_key => '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'
)
ses.send_email(
:to => email,
:from => CONTACT,
:subject => "You've confirmed one seat for the LA Dev/Ops and JS.LA Holiday Jam on Thursday, December 13th, at 7pm",
:body => "
Hi! We're excited to see you December 13th!
Here's the address:
NextSpace @ Amplify
1600 Main St
Venice, CA 90291
Here's a link: http://goo.gl/maps/2ow7w
You're on your own for parking.
See you there!
the js.la and LA Dev/Ops team
"
)
end
def send_waitinglist_email(email,string)
ses = AWS::SES::Base.new(
:access_key_id => '',
:secret_access_key => ''
)
ses.send_email(
:to => email,
:from => CONTACT,
:subject => "Thanks for getting on our #{NEXT_EVENT} waiting list",
:body => "Hi there. You're now on our waiting list for #{NEXT_EVENT}.
We'll email you if a seat becomes available.
If you have any questions, please feel free to reply to this email."
)
end
# jacked from http://vitobotta.com/sinatra-contact-form-jekyll/
def valid_email?(email)
if email =~ /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
@mx.size > 0 ? true : false
else
false
end
end
# /jacked
get '/holidayjam' do
@seats = rsvps_left
if @seats > 0
@rsvps = RSVP_LIMIT - @seats
erb :open
else
@seats = RSVP_LIMIT - @seats
@waitinglist = waitinglist_count
erb :waitinglist
end
end
post '/holidayjam' do
# if rsvps_left > 0
if captcha_pass?
user = Hash.new
params[:user].each do |k,v|
user[k] = Sanitize.clean(v)
end
email = user["email"]
user["cancel"] = rand(36**15).to_s(36)
if valid_email?(email)
unless already_rsvpd(email)
rsvp(NEXT_EVENT, user)
send_email(email,user["cancel"])
@msg = "Thanks! You have been confirmed for our Holiday Jam. Check your email"
erb :msg
else
@msg = "you are already rsvp'd for this event"
erb :msg
end
else
@msg = "your email looks fake. are you a bot?"
erb :msg
end
else
@msg = "the captcha was wrong. are you a bot?"
erb :msg
end
# else #someone is fucking with us
# erb :closed
# end
end
get '/holidayjam/cancel/:authstring' do |authstring|
@seats = rsvps_left
@authstring = authstring
erb :confirm_cancel
end
post '/holidayjam/cancel/:authstring' do |authstring|
email = params["email"]
if already_rsvpd(email)
if authstring == get_auth(email)
delete(NEXT_EVENT,email)
@msg = "You have canceled from our #{NEXT_EVENT} event"
else
@msg = "Sorry, I think this request is bogus. Email us at #{CONTACT} to cancel"
end
else
@msg = "Sorry, I do not know this email. Email us at #{CONTACT} to cancel"
end
erb :msg
end
get '/rsvplist' do
@rsvps = Hash.new
list = REDIS.keys "#{NEXT_EVENT}*"
list.each do |rsvp|
@rsvps[rsvp] = JSON.parse(REDIS.get rsvp)
end
erb :list
end