-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacademyvoter.rb
88 lines (70 loc) · 2.13 KB
/
academyvoter.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
#!/usr/bin/env ruby -wKU
# AcademyVoter - By Aaron Cohen
current_path = File.dirname(__FILE__)
$LOAD_PATH << File.join(current_path, 'lib')
require 'rubygems'
require 'sinatra'
require 'YAML'
require 'uri'
require 'Pathname'
require 'votingmachine'
BALLOT_DIR = Pathname.new("./ballots/").expand_path
NOMS_FILE = Pathname.new("./nominees.yaml").expand_path
if not BALLOT_DIR.exist?
Dir.new(BALLOT_DIR)
end
main_ballot_box = BallotBox.new(BALLOT_DIR, NOMS_FILE)
# SinatraStuff
set :port, 9494
enable :sessions
helpers do
def base_url
base = "http://#{Sinatra::Application.host}"
port = Sinatra::Application.port == 80 ? base : base << ":#{Sinatra::Application.port}"
end
def url(path = '')
[base_url, path].join('/')
end
end
get '/' do
erb :home
end
get '/vote' do
if session.has_key?('voter_name') and session['voter_name'] != ""
redirect "/vote/#{URI.encode(session['voter_name'])}"
else
erb :vote_name
end
end
post '/vote' do
if params.has_key?("name") and params[:name] != ""
session['voter_name'] = params[:name]
redirect "/vote/#{URI.encode(params[:name])}"
else
redirect "/vote"
end
end
get '/vote/:voter_name' do
ballot = main_ballot_box.get_ballot_for_name(params[:voter_name])
category, nominees = ballot.get_next_category_and_nominees
if category.nil?
session[:voter_name] = ""
end
erb :vote, :locals => {:voter_name => params[:voter_name], :category => category, :nominees => nominees}
end
post '/vote/:voter_name' do
if params.has_key?("voter_name") and params.has_key?("category") and params.has_key?("choice")
ballot = main_ballot_box.get_ballot_for_name(params[:voter_name])
ballot.vote(params[:category.to_sym], main_ballot_box.get_nominee_by_cat_and_name(params[:category.to_sym], params[:choice]))
ballot.cast_ballot
end
redirect "/vote/#{URI.encode(params[:voter_name])}"
end
get '/change_user' do
session['voter_name'] = ""
redirect '/vote'
end
get '/viewresults' do
erb :results, :locals => {:scores => main_ballot_box.get_all_scores}
end
# End Sinatra Stuff