Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ gem "autoprefixer-rails"
gem 'rails-assets-angular', "1.3.0.rc.4"
gem 'pusher'

gem 'redis'

group :production, :staging do
gem 'pg'
gem 'rails_12factor'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ GEM
rake (10.3.2)
rdoc (4.1.2)
json (~> 1.4)
redis (3.1.0)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -149,6 +150,7 @@ DEPENDENCIES
rails (= 4.1.6)
rails-assets-angular (= 1.3.0.rc.4)
rails_12factor
redis
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
spring
Expand Down
38 changes: 33 additions & 5 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
class VotesController < ApplicationController
before_filter :set_question, only: [:show]
before_filter :set_question_id, only: [:update]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not wrong and I know it is the rails way but since this is only used in the update function (applies to the other filters below too), I think it is better to keep it inside the respective function. This seems like a premature optimization.

Again, nothing wrong in doing it the way you have it.

before_filter :set_vote_id, only: [:update]
before_filter :throttle, only: [:update]

def update
question_id = params[:vote][:question_id]
vote_id = cookies["vote_#{question_id}"]
vote = Vote.where({secret: vote_id}).first_or_initialize
vote = Vote.where({secret: @vote_id}).first_or_initialize

question = Question.where({secret: question_id}).first
question = Question.where({secret: @question_id}).first
vote.secret = SecureRandom.urlsafe_base64(nil, false) unless vote.secret
vote.question_id = question.id unless vote.question_id
vote.option_id = Option.find(params[:vote][:option_id]).id

Pusher[question_id].trigger("vote", {})
Pusher[@question_id].trigger("vote", {})

if vote.save!
cookies.permanent["vote_#{question.secret}"] = vote.secret
Expand All @@ -26,6 +27,25 @@ def update
def show
end

def throttle
unless Vote.where({secret: @vote_id}).exists?
client_ip = env["REMOTE_ADDR"]
agent_hash = Digest::MD5.hexdigest(env["HTTP_USER_AGENT"])
key = "vote:#{client_ip}-#{agent_hash}"
count = REDIS.get(key)
unless count
REDIS.set(key, 0)
REDIS.expire(key, 10)
end

if count.to_i >= 1
render nothing: true, status: 429
else
REDIS.incr(key)
end
end
end

private

def set_question
Expand All @@ -35,4 +55,12 @@ def set_question
def question_params
params.require(:question).permit(:title)
end

def set_question_id
@question_id = params[:vote][:question_id]
end

def set_vote_id
@vote_id = cookies["vote_#{@question_id}"]
end
end
4 changes: 4 additions & 0 deletions config/initializers/throttle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "redis"

redis_conf = YAML.load(File.join(Rails.root, "config", "redis.yml"))
REDIS = Redis.new(:host => redis_conf["host"], :port => redis_conf["port"])
2 changes: 2 additions & 0 deletions config/redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: localhost
port: 6379