Skip to content

Commit

Permalink
switch to a waiting list when all seats are filled
Browse files Browse the repository at this point in the history
  • Loading branch information
C. Flores committed Mar 29, 2012
1 parent babebb1 commit 956708d
Showing 1 changed file with 67 additions and 33 deletions.
100 changes: 67 additions & 33 deletions rsvp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
set :port, 5062

NEXT_EVENT = "A get together somewhere!"
WAITING_LIST = "waiting list"
RSVP_LIMIT = 95
CONTACT = "rsvp@someaddress"

Expand All @@ -19,16 +20,25 @@ def rsvps_left()
RSVP_LIMIT - rsvps.length
end

def rsvp(user)
REDIS.set "#{NEXT_EVENT}:#{user['email']}", user.to_json
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)
REDIS.exists "#{NEXT_EVENT}:#{email}"
if (REDIS.exists "#{NEXT_EVENT}:#{email}") || (REDIS.exists "#{WAITING_LIST}:#{email}")
return true
else
return false
end
end

def delete(email)
REDIS.del("#{NEXT_EVENT}:#{email}")
def delete(redis_connection,email)
REDIS.del("#{redis_connection}:#{email}")
end

def get_auth(email)
Expand All @@ -54,6 +64,26 @@ def send_email(email,string)
)
end

def send_waitinglist_email(email,string)
ses = AWS::SES::Base.new(
:access_key_id => 'id',
:secret_access_key => 'key'
)
# stick the user info into the subject instead of headers
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-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
Expand All @@ -73,42 +103,46 @@ def valid_email?(email)
if @seats > 0
erb :open
else
erb :closed
@seats = RSVP_LIMIT - @seats
@waitinglist = waitinglist_count
erb :waitinglist
end
end

post '/rsvp' do
if rsvps_left > 0
if captcha_pass?
user = Hash.new
params[:user].each do |k,v|
user[k] = Sanitize.clean(v)
end
unless (user[:name])
@msg = "Hey, friend. Please enter your first and last name. We might have name tags."
erb :msg
end
email = user["email"]
user["cancel"] = rand(36**15).to_s(36)
if valid_email?(email)
unless already_rsvpd(email)
rsvp(user)
send_email(email,user["cancel"])
erb :confirmed
else
@msg = "you are already rsvp'd for this event"
erb :msg
end
@msg=''
if captcha_pass?
user = Hash.new
params[:user].each do |k,v|
user[k] = Sanitize.clean(v)
end
unless (user[:name])
@msg = "Hey, friend. Please enter your first and last name. We might have name tags."
erb :msg
end
email = user["email"]
user["cancel"] = rand(36**15).to_s(36)
unless valid_email?(email)
@msg = "your email looks fake. are you a bot?"
erb :msg
end
if !already_rsvpd(email)
if rsvps_left > 0
rsvp(NEXT_EVENT,user)
send_email(email,user["cancel"])
erb :confirmed
else
@msg = "your email looks fake. are you a bot?"
erb :msg
rsvp(WAITING_LIST,user)
send_waitinglist_email(email,user["cancel"])
erb :confirmed_waitinglist
end
else
@msg = "the captcha was wrong. are you a bot?"
@msg = "you are already rsvp'd for this event"
erb :msg
end
else #someone is fucking with us
erb :closed
else
@msg = "the captcha was wrong. are you a bot?"
erb :msg
end
end

Expand All @@ -122,7 +156,7 @@ def valid_email?(email)
email = params["email"]
if already_rsvpd(email)
if authstring == get_auth(email)
delete(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"
Expand Down

0 comments on commit 956708d

Please sign in to comment.