Skip to content

Commit

Permalink
add question to model. Move captcha/confirm logic to model
Browse files Browse the repository at this point in the history
  • Loading branch information
mscoutermarsh committed Dec 19, 2012
1 parent dfcec19 commit 5e76698
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
32 changes: 16 additions & 16 deletions app/apis/votes.rb
Expand Up @@ -13,46 +13,48 @@ def ip_address

def ip_votes_hour
# more than 3 votes in the past hour?
Vote.find(:all, :conditions => ["created_at > ? AND ip = ?", 1.hour.ago, ip_address]).count >= 3
Vote.find(:all, :conditions => ["created_at > ? AND ip = ? AND valid_vote = ?", 1.hour.ago, ip_address, true]).count <= 3
end

def ip_votes_day
# more than 20 votes in the past day?
Vote.find(:all, :conditions => ["created_at > ? AND ip = ?", 1.day.ago, ip_address]).count >= 20
Vote.find(:all, :conditions => ["created_at > ? AND ip = ? AND valid_vote = ?", 1.day.ago, ip_address, true]).count <= 20
end

def validate
# validate the voter.
# is this and ajax request? and is there an ip address?
# have they votes >= 5 times this hour?

ajax and ip_address and ip_votes_hour and ip_votes_day
# ajax and
ip_address and ip_votes_hour and ip_votes_day

end
end

resource 'votes' do
# get "/ip_votes_hour" do
# Vote.find(:all, :conditions => ["created_at > ? AND ip = ?", 1.hour.ago, ip_address]).count
# end
# get "/ip_votes_day" do
# Vote.find(:all, :conditions => ["created_at > ? AND ip = ?", 1.day.ago, ip_address]).count
# end
get "/" do
ip_votes_day
end

get "/:id" do
Vote.count(:vote => params['id'], :valid => true)
Vote.where(:vote => params['id'], :valid_vote => true).count
end

desc "Create new vote"
params do
requires :vote, :type => String, :desc => "Integer for which contestant you're voting for."
requires :id, :type => String, :desc => "Integer for which contestant you're voting for."
end
post "/create" do
post "/:id" do
# ask the user simple math

if validate then
int1 = 1 + rand(15)
int2 = 1 + rand(4)
answer = int1 + int2
new_vote = Vote.create(:vote => params['vote'], :ip=>request.ip, :valid_vote => false, :answer => answer)
{:id=>new_vote.id,:question=>"What's #{int1}+#{int2}?"}
new_vote = Vote.create(:vote => params['id'], :ip=>request.ip)
{:id=>new_vote.id,:question=>new_vote.question}
else
error!({:message=>"You've already voted!"}, 401)
end
Expand All @@ -67,9 +69,7 @@ def validate
new_vote = Vote.find(params[:id])
if new_vote.valid_vote then
{:message=>"Already voted."}
elsif new_vote.answer == params['answer']
new_vote.valid_vote = true
new_vote.save
elsif new_vote.confirm(params['answer'])
{:message=>"Thanks for voting!"}
else
error!({:message=>"Whoops, wrong answer."}, 400)
Expand Down
23 changes: 21 additions & 2 deletions app/models/vote.rb
Expand Up @@ -2,6 +2,25 @@ class Vote < ActiveRecord::Base

validates :vote, :presence => true
validates :ip, :presence => true
validates :answer, :presence => true


after_create :create_captcha

def create_captcha
int1 = 1 + rand(15)
int2 = 1 + rand(4)
self.answer = int1 + int2
self.valid_vote = false
self.question = "What's #{int1}+#{int2}?"
self.save
end

def confirm(answer)
if answer == self.answer then
self.valid_vote = true
self.save
true
else
false
end
end
end
1 change: 1 addition & 0 deletions db/migrate/1_create_votes.rb
Expand Up @@ -4,6 +4,7 @@ def change
create_table :votes do |t|
t.integer :vote
t.string :ip
t.string :question
t.boolean :valid_vote
t.integer :answer
t.timestamps
Expand Down

0 comments on commit 5e76698

Please sign in to comment.