Skip to content

Commit

Permalink
starting specs and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mscoutermarsh committed Dec 23, 2012
1 parent 2f4c5f5 commit da3a82c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -13,4 +13,6 @@ capybara-*.html
/spec/tmp/*
**.orig
rerun.txt
pickle-email-*.html
pickle-email-*.html
.idea/*
*.xml
5 changes: 4 additions & 1 deletion Gemfile
Expand Up @@ -2,11 +2,14 @@ source 'https://rubygems.org'

gem 'pg'
gem 'em-postgresql-adapter', :git => 'git://github.com/leftbee/em-postgresql-adapter.git'
gem 'em-http-request'
gem 'rack-fiber_pool', :require => 'rack/fiber_pool'
gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git',
:require => ['em-synchrony', 'em-synchrony/activerecord']

gem 'grape'
gem 'goliath'
gem 'tilt'
gem 'haml'
gem 'haml'
gem 'rack'
gem 'rspec'
21 changes: 21 additions & 0 deletions Gemfile.lock
Expand Up @@ -34,6 +34,16 @@ GEM
rack (~> 1.1)
backports (2.6.4)
builder (3.0.4)
cookiejar (0.3.0)
diff-lcs (1.1.3)
em-http-request (1.0.3)
addressable (>= 2.2.3)
cookiejar
em-socksify
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.5.3)
em-socksify (0.2.1)
eventmachine (>= 1.0.0.beta.4)
em-websocket (0.3.8)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
Expand Down Expand Up @@ -77,6 +87,14 @@ GEM
rack (>= 1.0.0)
rack-respond_to (0.9.8)
rack-accept-media-types (>= 0.6)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.0)
tilt (1.3.3)
tzinfo (0.3.33)
virtus (0.5.2)
Expand All @@ -86,11 +104,14 @@ PLATFORMS
ruby

DEPENDENCIES
em-http-request
em-postgresql-adapter!
em-synchrony!
goliath
grape
haml
pg
rack
rack-fiber_pool
rspec
tilt
32 changes: 15 additions & 17 deletions app/apis/votes.rb
Expand Up @@ -13,12 +13,12 @@ def ip_address

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

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

# validate the voter.
Expand All @@ -33,41 +33,39 @@ def validate
# return number of votes
resource 'votes' do
get "/:contestant" do
votes = Vote.where(:vote => params['contestant'], :valid_vote => true).count
{:votes=>votes}
votes = Vote.where(vote: params['contestant'], valid_vote: true).count
{votes:votes}
end
end

# create and confirm votes
resource 'vote' do
desc "Create new vote"
params do
requires :contestant, :type => Integer, :desc => "Integer for which contestant you're voting for."
requires :contestant, type: Integer, desc: "Integer for which contestant you're voting for."
end
post "/:contestant" do
# Validate voter and then ask the user some math.
if validate then
new_vote = Vote.create(:vote => params['contestant'], :ip=>request.ip)
{:id=>new_vote.id,:question=>new_vote.question}
if validate
new_vote = Vote.create(vote: params['contestant'], ip:request.ip)
{id:new_vote.id,question:new_vote.question}
else
error!({:message=>"You've already voted!"}, 401)
error!({message:"You've already voted!"}, 401)
end
end

desc "Confirm"
params do
requires :answer, :type => Integer, :desc => "Answer the math problem."
requires :answer, type: Integer, desc: "Answer the math problem."
end
post "/confirm/:id/:answer" do
# check the users answer
new_vote = Vote.find(params[:id])
if new_vote.valid_vote then
{:message=>"Already voted."}
elsif new_vote.confirm(params['answer'])
{:message=>"Thanks for voting!"}
else
error!({:message=>"Whoops, wrong answer."}, 400)
end

error!({message:"You've already voted!"}, 401) if new_vote.valid_vote

new_vote.confirm(params['answer']) ? {message:"Thanks for voting!"} : error!({message:"Whoops, wrong answer."}, 400)

end

end
Expand Down
16 changes: 8 additions & 8 deletions app/models/vote.rb
Expand Up @@ -12,15 +12,15 @@ def create_captcha
self.valid_vote = false
self.question = "What's #{int1}+#{int2}?"
self.save
end
end

def make_valid
self.valid_vote = true
self.save
true
end

def confirm(answer)
if answer == self.answer then
self.valid_vote = true
self.save
true
else
false
end
answer == self.answer ? self.make_valid : false
end
end
25 changes: 25 additions & 0 deletions spec/api_spec.rb
@@ -0,0 +1,25 @@
require "spec_helper"


describe Application do
include Goliath::TestHelper

let(:err) { Proc.new { |c| fail "HTTP Request failed #{c.response}" } }


describe "GET /votes/" do
it "should return 0 votes" do
#get_request({:path => "/votes/1"}, err) do |c|
# c.response_header.status.should == 200
# JSON.parse(c.response)['votes'].should == 0
#end
end

it "should return 1 vote" do
#Vote.create(:vote => 1, :valid_vote => true)
#post "/votes/1"
#response.status.should == 200
#JSON.parse(response.body)['votes'].should == 1
end
end
end
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,14 @@
require 'bundler'

Bundler.setup
Bundler.require

require 'goliath/test_helper'

Goliath.env = :test

RSpec.configure do |c|
c.include Goliath::TestHelper, :example_group => {
:file_path => /spec\/integration/
}
end

0 comments on commit da3a82c

Please sign in to comment.